Package-level declarations

Types

Link copied to clipboard
interface BleCodec<T> : BleEncoder<T> , BleDecoder<T>

Bidirectional codec combining BleEncoder and BleDecoder.

Link copied to clipboard
fun interface BleDataDecoder<out T>

Decodes a BleData into a value of type T without copying.

Link copied to clipboard
fun interface BleDataEncoder<in T>

Encodes a value of type T directly into BleData.

Link copied to clipboard
fun interface BleDecoder<out T>

Decodes a ByteArray into a value of type T.

Link copied to clipboard
fun interface BleEncoder<in T>

Encodes a value of type T into a ByteArray for BLE transmission.

Link copied to clipboard
class CharacteristicNotFoundException(val serviceUuid: Uuid, val characteristicUuid: Uuid) : PeripheralCodecException

Raised when the peripheral is ready to resolve the characteristic and the characteristic is absent from the device's GATT database. Ready to resolve means Peripheral.services is populated and Peripheral.state is a stable State.Connected substate other than State.Connected.ServiceChanged.

Link copied to clipboard
sealed interface DecodedObservation<out T>

Typed equivalent of com.atruedev.kmpble.gatt.Observation.

Link copied to clipboard

Raised when a BleDecoder rejects a value read from a characteristic. The raw bytes are preserved for logging or inspection of the malformed payload, and the underlying decoder exception is preserved as the cause.

Link copied to clipboard
interface Framer

Wraps and recovers payloads on a stream transport (L2CAP CoC, an MTU-spanning concatenation, etc.).

Link copied to clipboard
class FrameTooLargeException(val size: Long, val maxSize: Int) : RuntimeException

Thrown from Unframer.feed when a length prefix exceeds the configured cap. Indicates a malformed stream or hostile peer; the Unframer should not be reused.

Link copied to clipboard

Little-endian signed 16-bit integer codec (-32768..32767). Strict size on decode.

Link copied to clipboard

Little-endian signed 32-bit integer codec. Strict size on decode.

Link copied to clipboard

Single-byte signed integer codec (-128..127). Strict size on decode.

Link copied to clipboard
class LengthPrefixFramer(maxFrameSize: Int = DEFAULT_MAX_FRAME_SIZE) : Framer

Length-prefix framing: each frame is [length: uint32 LE][payload: bytes].

Link copied to clipboard

Sealed root of failures raised by readAs / writeAs / observeAs that originate inside the codec layer (as opposed to the underlying BLE op).

Link copied to clipboard
class PeripheralNotReadyException(val serviceUuid: Uuid, val characteristicUuid: Uuid, val currentState: State) : PeripheralCodecException

Raised when the peripheral is not ready to resolve the characteristic: discovery has not completed, the cached service list is pending invalidation (State.Connected.ServiceChanged), or the peripheral is disconnecting/disconnected. Retry after Peripheral.state reaches State.Connected.Ready, reconnecting first if necessary. The currentState is captured at dispatch time for diagnostics.

Link copied to clipboard

Codec that passes bytes through unchanged. Defensive copies on both sides.

Link copied to clipboard

A typed, framed view over an L2capChannel.

Link copied to clipboard

Little-endian unsigned 16-bit integer codec (0..65535). Strict size on decode.

Link copied to clipboard

Little-endian unsigned 32-bit integer codec (0..4_294_967_295). Strict size on decode.

Link copied to clipboard

Single-byte unsigned integer codec (0..255). Common for level/enum characteristics like Battery Level (0x2A19).

Link copied to clipboard
interface Unframer

Stateful inverse of Framer.frame. Implementations buffer partial frames across feed calls; each call returns zero or more complete payloads.

Link copied to clipboard

UTF-8 string codec. decode throws on malformed UTF-8 byte sequences via ByteArray.decodeToString with throwOnInvalidSequence = true. The exact exception type is platform-dependent (CharacterCodingException on JVM, kotlin.io.CharacterCodingException on Native); treat as a generic decode failure.

Functions

Link copied to clipboard

Bridge a BleDecoder to work with BleData input.

Link copied to clipboard

Bridge a BleEncoder to produce BleData via BleData factory.

Link copied to clipboard

Bridge a BleDataDecoder to work with ByteArray input.

Link copied to clipboard

Bridge a BleDataEncoder to produce ByteArray via BleData.toByteArray.

Link copied to clipboard
fun <A, B> BleCodec<A>.bimap(encode: (B) -> A, decode: (A) -> B): BleCodec<B>

Transform both directions of this codec.

Link copied to clipboard
fun <T> bleCodec(encoder: BleEncoder<T>, decoder: BleDecoder<T>): BleCodec<T>

Combine a standalone BleEncoder and BleDecoder into a BleCodec.

Link copied to clipboard
fun <A, B> BleDataEncoder<B>.contramap(transform: (A) -> B): BleDataEncoder<A>

Transform the input of this BleData encoder.

fun <A, B> BleEncoder<B>.contramap(transform: (A) -> B): BleEncoder<A>

Transform the input of this encoder.

Link copied to clipboard
fun <T> Flow<ByteArray>.decodeFramed(decoder: BleDecoder<T>, framer: Framer = LengthPrefixFramer(), onDecodeFailure: (ByteArray) -> Unit = {}): Flow<T>

