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

Parents
  • 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

Reply
  • 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

Children
  • 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

  • No problem, I'm happy to help. Please feel free to close this question at your convenience.

  • I wanted to provide an update to this issue. In our case, getting scan results via PendingIntent did not work. As explained below, our problem was related to app permissions.

    To continue to get scan results when the app is in the background or screen is locked, the app needs to have permission ACCESS_BACKGROUND_LOCATION. On Android 10 or earlier, when first installing/launching the app and the user is prompted to accept location permission, they can specify option "Allow all the time". In addition to allowing the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission, my understanding is that this also implicitly gives the app the ACCESS_BACKGROUND_LOCATION permission.

    Starting with Android 11, this changed. When prompting for location permission on Android 11 or later, there is no "Allow all the time" option; at most there is a permission level to use location "While using the app". This DOES NOT provide the implicit ACCESS_BACKGROUND_LOCATION permission. The user can manually go to Settings on the phone and go to the App's location permissions and specify "Allow all the time". Better solution would be to prompt the user for this permission; I haven't tried it yet, but the following page discusses how to do this (looks like the app needs to prompt for ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission first and then on the response to that then prompt for ACCESS_BACKGROUND_LOCATION permission):

    https://stackoverflow.com/questions/64246883/android-11-users-can-t-grant-background-location-permission

    Also see the following page for more information about this issue:

    https://developer.android.com/training/location/permissions#request-background-location

    Best regards,

    John

  • I'm sorry my suggestion didn't work, and thanks a lot for updating us about your findings, John. It is very helpful for the community.

    If you feel satisfied with your answer, please feel free to verify it any time.

    Best regards,

    Hieu

Related