setPairingRequestHandler method

void setPairingRequestHandler(
  1. Future handler(
    1. BluetoothPairingRequest request
    )?
)

Allows listening and responsing for incoming pairing requests.

Various variants of pairing requests might require different returns:

  • PairingVariant.Pin or PairingVariant.Pin16Digits (prompt to enter a pin)
    • return string containing the pin for pairing
    • return false to reject.
  • BluetoothDevice.PasskeyConfirmation (user needs to confirm displayed passkey, no rewriting necessary)
    • return true to accept, false to reject.
    • there is passkey parameter available.
  • PairingVariant.Consent (just prompt with device name to accept without any code or passkey)
    • return true to accept, false to reject.

If returned null, the request will be passed for manual pairing using default Android Bluetooth settings pairing dialog.

Note: Accepting request variant of PasskeyConfirmation and Consent will probably fail, because it require Android setPairingConfirmation which requires BLUETOOTH_PRIVILEGED permission that 3rd party apps cannot acquire (at least on newest Androids) due to security reasons.

Note: It is necessary to return from handler within 10 seconds, since Android BroadcastReceiver can wait safely only up to that duration.

Implementation

void setPairingRequestHandler(
    Future<dynamic> handler(BluetoothPairingRequest request)?) {
  if (handler == null) {
    _pairingRequestHandler = null;
    _methodChannel.invokeMethod('pairingRequestHandlingDisable');
    return;
  }
  if (_pairingRequestHandler == null) {
    _methodChannel.invokeMethod('pairingRequestHandlingEnable');
  }
  _pairingRequestHandler = handler;
}