Bluetooth scanner returns no results

1 week ago 15
ARTICLE AD BOX

I'm developing an app to connect to a Bluetooth device. I'm having trouble with the Bluetooth scanner part of the app.

const val PERMISSION_BLUETOOTH_SCAN = "android.permission.BLUETOOTH_SCAN" class BLEScanner(context: Context) { private val bluetooth = context.getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager ?: throw Exception("Bluetooth is not supported by this device") val isScanning = MutableStateFlow(false) val foundDevices = MutableStateFlow<List<BluetoothDevice>>(emptyList()) private val scanner: BluetoothLeScanner get() = bluetooth.adapter.bluetoothLeScanner private val scanCallback = object : ScanCallback() { override fun onScanResult(callbackType: Int, result: ScanResult?) { super.onScanResult(callbackType, result) Log.d("BTScanning", result.toString()) result ?: return if (!foundDevices.value.contains(result.device)) { foundDevices.update { it + result.device } Log.d("BTDevice", "Device Result: $result.toString()") } else Log.d("BTScanning", "No devices found") } override fun onBatchScanResults(results: MutableList<ScanResult>?) { super.onBatchScanResults(results) Log.d("BTScanning", "Batch Results: $results.toString()") } override fun onScanFailed(errorCode: Int) { super.onScanFailed(errorCode) isScanning.value = false Log.d("BTScanning", "Scan failed: $errorCode") } } @RequiresPermission(PERMISSION_BLUETOOTH_SCAN) fun startScanning() { scanner.startScan(scanCallback) Log.d("BTScanning", "StartScanning() called") } @RequiresPermission(PERMISSION_BLUETOOTH_SCAN) fun stopScanning() { scanner.stopScan(scanCallback) isScanning.value = false } }

The only log result that I get is "StartScanning() called", which is initiated by a button press in a view. If there was no devices found shouldn't I get a null result from onScanResult() or a "No devices found"? I don't get an error log message (from onScanFailed()) either. I used the nRF Connect app to scan for devices so I know there are devices around me advertising.

Read Entire Article