androidandroid-bluetoothandroid-ble

Android BLE onScanResult is never called in the background on Android 11. Was working on Android 10


I have an app which scans for BLE devices. It was working perfectly on Android 10, but since I updated my phone to Android 11, the onScanResult just never gets called if I put the application to the background, or if I lock the screen.

This is quite annoying. I haven't found any reasonable ideas what could cause this. I haven't found any differences in Android 11 whatsoever which would indicate this behavior change. Android 12 has new BT permissions if you target your app to api level 31, but I do target mine to api level 30, and I do run my app on Android 11.

I'm absolutely clueless. I've tried different scan modes, as well with adding scan filter but nothing has changed.

My scan settings:

ScanSettings ss = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER).build();
        List<ScanFilter> filterList = new ArrayList<>();
        filterList.add(new ScanFilter.Builder().setManufacturerData(Constants.HARDWARE_MANUFACTURER_ID, new byte[]{}).build());
        BluetoothUtil.getBluetoothLeScannerInstance().startScan(filterList, ss, leScanCallback);

So far I experience the following:

The onScanResult calls perfectly when the app is in the foreground and the device's screen is not locked. As soon as I push the app to the background or I lock the screen I no longer getting any callbacks for onScanResult

Here is an example log when the app is in the foreground, not locked screen:

15:47:15 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:47:17 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:21 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:21 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:22 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:22 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)
15:47:25 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:47:25 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:26 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:26 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:29 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:30 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:30 com.myapp.co I/SCANNED_: onScanResult ran!
15:47:32 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)

And an example log when the app is in background and or locked screen:

15:48:25 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:48:32 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)
15:48:35 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:48:42 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)
15:48:45 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:48:52 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)
15:48:55 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:49:02 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)
15:49:05 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:49:12 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)
15:49:15 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:49:22 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)
15:49:25 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:49:32 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)
15:49:35 com.myapp.co I/BL_START: ...Starting BL scan. (Start of current window)
15:49:42 com.myapp.co I/BL_STOP: ...Finishing BL scan. (End of current window)

As you can see the onScanResult are getting called as it should, when the app is in the foreground and the screen is not locked.

What may cause this, and how can I prevent such functionality?


Solution

  • Apparently, there is a bug in Android 11. If you ask both of the location permissions at the same time, it wouldn't pop the permission dialog. You have to ask them one by one.