AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons
Apache License 2.0
2.83k stars 836 forks source link

Huawei Blocks Scans When Screen is Off #554

Open zukopvd opened 6 years ago

zukopvd commented 6 years ago

Expected behavior

An application should be able to detect beacon region exit or entrance regardless of the application state: either foreground or background.

Actual behavior

When an application is in the background mode and screen is locked after some iterations of BeaconManager's scanning you can see the following error in logs: E/BtGatt.GattService: App 'package_name' is scanning too frequently on screen off

After that each subsequent iteration causes the same error. After first appearance of this error exit from beacon's region may happen (if was entered previously) and no more any region entrance can be detected.

Steps to reproduce this behavior

  1. Configure the BeaconManager for background scanning like that:

    beaconManager.setBackgroundMode(true);
    beaconManager.setBackgroundScanPeriod(5000L);
    beaconManager.setBackgroundBetweenScanPeriod(30000L);
  2. Start the application

  3. Go to the background using "Home" button

  4. Lock the device's screen

  5. See device's logs by tag "scan" without package filters

Mobile device model and OS version

Samsung Galaxy S7 (Android 7.0) Huawei Honor 8 (Android 7.0)

Android Beacon Library version

2.11

As I know it is a quite common issue related to Android's N DOZE mode. Some other beacon manufacturers (kontaktio, estimote) faced the same issue (with exact same case and error) lately. Mostly, the following official restriction mentioned: "Since Nougat, every application is allowed to start/stop BLE scan a maximum of 5 times per 30s." but with the AltBeacon library the issue occurs even if I configure the BeaconManager like that:

beaconManager.setBackgroundScanPeriod(200L);
beaconManager.setBackgroundBetweenScanPeriod(120000L);

I've read issues #506, #509 and #512 but didn't found working solution. I really like AltBeacon library, it works so great but this issue totally confused me. Please, help. Maybe I missed something.

Thank you!

IMPORTANT: This forum is reserved for feature requests or reproducible bugs with the library itself. If you need help with using the library with your project, please open a new question on StackOverflow.com.

carbonaro commented 6 years ago

Hello David,

Itried #663 on a Huawei Honor 5C but it does not seem to work unfortunately. For the purpose of testing it, I used the support-foreground-service branch of the library in the reference app, but with the screen off, no detection occurs. Any thoughts on what to look for in the logs ?

davidgyoung commented 6 years ago

@carbonaro, please try this branch here: AltBeacon/android-beacon-library-reference#46

carbonaro commented 6 years ago

Thanks @davidgyoung ! Tested it and it works for about ~ 40 s, which is approximately the time it takes for the "G.GattService:App 'org.altbeacon.beaconreference' is scanning too frequently on screen off" error to fire up. After that, detection only works again when the screen is turned on.

Then I have increased the backgroundScanPeriod to something much bigger than 1100 ms - 1 hour for instance - and detection with screen off still works after a couple of minutes. Your assumption regarding not restart scanning might work with the foreground service. Still more testing to do and I fear this will not fit my use case, as I need this to work without draining the battery.

davidgyoung commented 6 years ago

@carbonaro, yes, there are two problems with background scanning on Huawei devices, and the foreground service only fixes the first of them:

  1. Huawei kills background services that run for ~80 minutes unless they are a foreground service.

  2. Huawei limits starting bluetooth scans with the screen off.

If you change the betweenScanPeriod to 30 minutes and have a beacon transmitting in the vicinity, I have confirmed Huawei lets you do a scan in the background every 30 minutes with a foreground service as @Fintasys reported above.

One major caveat here: if no beacon is visible, the library will try to start an immediate lower power scan as soon as the app goes to the background. This gets blocked with the same error "scanning too frequently" message, and then so do all subsequent scans. So it seems these low power scans would have to be disabled somehow for this combination of a foreground service and rare background scans to work on Huawei.

lollipierre commented 6 years ago

@davidgyoung I'm looking at this issue as well on a Huawei.

With your solution, and assuming 2. can be resolved in some way, does this mean that in a worst case scenario it could take up to 30 minutes to detect a beacon on a Huawei device?

davidgyoung commented 6 years ago

Exactly. Not ideal I know.

Fintasys commented 6 years ago

We decided to use 15 minutes and works well so far. So changes in scanning modes should be done in this interval. Btw. App 'package_name' is scanning too frequently also exists on screen on, but it's much less sensitive. I just came across is while debugging.

TracyEminem commented 5 years ago

I think huawei may not blocks scans when the screen is off,as you say this library uses scanfilters to match the BLE advertisement pattern ,i have tried to use scanfilters to scan ,and i keep receiving result for a few hours when screen is off and the phone is not charging.

isaacurbina commented 3 years ago

In my experience, I still get bluetooth scan results up to 1 minute after screen goes off, however, once that time is up, I always get empty list of results for scan. The only workaround I found so far is to send a notification every N minutes to turn the screen on and keep getting results for another minute only for Huawei devices. This would reduce battery life but it's the only think that's worked so far.

mreda7 commented 2 years ago

We have also this problems and the only solution was like @isaacurbina have said is to send a notification every N minutes to turn the screen on.

Do you know if this problem happens on other Android phone manufacturers other than Huawei ?