Android Bluetooth Le scanner stops after a while

I run an application or service with an active Bluetooth LE scanner and display the scan results on the Log console. If I close the phone at the table and don’t touch it anymore. After a while, it stops, and this does not give me more scan results.

If I press the power button and the screen wakes up, it will give me more scan results. If I close the screen again or wait for it to automatically lock, it stops and does not give me more scan results.

I am testing a service and application that give me more results in the log and see that the application is working, but the LE scanner stops and does not give me more results while the screen is locked.

I have an application in “no optimized battery” for the dose mode. I test forcing with a command that enters the phone mode in doze mode and works great to give me scan results.

In my Nexus 5 with Android 7.1.1 interface, when starting APP and locking the screen, rather than touching the phone anymore. The time is 30 minutes. The phone is in one desk, only connected to microusb, to see the magazine in android studio.

In another Moto G2 with android 7.1, they go exactly, but the time between 20 and 40 minutes is rather aleatory. The phone is in one desk, only connected to microusb, to see the magazine in android studio.

In order to work well again, I need to forcefully close the application manually and restart, otherwise it only works when the screen is active and does not give me more results when the screen is locked.

This is used to get the beacon results, first I use the Android Beacon Library for this purpose, and the result was the same.

I think this is a bluetooth component problem for Android, because I have the same result with Android Beacon Library, or if I implement my own BLE scanner, but I do not know how to solve it.

Are there any forms for using Bluetooth Scanner LE that always work in Android when the phone takes a long time in a locked state?

Thanks in advance.

Sincerely.

+5
source share
1 answer

Android 7.0 introduced the BLE scan timeout, where any scan that runs for 30 minutes or more effectively stops automatically and resumes “opportunistically,” which essentially means that if another process runs the scan, it can also get results.

This can be seen by setting the code to start the Bluetooth LE scan and leave it indefinitely. After exactly 30 minutes, the scan will stop and you will see the following entries in LogCat:

06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: clientIf set to scan opportunisticly: 6 06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: configureRegularScanParams() - queue=1 06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: configureRegularScanParams() - ScanSetting Scan mode=-1 mLastConfiguredScanSetting=2 06-11 19:00:22.848 5123 5147 D BtGatt.ScanManager: configureRegularScanParams() - queue emtpy, scan stopped 06-11 19:00:22.849 5123 5147 D BtGatt.ScanManager: stop scan 

Here you can see the code that does this in the AOSP source:

https://android.googlesource.com/platform/packages/apps/Bluetooth/+/android-7.0.0_r1/src/com/android/bluetooth/gatt/ScanManager.java#72

The workaround for this is to not continue a long scan. You can just stop them and restart them periodically.

+9
source

All Articles