Search code examples
androidaltbeacon

Altbeacon monitoring won't work when app is in background and foreground service is running in Android 10+


I'm using Altbeacon library to detect when my app is inside or outside a given region.

The problem I'm having is that when the app goes to the background the didExitRegion method will be called even with the phone next to the beacon and will only come back and detect it when I put the app in foreground.

This is the configuration I'm using before binding the beacon manager

            iBeaconManager.enableForegroundServiceScanning(
                BeaconNotificationManager.createNotification(
                    context
                ), BeaconNotificationManager.notificationId
            )
            iBeaconManager.setEnableScheduledScanJobs(false)
            iBeaconManager.backgroundBetweenScanPeriod = 1000
            iBeaconManager.backgroundScanPeriod = 1500
            iBeaconManager.foregroundBetweenScanPeriod = 1000
            iBeaconManager.foregroundScanPeriod = 1500

I noticed that when then backgroundBetweenScanPeriod is 0 this problem is not reproduced.

Finally this problem also stops when the app allows background location permission but if I understand correctly that should not be necessary since I'm enabling the foreground service scanning.

Thanks in advance


Solution

  • You must have background location permission to detect beacons in the background. This is true regardless of whether you have a foreground service.

    Android’s naming of the term “ForegroundService” is very misleading. Having one does not change the operating system’s perception if your app to be “in the foreground” for permissions enforcement purposes. Your app is still in the background when there is no visible activity on the screen. See here.

    The foreground service indicator in the status bar simply allows us he service to run for extended periods of time on Android 8+, whereas apps without them are limited to 10 minutes of background time.