13

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.

Redhunt
  • 165
  • 1
  • 10
  • 1
    If I put a alarm in the phone every 10 minutes and lock the screen putting other alarm for the next 10 minutes the scanner BLE never stops. But if i put extern alarm and only move the phone every 10 minutes without unlock the phone, the scanner 30 minutes after lock the screen stops reading. – Redhunt May 08 '17 at 05:51
  • 1
    I didn't have form to have the scanner ble always running after all my test in background mode without touching the phone. – Redhunt May 13 '17 at 10:27

1 Answers1

18

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.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • 2
    What is really the point of this strange new "feature"? There are a lot of apps that need to continuously scan in the background for BLE devices, and this new "feature" breaks those apps. It feels very arbitrary to just stop the scan after 30 minutes, especially if the app already uses a "low power" scan mode. Programmers are just going to use a timer to restart it after 30 minutes anyway now as a workaround. – Emil Jun 12 '17 at 00:14
  • 1
    True. One benefit would be to save battery in cases where programmers left scanning on inadvertently. – davidgyoung Jun 12 '17 at 11:00
  • 1
    @davidgyoung, you helped me and solve my issue when I posted it in the github https://github.com/AltBeacon/android-beacon-library/issues/506. Thank you for your help. – Redhunt Jun 13 '17 at 17:21
  • 1
    For anyone wondering, this is still a "feature" in android 11. The integer has moved to a file called AppScanStats, here https://android.googlesource.com/platform/packages/apps/Bluetooth/+/refs/heads/android11-release/src/com/android/bluetooth/gatt/AppScanStats.java#111 In my app we stop and restart the scan every 15 minutes, to get around this limitation – Adam Diament Sep 15 '21 at 13:10