I am bonding/pairing with a Bluetooth Low Energy device from my Android app. When bonding is initiated, an "Enter PIN"-dialog is shown. It automatically disappears when I set pin from code. The dialog appears and disappears slow enough to confuse and annoy the user. How can I avoid this dialog?
I found nothing on bonding in the android BLE guide https://developer.android.com/guide/topics/connectivity/bluetooth-le.html
I was helped by this other question, however the solution does not remove the dialog. How can I avoid or dismiss Android's Bluetooth pairing notification when I am doing programmatic pairing?
This question suggests removing the reciever in the SDK. Android Prevent Bluetooth Pairing Dialog
Is there really no way to solve this within API? Most question on the subject are a few years old and there has been a lot of changes in the Android Bluetooth API recently. I am currently using API 19 (4.4 Kitkat) but I would use API 22 (5.1 Lollipop) if that would help.
This is how I do the bonding:
myBluetoothDevice.createBond();
I then listen for the pairing intent to be able to provide the pin at the right moment. I also listen for bonding intent.
//Register before calling createBond
context.registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
context.registerReceiver(broadcastReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));
//The recievers
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action))
{
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
byte[] pin = convertPinToBytes("111111"); //convertPinToBytes for some reason not available in API, so I made a copy
device.setPin(pin);
}
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
if (BluetoothDevice.BOND_BONDED == device.getBondState())
{
//Success!
}
}
}