Giter VIP home page Giter VIP logo

kmmbeacons's Introduction

KmmBeacons

A Beacon scanner for KMM projects.

Adding a dependency

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")

Basic usage

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()

Customization

Regions

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.

Customize intervals

It possible to customize the intervals of the bluetooth scans. Use setScanPeriod(millis) and setBetweenScanPeriod(millis) to adjust them.

Other libs used

On Android, KmmBeacons uses AltBeacon to perform beacon scanning. On iOS, it uses CLLocationManager.

Licence

Developed at Nextome. Shared under Apache License 2.0.

kmmbeacons's People

Contributors

paolorotolo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

arunabhdas

kmmbeacons's Issues

i using kotlin version 1.9.10 and get this error.

Could not find "co.touchlab:stately-common" in [, konan/klib, konan/kotlin-native-prebuilt-macos-aarch64-1.9.10/klib/common, kotlin-native-prebuilt-macos-aarch64-1.9.10/klib/platform/ios_arm64]

my iOS did not found any beacon, but android is working fine.

compose multiplatform version = 1.5.10
kotlin version = 1.9.10
agp version = 8.1.1

problem:
my iOS did not found any beacon, but android is working fine.

class MainViewModel: ViewModel() {
    private val scanner: KmmBeacons = KmmBeacons()

    private val _state = MutableStateFlow(0)
    val state = _state.asStateFlow()

    init {
        viewModelScope.launch {
            startScanAndObserveResults()
        }
    }

    private suspend fun startScanAndObserveResults() {
        scanner.startScan()
        scanner.observeResults().collect { results ->
            _state.value = results.size
            println("Found ${results.size} beacons:")
            results.forEach {
                println("""
                    UUID: ${it.uuid}
                    Major: ${it.major}
                    Minor: ${it.minor}
                    RSSI: ${it.rssi}
                """.trimIndent())
            }
        }

        scanner.setIosRegions(
            listOf(KScanRegion(uuid = "2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"))
        )

        scanner.setScanPeriod(1000L)
        scanner.setBetweenScanPeriod(250L)
    }
}

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.