@@ -50,6 +50,7 @@ class CentralManager(
5050 private var isScanning = false
5151 private var lastNScans = mutableListOf<Long >()
5252 private val scanMutex = Mutex ()
53+ private val excludeFromScan = mutableSetOf<String >()
5354
5455 @Throws(SecurityException ::class )
5556 suspend fun scanForPeripherals (serviceIds : List <String > = listOf()) {
@@ -59,7 +60,9 @@ class CentralManager(
5960 // Android excludes bonded devices from showing up in advertisements. So we need
6061 // to connect to it in order to check out its services. We'll disconnect if it's
6162 // of no use to us.
63+ excludeFromScan.clear()
6264 btMan.adapter.bondedDevices.forEach { device ->
65+ excludeFromScan.add(device.address)
6366 CoroutineScope (Dispatchers .IO ).launch {
6467 for (i in 1 .. 3 ) {
6568 try {
@@ -86,7 +89,7 @@ class CentralManager(
8689 disconnectFromDevice(device.address)
8790 }
8891 } catch (e: Throwable ) {
89- Log .d(TAG , " failed to connect to bonded device" , e)
92+ Log .d(TAG , " failed to connect to bonded device ${device.address} " , e)
9093 }
9194 delay(5000 )
9295 }
@@ -222,6 +225,11 @@ class CentralManager(
222225 ) {
223226 super .onScanResult(callbackType, result)
224227 CoroutineScope (Dispatchers .IO ).launch {
228+ scanMutex.withLock {
229+ if (excludeFromScan.contains(result.device.address)) {
230+ return @launch
231+ }
232+ }
225233 _scanForPeripheralFlow .emit(
226234 Result .success(
227235 hashMapOf(
0 commit comments