ClientBleGatt

class ClientBleGatt(gatt: GattClientAPI, scope: CoroutineScope, mutex: MutexWrapper, bufferSize: Int)

A class for managing BLE connection. It propagates events (ClientGattEvent) to it's corresponding characteristics (ClientBleGattCharacteristic) and descriptors (ClientBleGattDescriptor). Thanks to that values are getting updated.

Despite that it's responsible for exposing connection parameters like mtu, phy, connection state and request their changes.

Constructors

Link copied to clipboard
constructor(gatt: GattClientAPI, scope: CoroutineScope, mutex: MutexWrapper, bufferSize: Int)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val bondState: StateFlow<BondState?>

Returns bond state of the server device.

Link copied to clipboard

Returns last GattConnectionState without it's status.

Link copied to clipboard

Returns last observed GattConnectionState with it's corresponding status BleGattConnectionStatus.

Link copied to clipboard

Returns whether a device is connected.

Link copied to clipboard
@IntRange(from = 23, to = 517)
val mtu: StateFlow<Int>

Established MTU size. There are incoming changes on Android 14 where this value is gonna to be always max value - 517 and wouldn't be able to change.

Link copied to clipboard

Returns Flow which emits services. Services can be outdated which results in emitting ServiceChanged. That's why usage of Flow may be handy.

Functions

Link copied to clipboard

Aborts reliable write. All writes to a characteristics which supports reliable writes will be reverted to a state preceding call to beginReliableWrite.

Link copied to clipboard

Begins reliable write. All writes to a characteristics which supports this feature will be transactional which means that they can be reverted in case of data inconsistency.

Link copied to clipboard

Clears service cache.

Link copied to clipboard
fun close()

Closes GATT instance and releases resources. After that ClientBleGatt cannot be used.

Link copied to clipboard

Disconnects current device.

Link copied to clipboard
Link copied to clipboard

Executes reliable write. All writes to a characteristics which supports reliable write will be executed and new values will be set permanently.

Link copied to clipboard
suspend fun readPhy(): PhyInfo

Reads preferred PHY for the connection.

Link copied to clipboard
suspend fun readRssi(): Int

Suspend function reading server device's RSSI.

Link copied to clipboard
fun reconnect()

Reconnects to the device if disconnected. Works only if BleGattConnectOptions.closeOnDisconnect is set to false.

Link copied to clipboard

Requests connection priority. It will influence latency and power consumption.

Link copied to clipboard
suspend fun requestMtu(mtu: Int): Int

Suspend function requesting new MTU size.

Link copied to clipboard
suspend fun setPhy(txPhy: BleGattPhy, rxPhy: BleGattPhy, phyOption: PhyOption): PhyInfo

Sets preferred PHY for the connection.

Link copied to clipboard
suspend fun waitForBonding(timeInMillis: Long = 2000)

Auxiliary function which waits for bonding. The bonding may be initiated in different scenarios e.g. after connected or when reading from characteristic which is protected.