- Kotlin 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .github/workflows | ||
| beacon-sdk | ||
| example | ||
| gradle/wrapper | ||
| .gitignore | ||
| build.gradle.kts | ||
| CHANGELOG.md | ||
| CODE_OF_CONDUCT.md | ||
| CODING_STANDARDS.md | ||
| CONTRIBUTING.md | ||
| gradle.properties | ||
| gradlew | ||
| gradlew.bat | ||
| LICENSE.md | ||
| README.md | ||
| RELEASE.md | ||
| settings.gradle.kts | ||
| TERMS_AND_CONDITIONS_OF_USE_DE.pdf | ||
| TERMS_AND_CONDITIONS_OF_USE_EN.pdf | ||
| TERMS_AND_CONDITIONS_OF_USE_FR.pdf | ||
| TERMS_AND_CONDITIONS_OF_USE_IT.pdf | ||
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.