Device Fragment
Fragment demonstrating BLE device connection and operations with BLE-X library.
This fragment shows how to:
Connect to a BLE device with lifecycle awareness
Monitor connection state changes
Perform GATT operations (read, write, notifications)
Handle device bonding/pairing
Properly manage resources and cleanup
Architecture Pattern
This fragment follows the recommended pattern for BLE operations:
Lifecycle-Aware Connection: Uses LifecycleAwareBleConnection which automatically disconnects when the fragment stops and closes when destroyed.
State Observation: Observes ConnectionState flow to update UI accordingly.
Suspend Functions: Uses Kotlin coroutines for read/write operations.
Usage
// Navigate to this fragment with a device address:
val fragment = DeviceFragment.newInstance("AA:BB:CC:DD:EE:FF")
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit()Lifecycle Handling
The fragment automatically handles:
onViewCreated: Initiates connection
onStop: Connection is automatically disconnected (via LifecycleAwareBleConnection)
onDestroyView: Connection is automatically closed (via LifecycleAwareBleConnection)