BLE scanning does not work when the screen is off on Android 8.1.0

I am using a pixel with the latest update of android 8.1.0.

I ran into a problem related to crawling BLE ads. Whenever I turn off the screen (for example, the power button), my scan will stop. it will restart immediately after turning on the screen.

I checked the latest code for BLE. Google will introduce this feature again ( Link Link ).

Is there a way to skip this part, I mean that the scan should not stop regardless of whether the screen is on or off.

+20
source share
4 answers

Starting with Android 8.1, unfiltered scanning via Bluetooth is blocked when the screen turns off. Despite the fact that such a significant change was made in a small release of Android, it is, of course, a deliberate change based on comments in the commit: Stop unfiltered BLE scans when the screen turns off.

The workaround is to use ScanFilter with all scans. The new 8.1 operating system code simply confirms that all active scans, when the screen is off, have at least one scan filter. If these conditions are met, the scan results are issued as in Android 8.0.x and earlier versions.

To set up such a scan, you must use the APIs introduced in Android 5.0 and create a ScanFilter with each scan. Below is a filter that will find manufacturer’s ads for any Apple device with manufacturer ID 0x004c (including iBeacons):

 ScanFilter.Builder builder = new ScanFilter.Builder(); builder.setManufacturerData(0x004c, new byte[] {}); ScanFilter filter = builder.build(); 

Similarly, if you are interested in GATT service advertisements (such as those used with Eddystone beacons), you can search for the GATT service UUIDs using a filter similar to the following:

 ScanFilter.Builder builder = new ScanFilter.Builder(); String serviceUuidString = "0000feaa-0000-1000-8000-00805f9b34fb"; String serviceUuidMaskString = "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"; ParcelUuid parcelUuid = ParcelUuid.fromString(serviceUuidString); ParcelUuid parcelUuidMask = ParcelUuid.fromString(serviceUuidMaskString); builder.setServiceUuid(parcelUuid, parcelUuidMask); ScanFilter filter = builder.build(); 

If necessary, you can add several filters to one scan, and any of them will return results. The only real limitation here is that you must know all the manufacturer codes or all the GATT UIIDs that may match in advance, at least when scanning with the screen off.

You start a scan using code similar to the following:

 bluetoothAdapter.getBluetoothLeScanner().startScan(filters, settings, scanCallback); 

EDIT: It is also possible to do with an empty ScanFilter which looks like this:

 ScanFilter.Builder builder = new ScanFilter.Builder(); ScanFilter filter = builder.build(); 

If you use such a scan filter, it will match any proposal and still allow screen off detection on Android 8.1, which will actually give you the same behavior on Android 8.0.x and earlier.

EDIT 2 : On Galaxy Note 9 with Android 8.1, and possibly on other Samsung devices with 8.1, scanning is blocked with the screen turned off even when the scan filter is empty. Scanning is allowed with the screen off with a non-empty scan filter, as described above.

+33
source

Obviously not, if they hadn't missed anything. But it will still work in the background if you have scan filters that you should have anyway. So is this really a problem?

+1
source

I ran into the same problem. I had scan filters to scan BLE devices, even if the screen was locked. But it didn’t work on Samsung devices, so I searched the Samsung forum and found the Knox SDK ( https://seap.samsung.com/sdk/knox-android ).

And that was the solution to my problem. All you have to do is add it to your application, create a license, activate it, and finally use this addPackageToBatteryOptimizationWhiteList method to unlock the scan when the Samsung device’s screen is locked.

+1
source

I have the same problem on phone 10 (COL L-29) 8.1.0, but adding a service filter does not solve the scanner problem. Do you have other ideas or have you encountered the same problem in EMUI? For information, on my device, the advertising data contains only the UUID service.

Thank you in advance ;)

0
source

Source: https://habr.com/ru/post/1274467/


All Articles