Closed rickymohk closed 11 months ago
Scanning is not in scope of this library. I recommend asking this question on StackOverflow.
As a quick answer I suggest using scan with a PendingIntent
. This is the desired way to handle continuous scan on Android 8+. Here is the API.
Oops, sorry, I mixed it up. Should have asked this in Android-Scanner-Compat-Library
I am developing an Android app to continuously scan for BLE devices.
First I encounter this problem https://stackoverflow.com/questions/43833904/android-bluetooth-le-scanner-stops-after-a-time
So I implement a timer to restart the scan at regular interval. But I still find it happening sometimes. When I dig into the Android source code, I found a subtle difference in the latest version of ScanManager.java.
In Android 7 :
In latest version:
The
removeMessage
call is removed insidehandleStartScan
. TheMSG_SCAN_TIMEOUT
message can now only be removed inhandleStopScan
.I suspect when I kill my app, I cannot call
stopScan
since my app is killed. I also lost the reference to myscanCallback
so there is no way for me to callstopScan
in new app session. Hence there is no way to cancel theMSG_SCAN_TIMEOUT
previously set up. When I start my app again and obtain the BLE scanner with the samescannerId
, the persistingMSG_SCAN_TIMEOUT
will move my newly started scan to opportunistic. Am I correct about this? If this is the case, is this a bug in Android OS and is there any workaround I can do?