Send a dialog box asking to confirm Bluetooth connection on the front panel to request a PIN

What I am trying to do is add a dialog to enter a PIN for the pairing process.

After connecting to the device, I receive a notification, but the pairing dialog is not displayed. I have to open it manually.

So far I have tried the following methods, which are called in the broadcast receiver when I receive 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 correctly, but after entering it, it does not connect my device.

I have 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);
}

      

That my app is crashing because it says I don't have broadcast permissions for PAIRING_REQUEST (even though I set both BLUETOOTH and BLUETOOTH_ADMIN permissions)

Please, I really need to show this dialog and much better if it's the default. I am connecting to a BLE device and once connected it needs a PIN to pair and the ability to change some of the characteristics.

Your help would be greatly appreciated!

Thanks in advance!

+3


source to share


4 answers


Try using the new Android BluetoothDevice API: bluetoothdevice.createBond (). After calling this method, the system will automatically call the connection request dialog. Then you can enter your PIN in this pop-up dialog.

Consider adding something like this to 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());
    }
}

      

+1


source


I had a similar problem, make sure your bluetooth user permissions are set in the xml manifest tag not under the app tag - in the AndroidManifest (that is). Hope this helps.



0


source


You can use the connection method directly to pair the device. If the two devices have not been previously paired, then the Android framework will automatically display a notification or message to pair with the user during the pairing procedure. Therefore, when trying to connect devices, your application does not have to worry about whether the devices are paired.

0


source


Try this for all API levels:

 public static void pairDevice(BluetoothDevice device) {
        try {
            Method method = device.getClass().getMethod("createBond", (Class[]) null);
            method.invoke(device, (Object[]) null);
            //From API 19.
            //  device.createBond();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

      

0


source







All Articles