LifecycleAwareBleConnection

A lifecycle-aware wrapper for BleConnection that automatically manages the BLE connection lifecycle in response to Android lifecycle events.

This class implements DefaultLifecycleObserver to observe the lifecycle of an Android component (such as an Activity or Fragment) and automatically disconnect from the BLE device when the lifecycle owner is stopped, and close the connection when the lifecycle owner is destroyed.

This helps prevent resource leaks and ensures that BLE connections are properly cleaned up when the associated UI component is no longer active.

Lifecycle Behavior

  • onStop: Automatically calls disconnect to gracefully disconnect from the BLE device while keeping resources allocated for potential reconnection.

  • onDestroy: Automatically calls close to fully release all resources and removes this observer from the lifecycle.

Usage Example

class MyActivity : AppCompatActivity() {
private lateinit var connection: LifecycleAwareBleConnection

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val bleConnection = BleConnection(applicationContext, bluetoothDevice)
connection = bleConnection.bindToLifecycle(this)

// Use the connection - it will automatically be managed
connection.connect()

// Observe connection state
lifecycleScope.launch {
connection.connectionStateFlow.collect { state ->
// Handle state changes
}
}
}
// No need to manually disconnect or close - handled automatically
}

Alternative Usage with Extension Function

val connection = BleConnection(context, device).bindToLifecycle(lifecycleOwner)
connection.connect()

Parameters

connection

The underlying BleConnection instance to wrap.

lifecycleOwner

The LifecycleOwner whose lifecycle will control this connection. Typically an Activity, Fragment, or other lifecycle-aware component.

See also

Constructors

Link copied to clipboard
constructor(connection: BleConnection, lifecycleOwner: LifecycleOwner)

Properties

Link copied to clipboard

A StateFlow that emits the current connection state of the BLE device.

Functions

Link copied to clipboard
fun bond(): Flow<BondState>

Initiates the bonding (pairing) process with the BLE device.

Link copied to clipboard
fun close()

Closes the BLE connection and releases all associated resources.

Link copied to clipboard
fun connect()

Initiates a connection to the BLE device.

Link copied to clipboard
fun disableNotifications(serviceUuid: UUID, characteristicUuid: UUID)

Disables notifications for a characteristic.

Link copied to clipboard

Disconnects from the BLE device while keeping resources allocated.

Link copied to clipboard
fun enableNotifications(serviceUuid: UUID, characteristicUuid: UUID, callback: (ByteArray) -> Unit)

Enables notifications for a characteristic and registers a callback for received values.

Link copied to clipboard
open fun onCreate(owner: LifecycleOwner)
Link copied to clipboard
open override fun onDestroy(owner: LifecycleOwner)

Called when the LifecycleOwner is destroyed.

Link copied to clipboard
open fun onPause(owner: LifecycleOwner)
Link copied to clipboard
open fun onResume(owner: LifecycleOwner)
Link copied to clipboard
open fun onStart(owner: LifecycleOwner)
Link copied to clipboard
open override fun onStop(owner: LifecycleOwner)

Called when the LifecycleOwner is stopped.

Link copied to clipboard
suspend fun readCharacteristic(serviceUuid: UUID, characteristicUuid: UUID): ByteArray?

Reads the value of a characteristic from the connected BLE device.

Link copied to clipboard

Returns the underlying BleConnection instance.

Link copied to clipboard
suspend fun writeCharacteristic(serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray): Boolean

Writes a value to a characteristic on the connected BLE device.