Skip to content

Detecting BLE beacons in the background

Michał Gasztold edited this page Aug 20, 2021 · 2 revisions

On latest Android versions background operation of services and background access to location permission is restricted.

First of all the application needs two additional permissions in the manifest:

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

Note that the app must ask the user to grant ACCESS_BACKGROUND_LOCATION permission. It is not required for the FOREGROUND_SERVICE permission.

To have the SDK be able to scan beacons in the background the following call must be made before calling mBeaconManager.start().

mBeaconManager.setForegroundServiceNotification(BackgroundNotification.getForegroundServiceNotification(applicationContext), NOTIFICATION_ID_INT)

Example implementation of a helper class BackgroundNotification can be found here: https://github.com/MGasztold/ubudu_contextual_interactions_sample/blob/main/app/src/main/java/com/ubudu/contextualsample/BackgroundNotification.kt

References:

https://developer.android.com/guide/components/foreground-services

https://developer.android.com/training/location/background

Clone this wiki locally