write method
Writes the value of a descriptor
Implementation
Future<void> write(List<int> value, {int timeout = 15}) async {
// check connected
if (device.isDisconnected) {
throw FlutterBluePlusException(
ErrorPlatform.fbp, "writeDescriptor", 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();
try {
var request = BmWriteDescriptorRequest(
remoteId: remoteId,
serviceUuid: serviceUuid,
secondaryServiceUuid: null,
characteristicUuid: characteristicUuid,
descriptorUuid: descriptorUuid,
value: value,
);
Stream<BmDescriptorData> responseStream = FlutterBluePlus._methodStream.stream
.where((m) => m.method == "OnDescriptorWritten")
.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('writeDescriptor', request.toMap());
// wait for response
BmDescriptorData response = await futureResponse
.fbpEnsureAdapterIsOn("writeDescriptor")
.fbpEnsureDeviceIsConnected(device, "writeDescriptor")
.fbpTimeout(timeout, "writeDescriptor");
// failed?
if (!response.success) {
throw FlutterBluePlusException(_nativeError, "writeDescriptor", response.errorCode, response.errorString);
}
} finally {
mtx.give();
}
return Future.value();
}