readCharacteristic method
Future<Uint8List?>
readCharacteristic({
- required String serviceUuid,
- required String characteristicUuid,
- Duration timeout = const Duration(seconds: 30),
override
readCharacteristic reads the value of a BLE characteristic. The return value is the raw bytes of the characteristic.
If the characteristic is not readable, this method will return null.
Implementation
@override
Future<Uint8List?> readCharacteristic({
required String serviceUuid,
required String characteristicUuid,
Duration timeout = const Duration(seconds: 30),
}) async {
if (_currentConnected == null) {
log("No device connected");
return null;
}
final services = await _currentConnected!.discoverServices();
final service = services.firstWhereOrNull((s) => s.uuid.toLowerCase() == serviceUuid.toLowerCase());
if (service == null) {
log("Service not found: $serviceUuid");
return null;
}
if (_notifications.containsKey(characteristicUuid)) {
log("Stop notify before reading");
return null;
}
try {
final characteristic = await service.getCharacteristic(characteristicUuid);
final value = await characteristic.readValue(timeout: timeout);
return value.buffer.asUint8List();
} catch (e) {
log("Error getting characteristic: $e");
return null;
}
}