waitForValueChange

abstract suspend fun waitForValueChange(rawDataFilter: (ByteArray) -> Boolean = { true }, merge: suspend (accumulator: ByteArray, received: ByteArray, index: Int) -> MergeResult = { _, rec, _ -> MergeResult.Completed(rec) }, filter: (ByteArray) -> Boolean = { true }, trigger: suspend RemoteCharacteristic.() -> Unit = {}): ByteArray

Waits for the value of the characteristic to change.

This method suspends until the value of the characteristic changes. If the notifications or indications are not enabled, this method will enable them automatically after subscribing for value changes.

val newValue = remoteCharacteristic.waitForValueChange(
trigger = {
// Write to the characteristic to request new data.
write(byteArrayOf(0x01))
// Note: You may also write to other characteristics or descriptors here:
// anotherCharacteristic.write(byteArrayOf(0x02))
},
rawDataFilter = { data ->
// Accept only packets starting with 0xAA.
data.isNotEmpty() && data[0] == 0xAA.toByte()
},
merge = { accumulator, received, index ->
// Assume the first byte indicates the total length of the message.
val expectedLength = if (index == 0) received[1].toInt() else accumulator[1].toInt()
val newAccumulator = accumulator + received
if (newAccumulator.size - 2 < expectedLength) {
// More data expected.
MergeResult.Accumulate(newAccumulator)
} else {
// Full message received.
MergeResult.Completed(newAccumulator.drop(2)) // Drop the header.
}
},
filter = { merged ->
// Accept only messages with a valid checksum.
val checksum = merged.last()
val calculated = merged.dropLast(1).fold(0.toByte()) { acc, byte -> acc + byte }
checksum == calculated
}
)
var response = parse(newValue)

Return

The received value of the characteristic.

Parameters

rawDataFilter

An optional filter function to evaluate the received notification or indication. Accepted packets will be sent to merge merge operator for further processing. By default, all received data is accepted.

merge

Ao optional merge function to combine multiple received values into one. This method can accumulate received values by returning MergeResult.Accumulate until a complete message is formed, which is then returned as MergeResult.Completed. By default, each received value is treated as a complete message.

filter

An optional filter function to evaluate the merged value. When the function returns true, the value is returned from this method.

trigger

An optional trigger that will be executed once before waiting for the value change. It can be used to perform an action that may cause the peripheral to send a notification or indication, e.g., writing to the characteristic to request data. The trigger is executed in the context of the RemoteCharacteristic, so its methods and properties can be accessed directly.

See also

Throws

if the operation failed.

if the characteristic has been invalidated due to disconnection or service change event.

if the implementation fails, see BluetoothException.cause for a reason.