BLE on Android - Get UUID of nearby devices

62 Views Asked by At

I am creating an app that through a ble scan returns a list of nearby devices, each device is an object which contains two pieces of information:

  • UUID : String,
  • rssi: Int

I can get the rssi value and device address but I can't figure out how to get the uuid. I can't find explanations on the basic code to implement and most of the answers are on Java code, which is not very easy to adapt into kotlin, at least for me.

I tried this way, but I don't know if I'm doing it right or if I'm completely off track for what I would like to achieve

Scan Settings:

private val scanSettings = ScanSettings.Builder()
        .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
        .build()`

Scan Callback

private val scanCallback = object : ScanCallback() {
        @SuppressLint("MissingPermission")
        override fun onScanResult(callbackType: Int, result: ScanResult) {

            val listUUID = result.scanRecord?.serviceUuids
            if(listUUID != null){
                val parcelUuid =  listUUID[0]
                val device = Device(devUUID = parcelUuid.uuid.toString(), rssi = result.rssi)
                listDevice.add(device)
            }

        override fun onScanFailed(errorCode: Int) {
            Log.e("ScanCallback", "onScanFailed: code $errorCode")
        }
    }

To start the scan bleScan.startScan(null, scanSettings, scanCallback)

0

There are 0 best solutions below