Android Bluetooth Le scanner stops after a time

守給你的承諾、 提交于 2020-07-05 01:57:28

问题


I am runnning an app or service with a active Bluetooth LE scanner and showing scan results on Log console. If I lock the phone in a table and not touching anymore. After a time it stops, and it doesn't give me more scan results.

If I press power button and the screen wake up it gives me more scan results. If i lock again the screen or wait to lock automatically it stops and not give me more scan results.

I test with service and an app that give me more results by Log and see the app is running but scanner LE stops and no give me more results while the screen is lock.

I have the app in "no optimized battery" for doze mode. I test forcing by command introducing the phone en doze mode and work fine it give me scan results.

In my Nexus 5 with Android 7.1.1 pass when running APP and lock the screen and not touch anymore the phone. The time is 30 minutes. The phone is in a table alone, only connected with microusb to see the log in android studio.

In other Moto G2 with android 7.1 pass exactly but the time is between 20 minutes and 40 minutes, it is more aleatory. The phone is in a table alone, only connected with microusb to see the log in android studio.

For have running well again, I need to force close the app manually and restart, otherwise only works when screen is active and no give me more results when screen is locked.

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

I think it is a problem of android bluetooth component, because I have the same result with the Android Beacon Library or if I implement my own BLE Scanner, but I don't know how to solve this.

Are any form to use Bluetooth Scanner LE always running in Android when the phone is much time in lock state??

Thanks in advance.

Best regards.


回答1:


Android 7.0 introduced a BLE scan timeout, where any scan running for 30 minutes or more is effectively stopped automatically and only resumed "opportunistically" which essentially means that if another process does a scan, it can get the results as well.

You can see this by setting up code to start a Bluetooth LE scan and leave it running indefinitely. After exactly 30 minutes, the scan will stop, and you will see entries like this 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

You can see the code that does this in the AOSP source here:

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

A workaround for this is to not keep scans going that long. You can simply stop them and restart them periodically.



来源:https://stackoverflow.com/questions/43833904/android-bluetooth-le-scanner-stops-after-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!