11

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!
        }
    }
}
Community
  • 1
  • 1
KixX
  • 236
  • 3
  • 8
  • Is there any specific reason you use pin pairing and not just "just works" pairing? Then you don't need any dialog. – Emil Sep 26 '16 at 21:06
  • @Emil, I want to avoid other phones, without the provided app, connecting to my device (or at least make it more difficult). Having known how much trouble this dialog would cause, I would have let the user enter the pin manually. – KixX Sep 27 '16 at 07:30
  • I think that is not the correct way to go. Instead send something on some characteristics only you know about. – Emil Sep 27 '16 at 10:14
  • I have exactly same problem, may be you can go this way : get callback from Android's Bluetooth pairing notification and handle anythings there. please share any other solution. i need open custom dialog for user to enter name + pin for ble device and after bonded, i wanna save data in local database. – mortezahosseini Nov 22 '18 at 03:30

0 Answers0