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.
There are many similar questions about this on SO but they are either a) about Bluetooth Classic, b) old (before setPin()
and createBond()
were public), or c) unanswered.
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 have created a device using mBed running on the nRF51-DK and given it a single 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
requireSecurity(SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM);
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.
Best Answer
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 methodcancelPairingUserInput()
but that doesn't seem to work for me.Edit: Success!
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 theACTION_PAIRING_REQUEST
. Fortunately it is an ordered intent broadcast so you can intercept it before the system does.Here's the procedure.
BluetoothDevice.ACTION_PAIRING_REQUEST
changed broadcast intents. Use a high priority!ACTION_PAIRING_REQUEST
broadcast receiver, check that the pairing type isBluetoothDevice.PAIRING_VARIANT_PIN
and if so, callsetPin()
andabortBroadcast()
. Otherwise you can just let the system handle it, or show an error or whatever.Here is the code.