I found this question in the first post review queue:
Android/Bluetooth programatically pairing options
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 theBluetoothDevice.ACTION_PAIRING_REQUEST
action and there you calldevice.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)
anddevice.setPinConfirmation(true)
first, and thendevice.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 toBluetoothDevice.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: Im not using reflection because Im not targeting older platforms. Thanks!
The user presents two ways to achieve his goal and asks "Which of both you find better?" and "Am I missing something?"
I flagged this question as "primarily opinion based" because—in my opinion—this is a "best practice" question, but the flag was disputed.