Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring Bluetooth pairing request notification dialog on front to ask for PIN

What I'm trying to do is to brin the dialog to input the PIN for a pairing process.

After I connect to a device, I receive a notification but the pairing dialog does not show up. I have to open it manually.

So far I tried the following methods which are called in the broadcast receiver when I get the PAIRING_REQUEST action:

public void pairDevice(BluetoothDevice device)
{
    String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
    Intent intent = new Intent(ACTION_PAIRING_REQUEST);
    String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
    intent.putExtra(EXTRA_DEVICE, device);
    String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
    int PAIRING_VARIANT_PIN = 0;
    intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

}

Which shows the dialog properly but after I input it, it does not pair my device.

I also tried this code:

public void pairDevice(BluetoothDevice device)
{   
    Intent intent = new Intent("android.bluetooth.device.action.PAIRING_REQUEST");
    String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
    intent.putExtra(EXTRA_DEVICE, device);
    int PAIRING_VARIANT_PIN = 272;
    intent.putExtra("android.bluetooth.device.extra.PAIRING_VARIANT", PAIRING_VARIANT_PIN);
    sendBroadcast(intent);
}

Which crashes my app because it says I don't have permissions to send broadcast for PAIRING_REQUEST (even if I set both permissions BLUETOOTH and BLUETOOTH_ADMIN)

Please, I really need to show this dialog and much better if it is the default one. I am connecting to a BLE device, and after connected it requires a PIN for pairing and be able to modify some characteristics.

Your help would be much appreciated!

Thanks in advance!

like image 937
kodartcha Avatar asked Nov 19 '25 16:11

kodartcha


1 Answers

Try to use new Android BluetoothDevice API: bluetoothdevice.createBond(). After you call this method, the system will invoke the pairing request dialog for you automatically. Then you can enter PIN in that pop up dialog.

Consider adding something like this in your code, where you want to start the pairing process:

private void pairDevice(BluetoothDevice device) {
    try {
        Log.d(TAG, "Start Pairing... with: " + device.getName());
        device.createBond();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}
like image 110
ian Avatar answered Nov 21 '25 04:11

ian