Android application: scan callbacks stop when screen is off

We have an Android application developed in Kotlin which uses the Nordic scanner library (Android-Scanner-Compat-Library) version 1.6.0.  It scans for devices which use the nRF52840. Our app specifies compileSdkVersion 31, minSdkVersion 26, targetSdkVersion 31.

In our app, we periodically stop BLE scanning and restart it. When the screen is off or the app is minimized, scanning/scan callbacks continue to work until one of the periodic intervals comes around to stop and restart scanning. When scanning is restarted, we no longer receive any scan callbacks from BLE advertisements.

Nordic documentation for the Android BLE Scanner Compat Library states "Note, that for unfiltered scans, scanning is stopped on screen off to save power. Scanning is resumed when screen is turned on again. To avoid this, use scanning with desired ScanFilter." This doesn't appear to be our problem for 2 reasons:

  • we are using a scan filter
  • with the screen off, the scan callbacks continue to work until we explicitly stop and restart scanning

We use SCAN_MODE_LOW_LATENCY for our scanning, which we understand will be more power hungry. Nordic documentation mentions the following about SCAN_MODE_LOW_POWER: "Perform Bluetooth LE scan in low power mode...This mode may be enforced if the scanning application is not in foreground."  Could our issue be that even though we are specifying SCAN_MODE_LOW_LATENCY, the system is automatically switching over to use SCAN_MODE_LOW_POWER when we restart scanning? If so, it seems surprising that we receive NO scan callbacks at all.

Thank you for your help with this issue.

John

  • Hi John,

    I will consult the library's author and come back with information. Please be informed that next week, there is a holiday here, so our response will likely be delayed until the week after that (22 May - 26 May). My apology for the inconvenience.

    Hieu

  • Hi John,

    I got some support far earlier than I thought. Our expert thinks that the reason could be due to the scan being stopped by the OS, rather than something in the library.

    They recommend using scanning with PendingIntent instead. Refer to startScan().

    By the way, I notice that you said you target minSdkVersion 26. The Android Scanner Compat Library is to help older Android versions support API level 26. If you don't support anything older than 26, then you don't need it at all.

    Hieu

  • Thank you, Hieu!  I wondered if you could help me understand your comment about the Android Scanner Compat Library?  Since our minSDKVersion is 26, are you saying we don't actually need to use the the Android Scanner Compat Library and instead we could directly use the Android BluetoothLeScanner class? What would be the disadvantage to using the Nordic Compat library in this case?

    Best regards,

    John

  • Hi John,

    jpw3 said:
    Since our minSDKVersion is 26, are you saying we don't actually need to use the the Android Scanner Compat Library and instead we could directly use the Android BluetoothLeScanner class?

    Yes.

    jpw3 said:
    What would be the disadvantage to using the Nordic Compat library in this case?

    Please see from the library's README:

    The Scanner Compat library solves the problem with scanning for Bluetooth Low Energy devices on Android. The scanner API, initially created in Android 4.3, has changed in Android 5.0 and has been extended in 6.0 and 8.0. This library allows to use modern API even on older phones, emulating not supported features.

    The library is meant to provide support for API level 26, Android 8.0, to older versions of Android. As your minimum supported Android version is already 26, the library provides no benefit.

    Best regards,

    Hieu

Related