GattConnection

interface GattConnection

One device's GATT surface. Every fallible op suspends until the stack acknowledges it (suspend-ack model). Characteristic identifiers are the UUID strings from GattUuids.

Attribute ops (read, readTimed, write, setNotify) and cancellation: an op called from a cancelled coroutine throws kotlinx.coroutines.CancellationException without reaching the device, and an op already issued is never abandoned — it finishes (answered, failed, or timed out by the implementation's own bound) before its caller's cancellation is delivered. Abandoning one in flight leaves the platform's GATT client busy, so the ops after it fail. Teardown writes from a cancelled coroutine therefore need NonCancellable.

Properties

Link copied to clipboard
abstract val connected: StateFlow<Boolean>

True while the link is up; flips to false on disconnect (drop or local close).

Link copied to clipboard
abstract val id: DeviceId
Link copied to clipboard
abstract val link: StateFlow<LinkInfo>

The current negotiated BLE link parameters (MTU, PHY, connection interval/latency/timeout), updated as the stack reports them. Implementations feed it from the granted MTU and the platform PHY / connection-parameter callbacks; the fake drives it directly in tests.

Functions

Link copied to clipboard
abstract suspend fun disconnect()
Link copied to clipboard
abstract suspend fun discoverServices()
Link copied to clipboard
abstract fun has(characteristic: String): Boolean

Whether characteristic was discovered on this device. Optional services (device info, firmware update) vary by model/firmware; callers gate on this instead of catching reads.

Link copied to clipboard
abstract fun observe(characteristic: String): Flow<ByteArray>

Stream of notification payloads for characteristic.

Link copied to clipboard
abstract suspend fun read(characteristic: String): ByteArray
Link copied to clipboard
open suspend fun readTimed(characteristic: String): TimedRead

read with the host clock stamped around the radio exchange, for clock synchronization (the time-sync characteristic, see TimeSync): TimedRead.sentNs when the request leaves for the device, TimedRead.receivedNs when its reply arrives, both System.nanoTime().

Link copied to clipboard
open suspend fun requestHighThroughput()

Best-effort link-throughput tuning (connection priority / preferred PHY). High-rate models (MAXI: 32 ch, ~14 kB/s) sit near the default connection parameters' budget; this adds headroom for retransmissions. Implementations MUST swallow unsupported-stack failures; default is a no-op.

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

Request an MTU; returns the granted MTU.

Link copied to clipboard
abstract suspend fun setNotify(characteristic: String, enable: Boolean)

Enable/disable notifications (writes the CCCD) for characteristic.

Link copied to clipboard
abstract suspend fun write(characteristic: String, value: ByteArray)