read method
Retrieves the value of a specified descriptor
Implementation
Future<List<int>> read({int timeout = 15}) async {
// check connected
if (device.isDisconnected) {
throw FlutterBluePlusException(
ErrorPlatform.fbp, "readDescriptor", FbpErrorCode.deviceIsDisconnected.index, "device is not connected");
}
// Only allow a single ble operation to be underway at a time
_Mutex mtx = _MutexFactory.getMutexForKey("global");
await mtx.take();
// return value
List<int> readValue = [];
try {
var request = BmReadDescriptorRequest(
remoteId: remoteId,
serviceUuid: serviceUuid,
secondaryServiceUuid: null,
characteristicUuid: characteristicUuid,
descriptorUuid: descriptorUuid,
);
Stream<BmDescriptorData> responseStream = FlutterBluePlus._methodStream.stream
.where((m) => m.method == "OnDescriptorRead")
.map((m) => m.arguments)
.map((args) => BmDescriptorData.fromMap(args))
.where((p) => p.remoteId == request.remoteId)
.where((p) => p.serviceUuid == request.serviceUuid)
.where((p) => p.characteristicUuid == request.characteristicUuid)
.where((p) => p.descriptorUuid == request.descriptorUuid);
// Start listening now, before invokeMethod, to ensure we don't miss the response
Future<BmDescriptorData> futureResponse = responseStream.first;
// invoke
await FlutterBluePlus._invokeMethod('readDescriptor', request.toMap());
// wait for response
BmDescriptorData response = await futureResponse
.fbpEnsureAdapterIsOn("readDescriptor")
.fbpEnsureDeviceIsConnected(device, "readDescriptor")
.fbpTimeout(timeout, "readDescriptor");
// failed?
if (!response.success) {
throw FlutterBluePlusException(_nativeError, "readDescriptor", response.errorCode, response.errorString);
}
readValue = response.value;
} finally {
mtx.give();
}
return readValue;
}