disconnect method

Future<void> disconnect({
  1. int timeout = 35,
  2. bool queue = true,
})

Cancels connection to the Bluetooth Device

  • queue If true, this disconnect request will be executed after all other operations complete. If false, this disconnect request will be executed right now, i.e. skipping to the front of the fbp operation queue, which is useful to cancel an in-progress connection attempt.

Implementation

Future<void> disconnect({int timeout = 35, bool queue = true}) async {
  // Only allow a single disconnect operation at a time
  _Mutex dtx = _MutexFactory.getMutexForKey("disconnect");
  await dtx.take();

  // Only allow a single ble operation to be underway at a time?
  _Mutex mtx = _MutexFactory.getMutexForKey("global");
  if (queue) {
    await mtx.take();
  }

  try {
    // remove from auto connect list if there
    FlutterBluePlus._autoConnect.remove(remoteId);

    var responseStream = FlutterBluePlus._methodStream.stream
        .where((m) => m.method == "OnConnectionStateChanged")
        .map((m) => m.arguments)
        .map((args) => BmConnectionStateResponse.fromMap(args))
        .where((p) => p.remoteId == remoteId)
        .where((p) => p.connectionState == BmConnectionStateEnum.disconnected);

    // Start listening now, before invokeMethod, to ensure we don't miss the response
    Future<BmConnectionStateResponse> futureState = responseStream.first;

    // invoke
    bool changed = await FlutterBluePlus._invokeMethod('disconnect', remoteId.str);

    // only wait for disconnection if weren't already disconnected
    if (changed) {
      await futureState.fbpEnsureAdapterIsOn("disconnect").fbpTimeout(timeout, "disconnect");
    }
  } finally {
    dtx.give();
    if (queue) {
      mtx.give();
    }
  }
}