ScanResultAdapter

RecyclerView adapter for displaying discovered BLE devices.

This adapter shows a list of BluetoothDevice objects discovered during BLE scanning. Each item displays the device name (or "Unknown Device" if not available) and the device's MAC address. Clicking on an item triggers the onDeviceClick callback.

Features

  • Displays device name and MAC address

  • Handles unnamed devices gracefully

  • Click listener for device selection

  • Efficient view recycling

Usage Example

val devices = mutableListOf<BluetoothDevice>()
val adapter = ScanResultAdapter(devices) { device ->
// Handle device selection
connectToDevice(device)
}
recyclerView.adapter = adapter

// When new device is discovered:
devices.add(discoveredDevice)
adapter.notifyItemInserted(devices.size - 1)

Permissions Note

Accessing BluetoothDevice.getName requires BLUETOOTH_CONNECT permission on Android 12 (API 31) and above. Make sure this permission is granted before using this adapter.

Parameters

devices

The mutable list of discovered devices to display.

onDeviceClick

Callback invoked when a device item is clicked.

See also

Constructors

Link copied to clipboard
constructor(devices: MutableList<BluetoothDevice>, onDeviceClick: (BluetoothDevice) -> Unit)

Types

Link copied to clipboard

ViewHolder for a single device item.

Functions

Link copied to clipboard

Adds a device to the list if it's not already present.

Link copied to clipboard

Clears all devices from the list and notifies the adapter.

Link copied to clipboard
open override fun getItemCount(): Int
Link copied to clipboard
open fun getItemId(p0: Int): Long
Link copied to clipboard
open fun getItemViewType(p0: Int): Int
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun notifyItemMoved(p0: Int, p1: Int)
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open fun setHasStableIds(p0: Boolean)