bondDeviceAtAddress method
Starts outgoing bonding (pairing) with device with given address. Returns true if bonded, false if canceled or failed gracefully.
pin
or passkeyConfirm
could be used to automate the bonding process,
using provided pin or confirmation if necessary. Can be used only if no
pairing request handler is already registered.
Note: passkeyConfirm
will probably not work, since 3rd party apps cannot
get BLUETOOTH_PRIVILEGED
permission (at least on newest Androids).
Implementation
Future<bool?> bondDeviceAtAddress(String address,
{String? pin, bool? passkeyConfirm}) async {
if (pin != null || passkeyConfirm != null) {
if (_pairingRequestHandler != null) {
throw "pairing request handler already registered";
}
setPairingRequestHandler((BluetoothPairingRequest request) async {
Future.delayed(Duration(seconds: 1), () {
setPairingRequestHandler(null);
});
if (pin != null) {
switch (request.pairingVariant) {
case PairingVariant.Pin:
return pin;
default:
// Other pairing variant requested, ignoring pin
break;
}
}
if (passkeyConfirm != null) {
switch (request.pairingVariant) {
case PairingVariant.Consent:
case PairingVariant.PasskeyConfirmation:
return passkeyConfirm;
default:
// Other pairing variant requested, ignoring confirming
break;
}
}
// Other pairing variant used, cannot automate
return null;
});
}
return await _methodChannel
.invokeMethod('bondDevice', {"address": address});
}