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().

Leave a Comment