Combines unframing and decoding into one operator: byte chunks in, typed values out.

Link copied to clipboard
fun <T> Advertisement.decodeManufacturerData(companyId: Int, decoder: BleDataDecoder<T>): T?

Decode manufacturer-specific data. Zero-copy on iOS.

Link copied to clipboard
fun <T> Advertisement.decodeServiceData(serviceUuid: Uuid, decoder: BleDataDecoder<T>): T?

Decode service-specific data. Zero-copy on iOS.

Link copied to clipboard
inline fun <T, R> DecodedObservation<T>.fold(onValue: (T) -> R, onDisconnected: () -> R): R

Eliminate a DecodedObservation by applying the matching function.

Link copied to clipboard
fun <T> L2capListener.framedConnections(codec: BleCodec<T>, framer: Framer = LengthPrefixFramer(), onDecodeFailure: (ByteArray) -> Unit = {}): Flow<TypedL2capChannel<T>>

Accept incoming L2CAP connections and pre-wrap each one as a TypedL2capChannel using the supplied codec and framer.

Link copied to clipboard
fun <T> L2capChannel.framedIncoming(decoder: BleDecoder<T>, framer: Framer = LengthPrefixFramer(), onDecodeFailure: (ByteArray) -> Unit = {}): Flow<T>

Decode framed payloads from the L2CAP incoming stream.

Link copied to clipboard
fun <T> L2capChannel.incoming(decoder: BleDecoder<T>): Flow<T>

Decode each incoming L2CAP packet into a typed value.

Link copied to clipboard
suspend fun <T> GattServer.indicate(characteristicUuid: Uuid, device: Identifier, value: T, encoder: BleDataEncoder<T>)

Encode a value directly to BleData and send it as an indication.

suspend fun <T> GattServer.indicate(characteristicUuid: Uuid, device: Identifier, value: T, encoder: BleEncoder<T>)

Encode a value and send it as an indication.

Link copied to clipboard
fun <A, B> BleDataDecoder<A>.map(transform: (A) -> B): BleDataDecoder<B>

Transform the output of this BleData decoder.

fun <A, B> BleDecoder<A>.map(transform: (A) -> B): BleDecoder<B>

Transform the output of this decoder.

Link copied to clipboard
suspend fun <T> GattServer.notify(characteristicUuid: Uuid, device: Identifier?, value: T, encoder: BleDataEncoder<T>)

Encode a value directly to BleData and send it as a notification.

suspend fun <T> GattServer.notify(characteristicUuid: Uuid, device: Identifier?, value: T, encoder: BleEncoder<T>)

Encode a value and send it as a notification.

Link copied to clipboard
fun <T> Peripheral.observe(characteristic: Characteristic, decoder: BleDecoder<T>, backpressure: BackpressureStrategy = BackpressureStrategy.Latest): Flow<DecodedObservation<T>>

Observe decoded observations, including disconnect events.

Link copied to clipboard
fun <T> Peripheral.observeAs(serviceUuid: Uuid, characteristicUuid: Uuid, decoder: BleDecoder<T>, backpressure: BackpressureStrategy = BackpressureStrategy.Latest, onDecodeFailure: (ByteArray) -> Unit = {}): Flow<T>

Observes notifications/indications from a characteristic addressed by service+characteristic UUID, decoding each payload with decoder.

Link copied to clipboard
fun <T> Peripheral.observeValues(characteristic: Characteristic, decoder: BleDecoder<T>, backpressure: BackpressureStrategy = BackpressureStrategy.Latest): Flow<T>

Observe decoded values with transparent reconnection.

Link copied to clipboard
suspend fun <T> Peripheral.read(characteristic: Characteristic, decoder: BleDecoder<T>): T

Read a characteristic and decode the result.

Link copied to clipboard
suspend fun <T> Peripheral.readAs(serviceUuid: Uuid, characteristicUuid: Uuid, decoder: BleDecoder<T>): Result<T>

Reads a characteristic by service+characteristic UUID and decodes the value.

Link copied to clipboard
fun Flow<ByteArray>.unframedBy(framer: Framer): Flow<ByteArray>

Adapts a stream of byte chunks (e.g. from an L2CAP CoC incoming flow) into a stream of complete framed payloads.

Link copied to clipboard
suspend fun <T> L2capChannel.write(value: T, encoder: BleEncoder<T>)

Encode a value and write it to the L2CAP channel.

suspend fun <T> Peripheral.write(characteristic: Characteristic, value: T, encoder: BleEncoder<T>, writeType: WriteType = WriteType.WithResponse)

Encode a value and write it to a characteristic.

Link copied to clipboard
suspend fun <T> Peripheral.writeAs(serviceUuid: Uuid, characteristicUuid: Uuid, value: T, encoder: BleEncoder<T>, writeType: WriteType = WriteType.WithResponse): Result<Unit>

Encodes value with encoder and writes it to a characteristic addressed by service+characteristic UUID. Failure cases mirror readAs:

Link copied to clipboard
suspend fun <T> L2capChannel.writeFramed(value: T, encoder: BleEncoder<T>, framer: Framer = LengthPrefixFramer())

Encode a value, frame the bytes, and write the framed packet.