Search code examples
androidbluetooth-lowenergybeaconaltbeaconexposure

How to setup altbeacon to prevent stoping scan when Android dozes


I have an Android app that implements an foreground service, uses a partial wakelock and has the permission to empty the battery by using REQUEST_IGNORE_BATTERY_OPTIMIZATIONS as explained here: https://developer.android.com/training/monitoring-device-state/doze-standby

That all works and the service runs forever without ever being put to doze.

My service uses ranging of beacons and permanently reports the distances. With altbeacon it nicely works. However, when the screen is switched off and after approx. 1 to 5 min, the Android power management somehow kicks in and the altbeacon library calls the 'RangeNotifier' callback always with an empty beacon list. Once that happens, beacon list is always empty, even if I wake the phone up again.

This is how I configure it:

beaconManager = BeaconManager.getInstanceForApplication(this)
beaconManager.setEnableScheduledScanJobs(false)
beaconManager.isRegionStatePersistenceEnabled = false
beaconManager.foregroundBetweenScanPeriod = 0
beaconManager.foregroundScanPeriod = 1100

It somehow looks like the means I took to successfully evade the DOZE for my service are not extended to the Altbeacon service. How can I change this, and let altbeacon scan forever?

Edit: The relevant section in the Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hollyhook.oscHook">

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

    <!-- for beacons -->
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature android:name="android.hardware.sensor.accelerometer"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity>
            ... removed for brevity
        </activity>

        <service
            android:name=".OscHookService"
            android:enabled="true"
            android:exported="true">
         </service>

    </application>
</manifest> 

I'm testing with an Android 10 Samsung S10. I can provide an Android log with BeaconManager.setDebug(true) if it helps (it is long and noisy)

Much thanks for any help!


Solution

  • I found the solution. As explained here: https://altbeacon.github.io/android-beacon-library/requesting_permission.html

    Since target SDK 29+ accessing beacons in the background needs this permission declared in the manifest:

    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />