Read-only mirror of https://github.com/SchweizerischeBundesbahnen/beacon-sdk-android — SBB. Issues & pull requests at the source.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-18 14:41:09 +01:00
.github/workflows Setup build workflow 2024-05-30 18:04:13 +02:00
beacon-sdk Update version to 0.4.1 2026-11-17 11:56:24 +01:00
example Add initial version of the SDK and example app code 2024-05-30 17:16:20 +02:00
gradle/wrapper Add initial version of the SDK and example app code 2024-05-30 17:16:20 +02:00
.gitignore Add .gitignore 2024-05-30 16:15:37 +02:00
build.gradle.kts Add initial version of the SDK and example app code 2024-05-30 17:16:20 +02:00
CHANGELOG.md Update version to 0.4.1 2026-11-17 11:56:24 +01:00
CODE_OF_CONDUCT.md Initial commit 2024-05-03 11:19:35 +02:00
CODING_STANDARDS.md Define base coding standards 2024-05-30 16:41:35 +02:00
CONTRIBUTING.md Define initial contributing guidelines 2024-05-30 16:23:46 +02:00
gradle.properties Add initial version of the SDK and example app code 2024-05-30 17:16:20 +02:00
gradlew Add initial version of the SDK and example app code 2024-05-30 17:16:20 +02:00
gradlew.bat Add initial version of the SDK and example app code 2024-05-30 17:16:20 +02:00
LICENSE.md Fix header 2024-05-30 16:44:58 +02:00
README.md Update version to 0.4.1 2026-11-17 11:56:24 +01:00
RELEASE.md Describe release procedures 2024-05-30 16:51:48 +02:00
settings.gradle.kts Add initial version of the SDK and example app code 2024-05-30 17:16:20 +02:00
TERMS_AND_CONDITIONS_OF_USE_DE.pdf Update terms and conditions of use 2026-11-18 14:41:09 +01:00
TERMS_AND_CONDITIONS_OF_USE_EN.pdf Update terms and conditions of use 2026-11-18 14:41:09 +01:00
TERMS_AND_CONDITIONS_OF_USE_FR.pdf Update terms and conditions of use 2026-11-18 14:41:09 +01:00
TERMS_AND_CONDITIONS_OF_USE_IT.pdf Update terms and conditions of use 2026-11-18 14:41:09 +01:00

Beacon SDK 0.4.1

An Android library providing APIs to interact with beacons.

Preconditions

To use all features of this SDK, a client app must be registered on the SBB API platform.

Setup

To add the Beacon SDK to your app you must add the JitPack repository to your apps repositories. Add it in your root build.gradle.kts at the end of repositories:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

Add the Beacon SDK dependency to your app/build.gradle.kts:

dependencies {
    implementation 'com.github.SchweizerischeBundesbahnen:beacon-sdk-android:{latest-release}'
}

Permissions

Android requires that you request permission from the user at runtime in order to scan for beacons. The specific runtime permissions you must request depend on the Android SDK version you are targeting and the version of Android on which your app runs.

Add the following permissions to your app's manifest file::

<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

To detect beacons in the background, you must also add:

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

Getting started

Create a new BeaconSDK instance.

val sdk = BeaconSDK(
    clientId = "<client_id>",
    clientSecret = "<client_secret>",
    appId = BuildConfig.APPLICATION_ID,
    installationId = "<installation_id>",
)

Here the <client_id> and <client_secret> should be replaced by the values of the client app that you registered on the SBB API platform. The <installation_id> is a globally unique identifier for an app installation. Learn about the latest best practices for unique identifiers here or use the implementation in the example app as a guide.

Scan beacons

Use the BeaconSDK instance to create a new beacon scanner. All parameters except context are optional.

val scanner = sdk.createBeaconScanner(
    context = context,
    beaconTypes = BeaconType.entries.toSet(),
    foregroundScanPeriod = 1100.milliseconds,
    foregroundBetweenScanPeriod = 0.seconds,
    backgroundScanPeriod = 1100.milliseconds,
    backgroundBetweenScanPeriod = 15.minutes,
    monitoringRegionId = "MONITORING-REGION-${UUID.randomUUID()}",
    rangingRegionId = "RANGING-REGION-${UUID.randomUUID()}",
    foregroundServiceNotification = null,
    foregroundServiceNotificationId = 5634,
)

Invoke the scanner.start() method to start scanning. Scan results are emitted using the scanner.scanResults flow. To stop scanning invoke the scanner.stop() method.

Background scanning

Background scanning behaves differently depending on the android version. See the blog post here for a detailed description of the limits.

Using a Foreground Service

The BeaconSDK may be configured to use a foreground service to scan for beacons. A foreground service shows a persistent notification while beacon scanning is running so users know scanning is taking place. To use a foreground service set the foregroundServiceNotification parameter to a not null value and optionally set a custom foregroundServiceNotificationId when creating a beacon scanner.

You should also change the default background scan period and the time between scans. The scan period should be at least 1.1 seconds because many beacons transmit at a frequency of 1 Hz. The time between scans depends on your use case. Generally speaking, a larger value means a slower detection of beacons and a smaller value increases battery consumption. If the value is set to 0, scanning is constant. If scanning is constant you can decrease the scan period to get scan results faster.

The example app has code that shows how to use a foreground service.

Resolve scan results

If an app requires not only beacons but also associated information, the beacon resolver can be used to obtain this data. Use the BeaconSDK instance to create a new beacon resolver. All parameters are optional.

val resolver = sdk.createBeaconResolver(
    beaconInfoCache = InMemoryBeaconInfoCache(),
)

Assume that you have a list of scan results, and you want to get associated data such as the vehicles in which the scanned beacons are installed. Call resolve() to get that information:

val scanResults = ...
val beaconInfos = resolver.resolve(scanResults)

License

This project is licensed under MIT.