BleDataDecoder

fun interface BleDataDecoder<out T>

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

On iOS, BleData wraps NSData from CoreBluetooth with zero-copy. A BleDataDecoder reads directly from the platform-native buffer via indexed access (BleData.get), avoiding the allocation and memcpy that BleData.toByteArray would incur.

val decoder = BleDataDecoder<HeartRate> { data ->
val flags = data[0].toInt()
val bpm = if (flags and 0x01 == 0) data[1].toInt() and 0xFF
else (data[1].toInt() and 0xFF) or ((data[2].toInt() and 0xFF) shl 8)
HeartRate(bpm)
}

Functions

Link copied to clipboard

Bridge a BleDataDecoder to work with ByteArray input.

Link copied to clipboard
abstract fun decode(data: BleData): T
Link copied to clipboard
fun <A, B> BleDataDecoder<A>.map(transform: (A) -> B): BleDataDecoder<B>

Transform the output of this BleData decoder.