bindToLifecycle

Binds this BleConnection to a LifecycleOwner for automatic lifecycle management.

This extension function creates a LifecycleAwareBleConnection that wraps this connection and automatically manages it based on the provided lifecycle owner's state:

  • When the lifecycle owner is stopped (e.g., Activity goes to background), the connection is automatically disconnected.

  • When the lifecycle owner is destroyed (e.g., Activity is finished), the connection is automatically closed and all resources are released.

Usage Example

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

val connection = BleConnection(applicationContext, device)
.bindToLifecycle(this)

connection.connect()
// Connection will be automatically managed
}
}

Return

A LifecycleAwareBleConnection that wraps this connection with lifecycle awareness.

Parameters

lifecycleOwner

The LifecycleOwner to bind this connection to. Typically an Activity, Fragment, or other lifecycle-aware component.

See also