readCharacteristicResult

suspend fun readCharacteristicResult(serviceUuid: UUID, characteristicUuid: UUID): BleResult<ByteArray>

Reads the value of a characteristic from the connected BLE device, returning a BleResult.

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

Usage Example

connection.readCharacteristicResult(serviceUuid, charUuid)
.onSuccess { data ->
val text = String(data, Charsets.UTF_8)
Log.i("BLE", "Received: $text")
}
.onFailure { error ->
when (error) {
is BleNotFoundException -> Log.e("BLE", "Characteristic not found")
is BleCharacteristicException -> Log.e("BLE", "Read failed: ${error.gattStatus}")
else -> Log.e("BLE", "Error: ${error.message}")
}
}

Return

A BleResult containing either: - BleResult.Success with the characteristic value as a ByteArray - BleResult.Failure with a BleCharacteristicException or BleNotFoundException

Parameters

serviceUuid

The UUID of the GATT service containing the characteristic.

characteristicUuid

The UUID of the characteristic to read.

See also