Skip to content

Background Beacon Detection

Suhan Gui edited this page Mar 17, 2023 · 2 revisions

Official documentation for the library can be found here: https://altbeacon.github.io/android-beacon-library/documentation.html

Permissions

In order for Android to support background beacon detection, the user must enable permissions for ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION. Users must select "Allow all the time" on the location tracking pop up. For Android 11+/SDK 30+, the user must manually enable "Allow all the time" in the app settings page. For Android 12 and higher (SDK 31+), BLUETOOTH_SCAN and BLUETOOTH_CONNECT permissions must also be enabled.

In the case where the appropriate permissions are not given, the app does not detect beacons but no other errors arise from this.

How It Works

We are using beacon ranging to perform periodic scans for detectable beacons. It is currently restricted to iBeacons (beacon layout: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24) with the UUID 3bb7876d-403d-cb84-5e4c-907adc953f9c. This configuration can be easily changed to support other beacons.

When we first detect a beacon, we find the major (id2) which is the bus number. We then proceed to board the bus. In the case where multiple beacons are detected, the app boards the bus on the top of the list of beacons provided. We continue to scan for beacons. After a while of not detecting any beacons, the app proceeds to leave the bus. Caveat: The app is not guaranteed to detect the beacon every scan but is consistent enough to detect a beacon very quickly.

Beacon detection is functional in the background when the app is not on. We are required to show some sort of notification though this requirement appears to be from the library and not Android. Background detection is functional if the user chooses to dismiss the notification and even in the scenario where the user chooses to disable notifications.

Changeable Settings for Beacon Ranging

There are things that we may want to change in the future to optimize for what we want. Currently we have backgroundBetweenScanPeriod set at 10 seconds and backgroundScanPeriod set at 1.1 seconds. We can reduce backgroundBetweenScanPeriod for more frequent scans but it comes at the cost of more battery usage. We can also reduce backgroundScanPeriod to save on battery but the author of the library recommends 1.1 seconds to be the minimum as many beacons only transmit at a frequency of 1 hz. We have it set as 30 seconds from last beacon detection before we leave a bus if we are currently on a bus.

Clone this wiki locally