AFProximity
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
AFBeaconobjects.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
BeaconManagerfor monitoring and ranging.Region Management: Creates and manages
Regionobjects based on the providedAFBeacondata.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),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:
Fetch Facility Data: Get facility details (including beacons) from a backend or local storage.
Extract Beacons: Get the
List<AFBeacon>from the facility data.Start Scanning: Pass this beacon list to
AFProximity.startScanning().Observe Events: Subscribe to
AFProximity.eventsfor enter/exit notifications.