You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, we use a global mutex to do a single operation at a time in FBP. i.e. _Mutex mtx = _MutexFactory.getMutexForKey("global")
It would make sense for all functions to switch to a device mutex. i.e. _Mutex mtx = _MutexFactory.getMutexForKey(remoteId), to allow writes to multiple devices at the same time.
(Or potentially use a command queue with an explicit list of pending operations, similar to UniversalBle)
Current Code:
/// Writes a characteristic.
Future<void> write(List<int> value,
{bool withoutResponse = false, bool allowLongWrite = false, int timeout = 15}) async {
// check args
if (withoutResponse && allowLongWrite) {
throw ArgumentError("cannot longWrite withoutResponse, not allowed on iOS or Android");
}
// check connected
if (device.isDisconnected) {
throw FlutterBluePlusException(
ErrorPlatform.fbp, "writeCharacteristic", 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 {
final writeType = withoutResponse ? BmWriteType.withoutResponse : BmWriteType.withResponse;
var request = BmWriteCharacteristicRequest(
remoteId: remoteId,
characteristicUuid: characteristicUuid,
serviceUuid: serviceUuid,
writeType: writeType,
allowLongWrite: allowLongWrite,
value: value,
primaryServiceUuid: primaryServiceUuid,
);
var responseStream = FlutterBluePlusPlatform.instance.onCharacteristicWritten
.where((p) => p.remoteId == request.remoteId)
.where((p) => p.serviceUuid == request.serviceUuid)
.where((p) => p.characteristicUuid == request.characteristicUuid)
.where((p) => p.primaryServiceUuid == request.primaryServiceUuid);
// Start listening now, before invokeMethod, to ensure we don't miss the response
Future<BmCharacteristicData> futureResponse = responseStream.first;
// invoke
await FlutterBluePlus._invokeMethod(() => FlutterBluePlusPlatform.instance.writeCharacteristic(request));
// wait for response so that we can:
// 1. check for success (writeWithResponse)
// 2. wait until the packet has been sent, to prevent iOS & Android dropping packets (writeWithoutResponse)
BmCharacteristicData response = await futureResponse
.fbpEnsureAdapterIsOn("writeCharacteristic")
.fbpEnsureDeviceIsConnected(device, "writeCharacteristic")
.fbpTimeout(timeout, "writeCharacteristic");
// failed?
if (!response.success) {
throw FlutterBluePlusException(_nativeError, "writeCharacteristic", response.errorCode, response.errorString);
}
return Future.value();
} finally {
mtx.give();
}
}
The text was updated successfully, but these errors were encountered:
chipweinberger
changed the title
[Feature]: Support writing to multiple devices at the same time
[Feature]: Write to multiple devices at the same time
Jan 17, 2025
chipweinberger
changed the title
[Feature]: Write to multiple devices at the same time
[Feature]: Connect & Write to multiple devices at the same time
Jan 22, 2025
chipweinberger
changed the title
[Feature]: Connect & Write to multiple devices at the same time
[Feature]: Communicate with multiple devices at the same time
Jan 22, 2025
FlutterBluePlus Version
1.35.0
What is your feature request?
Currently, we use a global mutex to do a single operation at a time in FBP. i.e.
_Mutex mtx = _MutexFactory.getMutexForKey("global")
It would make sense for all functions to switch to a device mutex. i.e.
_Mutex mtx = _MutexFactory.getMutexForKey(remoteId)
, to allow writes to multiple devices at the same time.(Or potentially use a command queue with an explicit list of pending operations, similar to UniversalBle)
Current Code:
The text was updated successfully, but these errors were encountered: