@@ -50,6 +50,7 @@ class CentralManager(
50
50
private var isScanning = false
51
51
private var lastNScans = mutableListOf<Long >()
52
52
private val scanMutex = Mutex ()
53
+ private val excludeFromScan = mutableSetOf<String >()
53
54
54
55
@Throws(SecurityException ::class )
55
56
suspend fun scanForPeripherals (serviceIds : List <String > = listOf()) {
@@ -59,7 +60,9 @@ class CentralManager(
59
60
// Android excludes bonded devices from showing up in advertisements. So we need
60
61
// to connect to it in order to check out its services. We'll disconnect if it's
61
62
// of no use to us.
63
+ excludeFromScan.clear()
62
64
btMan.adapter.bondedDevices.forEach { device ->
65
+ excludeFromScan.add(device.address)
63
66
CoroutineScope (Dispatchers .IO ).launch {
64
67
for (i in 1 .. 3 ) {
65
68
try {
@@ -86,7 +89,7 @@ class CentralManager(
86
89
disconnectFromDevice(device.address)
87
90
}
88
91
} 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)
90
93
}
91
94
delay(5000 )
92
95
}
@@ -222,6 +225,11 @@ class CentralManager(
222
225
) {
223
226
super .onScanResult(callbackType, result)
224
227
CoroutineScope (Dispatchers .IO ).launch {
228
+ scanMutex.withLock {
229
+ if (excludeFromScan.contains(result.device.address)) {
230
+ return @launch
231
+ }
232
+ }
225
233
_scanForPeripheralFlow .emit(
226
234
Result .success(
227
235
hashMapOf(
0 commit comments