removeBond method
Remove bond (Android Only)
Implementation
Future<void> removeBond({int timeout = 30}) async {
// check android
if (kIsWeb || !Platform.isAndroid) {
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 = FlutterBluePlusPlatform.instance.onBondStateChanged
.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(() => FlutterBluePlusPlatform.instance.removeBond(BmRemoveBondRequest(remoteId: remoteId)));
// only wait for 'unbonded' state if we weren't already unbonded
if (changed) {
BmBondStateResponse bs = await futureResponse
.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();
}
}