AFProximity

actual object AFProximity : MonitorNotifier, RangeNotifier

AFProximity is an object responsible for managing Bluetooth beacon scanning and monitoring. It implements MonitorNotifier and RangeNotifier from the AltBeacon library to handle beacon detection events.

This object provides functionalities to start and stop scanning for specified beacons, and emits events through a Flow when beacons enter or exit a region, or when their proximity (ranging) information is updated.

It relies on AFContextProvider to obtain the Android application context and uses BeaconManager from the AltBeacon library for the underlying beacon operations.

Key Features:

  • Beacon Scanning: Initiates and terminates scanning for a list of AFBeacon objects.

  • Event Emission: Publishes AFProximityEvent (Enter, Exit, Heartbeat) via a shared Flow.

  • Bluetooth State Check: Ensures Bluetooth is enabled before starting scans.

  • BeaconManager Integration: Configures and utilizes BeaconManager for monitoring and ranging.

  • Region Management: Creates and manages Region objects based on the provided AFBeacon data.

  • Error Handling: Logs warnings and errors encountered during operations.

  • Coroutine Scope: Uses a CoroutineScope for managing background tasks related to beacon events.

Usage Example:

// Observe beacon events
lifecycleScope.launch {
AFProximity.events.collect { event ->
when (event) {
is AFProximityEvent.Enter -> println("Beacon entered: ${event.beacon.id}")
is AFProximityEvent.Exit -> println("Beacon exited: ${event.beacon.id}")
is AFProximityEvent.Heartbeat -> println("Beacon heartbeat: ${event.beacon.id}, RSSI: ${event.beacon.rssi}")
}
}
}

// Define beacons to scan for
val beaconsToScan = listOf(
AFBeacon(id = "beacon1", uuid = "FDA50693-A4E2-4FB1-AFCF-C6EB07647825", major = 10, minor = 1),
actual object AFProximity

Provides functionality for detecting and interacting with nearby Bluetooth beacons.

This object allows you to:

  • Start scanning for specified beacons.

  • Stop scanning for beacons.

  • Observe proximity events (entering/exiting beacon regions).

Context:

AFProximity simplifies working with Bluetooth Low Energy (BLE) beacons for location-aware features. You define beacons associated with locations (e.g., within a facility). When a device enters or exits a beacon's range, AFProximity triggers events, enabling your app to react (e.g., show relevant info).

Integration with Facility Data:

Beacon information is often part of a larger data model, like a "Facility" containing AFBeacon objects. The typical workflow:

  1. Fetch Facility Data: Get facility details (including beacons) from a backend or local storage.

  2. Extract Beacons: Get the List<AFBeacon> from the facility data.

  3. Start Scanning: Pass this beacon list to AFProximity.startScanning().

  4. Observe Events: Subscribe to AFProximity.events for enter/exit notifications.

Properties

Link copied to clipboard
actual val events: Flow<AFProximityEvent>
actual val events: Flow<AFProximityEvent>

Functions

Link copied to clipboard
open override fun didDetermineStateForRegion(state: Int, region: Region?)
Link copied to clipboard
open override fun didEnterRegion(region: Region?)
Link copied to clipboard
open override fun didExitRegion(region: Region?)
Link copied to clipboard
open override fun didRangeBeaconsInRegion(beacons: Collection<Beacon?>?, region: Region?)
Link copied to clipboard
actual fun startScanning(beacons: List<AFBeacon>)
actual fun startScanning(beacons: List<AFBeacon>)
Link copied to clipboard
actual fun stopScanning()
actual fun stopScanning()