Oleh Ponomarenko
03/07/2019, 10:07 AMOleh Ponomarenko
03/07/2019, 10:18 AMCoroutineScope(Dispatchers.Default).launch {
BluetoothAdapter.getDefaultAdapter()?.takeIf { !it.isEnabled }?.enable()
if (bluetoothScanner == null) {
delay(3000)
bluetoothScanner = BluetoothAdapter.getDefaultAdapter()?.bluetoothLeScanner
}
try {
bluetoothScanner?.startScan(filters, settings, callback)
} catch (e: IllegalStateException) {
Log.e(TAG, e.localizedMessage)
}
}
Can you help to implement this due to my picture?
I need to scan for 30 seconds, then stop for 10 seconds, then resume to scanning for 30 seconds. This cycle should be endless.gildor
03/07/2019, 10:27 AMOleh Ponomarenko
03/07/2019, 10:29 AMgildor
03/07/2019, 10:52 AMOleh Ponomarenko
03/07/2019, 10:55 AMgildor
03/07/2019, 10:55 AMOleh Ponomarenko
03/07/2019, 10:56 AMCoroutineScope(Dispatchers.Default).launch {
while (true) {
val timeout = 20000L
try {
Log.d(TAG, "starting")
bluetoothScanner?.startScan(filters, settings, callback)
withTimeout(timeout) {
Log.d(TAG, "stop scan")
bluetoothScanner?.stopScan(callback)
}
delay(10000)
Log.d(TAG, "Pause works fine")
} catch (e: Exception) {
Log.e(TAG, e.localizedMessage)
}
}
}
?gildor
03/07/2019, 10:59 AMOleh Ponomarenko
03/07/2019, 11:09 AMsitepodmatt
03/07/2019, 1:36 PMbdawg.io
03/07/2019, 4:37 PMwithTimeout
is collaborative cancellation. If stopScan
is a blocking method that takes 25 seconds, it won’t be cancelled at the 20 second timeout.Oleh Ponomarenko
03/07/2019, 4:44 PMgildor
03/07/2019, 5:01 PM