Specifying a link key in android without pairing

I needed to solve this problem myself. The crucial step was finding this code, which told me to add the android.bluetooth package to my project, and add the files IBluetooth.aidl and IBluetoothCallback.aidl (which you'll find at the link).

Once you instantiate the IBluetooth object, you have access to the BluetoothService class, and can use any of the methods in IBluetooth.aidl. The method I was interested in was

setPin(String address, byte[] pin)

The problem with using it is the other Bluetooth code expects the pairing dialog to have been called already, and keeps track of that in a HashMap in the BluetoothEventLoop class. If you try calling setPin() without having initiated a pairing request, you'll see an error like this:

setPin(<address>) called but no native data available, ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device or by bluez.

So the workaround for me (using the Chat example) was starting the connect thread to initiate the pairing request, then sleeping 500 ms to ensure the thread had started, then calling setPin().


there is no public api mechanism to associate a link key with a device without going through the pairing process.

After pairing this association is automatically created (aka bonding) once devices are bonded then further connection will re-use the link key that was generated previously.

even if device is non-discoverable you should still be able to connect bond / pair with it, if you know the device bluetooth address.

internal / private mechanism by changing the underlying android bluez code, and hooking up to feed a pre-generated link key etc is theoretically possible and it will be a difficult project and a custom solution. that is assuming that you have the link keys to feed / associate. note - link key is a function of the device address of both devices in addition to device clock etc. bluetooth