writeCharacteristicResult

suspend fun writeCharacteristicResult(serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray): BleResult<Unit>

Writes a value to a characteristic on the connected BLE device, returning a BleResult.

This is a type-safe alternative to writeCharacteristic that returns detailed error information instead of a boolean. Use this method when you need to distinguish between different failure causes.

Usage Example

val data = "Hello".toByteArray()
connection.writeCharacteristicResult(serviceUuid, charUuid, data)
.onSuccess {
Log.i("BLE", "Write successful")
}
.onFailure { error ->
when (error) {
is BleCharacteristicException -> {
Log.e("BLE", "Write failed with GATT status: ${error.gattStatus}")
}
else -> Log.e("BLE", "Error: ${error.message}")
}
}

Return

A BleResult containing either: - BleResult.Success with Unit indicating successful write - BleResult.Failure with a BleCharacteristicException

Parameters

serviceUuid

The UUID of the GATT service containing the characteristic.

characteristicUuid

The UUID of the characteristic to write to.

value

The ByteArray value to write to the characteristic.

See also