I have a list of BluetoothDevice and I want to pair programmatically with one of them that has a PIN.
I have read several posts here in which the subject is discussed, but I've found two very different approachs.
FIRST OPTION: You call the device.createBond()
method. Then, on a BroadcastReceiver, you listen the BluetoothDevice.ACTION_PAIRING_REQUEST
action and there you call
device.setPin(PIN_BYTES);
device.setPinConfirmation(true);
You can see the complete example & post here: How to pair Bluetooth device programmatically Android
SECOND OPTION: What if you call device.setPin(PIN_BYTES)
and device.setPinConfirmation(true)
first, and then device.createBond()
? Eg:
if(connConfig!=null && connConfig.bluetooth!=null){
device.setPin(connConfig.bluetooth.pass);
device.setPairingConfirmation(true);
device.createBond();
}
And then you forget about listening the BluetoothDevice.ACTION_PAIRING_REQUEST
action on your BroadcastReceiver and only pay attention to BluetoothDevice.ACTION_BOND_STATE_CHANGED
events? Android + Pair devices via bluetooth programmatically
See in the first answer the code. As far as I get it, that dude isn't using any PIN for pairing, so I also need to use the setPin method.
Which of both you find better?
Am I missing something? BTW: I'm not using reflection because Im not targeting older platforms.