removeBond method

Future<void> removeBond({
  1. int timeout = 30,
})

Remove bond (Android Only)

Implementation

Future<void> removeBond({int timeout = 30}) async {
  // check android
  if (Platform.isAndroid == false) {
    throw FlutterBluePlusException(ErrorPlatform.fbp, "removeBond",
        FbpErrorCode.androidOnly.index, "android-only");
  }

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

  try {
    var responseStream = FlutterBluePlus._methodStream.stream
        .where((m) => m.method == "OnBondStateChanged")
        .map((m) => m.arguments)
        .map((args) => BmBondStateResponse.fromMap(args))
        .where((p) => p.remoteId == remoteId)
        .where((p) => p.bondState != BmBondStateEnum.bonding);

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

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

    // only wait for 'unbonded' state if we weren't already unbonded
    if (changed) {
      BmBondStateResponse bs = await futureResponse
          .fbpEnsureAdapterIsOn("removeBond")
          .fbpEnsureDeviceIsConnected(this, "removeBond")
          .fbpTimeout(timeout, "removeBond");

      // success?
      if (bs.bondState != BmBondStateEnum.none) {
        throw FlutterBluePlusException(
            ErrorPlatform.fbp,
            "createBond",
            FbpErrorCode.removeBondFailed.hashCode,
            "Failed to remove bond. ${bs.bondState}");
      }
    }
  } finally {
    mtx.give();
  }
}