AltBeacon / android-beacon-library

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

Xiaomi: Library Stops Scanning after a while with power saving mode active on a mobile phone #643

Open Kitsopappas opened 6 years ago

Kitsopappas commented 6 years ago

I have an application written in Ionic/Cordova and I need to range for beacons when the app is in the background.

Expected behavior

I scan for beacons in the background every 13 seconds iBeaconManager.setBackgroundScanPeriod(13000); and the library should not stop returning results. In the foreground the ranging does not stop.

Actual behavior

After a short period of time (aprox 1min), whilst the app is in the background, the ranging stops returning beacon results and returns an empty array instead. The ranging will not return any results even if I move the app to the foreground or restart the ranging. It will only start showing results again if we restart the mobile's bluetooth.

Steps to reproduce this behavior

0) Check if there is a power saving setting enabled for the application 1) Start scanning for ibeacons 2) Move application to the background 3) See ibeacon results returning an empty array after a while (Even if there are beacons around).

If we disable the power saving setting and restart the bluetooth then the app will return results of ibeacons nearby indefinitely.

Mobile device model and OS version

Xiaomi Note 5A Prime (Android Version 7.1.2) Xiaomi RedMi Note 4x (Android Version 7.0.0)

Android Beacon Library version

2.12.4-android8_1

Ionic Ibeacon Library

https://github.com/petermetz/cordova-plugin-ibeacon

Sample Code

        iBeaconManager = BeaconManager.getInstanceForApplication(cordovaActivity);
        iBeaconManager.setForegroundBetweenScanPeriod(foregroundBetweenScanPeriod);
        iBeaconManager.setForegroundScanPeriod(foregroundScanPeriod);
        iBeaconManager.setAndroidLScanningDisabled(true);
        iBeaconManager.setBackgroundBetweenScanPeriod(01);
        iBeaconManager.setBackgroundScanPeriod(13001);
        try {
            iBeaconManager.updateScanPeriods();
        } catch (Exception e) {
        }

Is there any way to avoid this/work around? Or should I inform the users using this application to dissable the power saving mode on their mobile phones?

davidgyoung commented 6 years ago

I don't know how power saving works on Xiaomi devices, although it sounds like a proprietary enhancement as the functionality does match any known behavior on stock Android AOSP 7.x. It is always very hard to come up with solutions to proprietary Android changes as they are closed source and tend to be undocumented.

Two possible workarounds:

It may also be useful to capture system level log messages when the scan results are blocked to see if they offer any clue as to what Xioami is doing.