Does anyone have a complete working example of how to programmatically pair with a BLE (not Bluetooth Classic) device that uses passkey entry (i.e. a 6-digit PIN) or Numeric Comparison on Android 4.4 or later? By ‘programmatically’ I mean I tell Android the PIN – the user isn’t prompted.
My understanding is as follows.
- You connect to the device and discover its services.
- You try to read a ‘protected’ characteristic.
- The device returns an authentication error.
- Android somehow initiates pairing and you tell it the PIN.
- You can now read the characteristic.
I set up the security parameters like so:
ble.securityManager().init( true, // Enable bonding (though I don't really need this) true, // Require MitM protection. I assume you don't get a PIN prompt without this, though I'm not 100% sure. SecurityManager::IO_CAPS_DISPLAY_ONLY, // This makes it us the Passkey Entry (PIN) pairing method. "123456"); // Static PIN
And then in the characteristic I used
Now when I try to read it with the Nordic Master Control Panel, I get a pairing request notification like this:
And I can put this PIN in, and then MCP says I’m bonded, and can read the characteristic.
However, in my app I would like to avoid having the user enter the PIN, since I know it already. Does anyone have a complete recent example of how to do this?
Edit: By the way this is the most relevant question I found on SO, but the answer there doesn’t seem to work.
I almost have it working. It pairs programmatically but I can’t get rid of the “Pairing request” notification. Some answers to this question claim to be able to hide it just after it is shown using the hidden method
cancelPairingUserInput() but that doesn’t seem to work for me.
I eventually resorted to reading the source code of
BluetoothPairingRequest and the code that sends the pairing request broadcast and realised I should be intercepting the
ACTION_PAIRING_REQUEST. Fortunately it is an ordered intent broadcast so you can intercept it before the system does.
Here’s the procedure.
- Register to receive
BluetoothDevice.ACTION_PAIRING_REQUESTchanged broadcast intents. Use a high priority!
- Connect to the device.
- Discover services.
- If you have disconnected by now, it’s probably because the bond information is incorrect (e.g. the peripheral purged it). In that case, delete the bond information using a hidden method (seriously Google), and reconnect.
- Try to read a characteristic that requires encryption MitM protection.
- In the
ACTION_PAIRING_REQUESTbroadcast receiver, check that the pairing type is
BluetoothDevice.PAIRING_VARIANT_PINand if so, call
abortBroadcast(). Otherwise you can just let the system handle it, or show an error or whatever.
Here is the code.
I also faced the same problem and after all the research, I figured out the below solution to pair to a BLE without any manual intervention.
(Tested and working!!!)
I am basically looking for a particular Bluetooth device (I know MAC address) and pair with it once found. The first thing to do is to create pair request using a broadcast receiver and handle the request as below.
You need to write the broadcastReceiver and handle it as below.
Voila! You should be able to pair to Bluetooth device without ANY MANUAL INTERVENTION.
Hope this helps 🙂 Please make it right answer if it works for you.