Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Communicate with multiple devices at the same time #1105

Open
chipweinberger opened this issue Jan 17, 2025 · 0 comments
Open

[Feature]: Communicate with multiple devices at the same time #1105

chipweinberger opened this issue Jan 17, 2025 · 0 comments
Labels
feature Feature request

Comments

@chipweinberger
Copy link
Owner

chipweinberger commented Jan 17, 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:

  /// 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();
    }
  }
@chipweinberger chipweinberger added the feature Feature request label Jan 17, 2025
@chipweinberger 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 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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature request
Projects
None yet
Development

No branches or pull requests

1 participant