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,
primaryServiceUuid: primaryServiceUuid,
serviceUuid: serviceUuid,
characteristicUuid: characteristicUuid,
instanceId: instanceId,
descriptorUuid: descriptorUuid,
);
Stream<BmDescriptorData> responseStream = FlutterBluePlusPlatform.instance.onDescriptorRead
.where((p) => p.remoteId == request.remoteId)
.where((p) => p.primaryServiceUuid == request.primaryServiceUuid)
.where((p) => p.serviceUuid == request.serviceUuid)
.where((p) => p.characteristicUuid == request.characteristicUuid)
.where((p) => p.instanceId == instanceId)
.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._invokePlatform(() => FlutterBluePlusPlatform.instance.readDescriptor(request));
// 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;
}