A Beacon scanner for KMM projects.
Include KmmBeacons in your project as a dependency from GitHub Packages.
In root build.gradle
add:
repositories {
[...]
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/Nextome/KmmBeacons")
credentials {
username = GITHUB_USERNAME
password = GITHUB_TOKEN
}
}
}
In your module build.gradle
add:
implementation("com.nextome.kmmbeacons:kmmbeacons:$latestVersion")
Get a scanner instance with:
val scanner = KmmBeacons()
Then start scanning:
scanner.startScan()
scanner.observeResults().collect { results ->
println("Found ${results.size} beacons:")
results.forEach {
println("""
UUID: ${it.uuid}
Major: ${it.major}
Minor: ${it.minor}
RSSI: ${it.rssi}
""".trimIndent())
}
}
Finally, stop scanning with:
scanner.stopScan()
On Android, KmmBeacons will automatically detect all available beacons. If you want to filter for region, you can use:
scanner.setAndroidRegions(
listOf(KScanRegion(uuid = "B39A8A15-1A82-4D24-A311-EBF122BA6AF9"))
)
On iOS, it is mandatory to provide at least one region. If no regions are specified, KmmBeacons will not detect beacons.
scanner.setIosRegions(
listOf(KScanRegion(uuid = "B39A8A15-1A82-4D24-A311-EBF122BA6AF9"))
)
Eventually, you can set a region on iOS only. In this way, Android will detect all beacons while iOS will return all the beacons in the given regions.
It possible to customize the intervals of the bluetooth scans. Use setScanPeriod(millis)
and setBetweenScanPeriod(millis)
to adjust them.
On Android, KmmBeacons uses AltBeacon to perform beacon scanning. On iOS, it uses CLLocationManager.
Developed at Nextome. Shared under Apache License 2.0.