在前面带蓝牙配对请求通知对话框以要求输入PIN [英] Bring Bluetooth pairing request notification dialog on front to ask for PIN

查看:113
本文介绍了在前面带蓝牙配对请求通知对话框以要求输入PIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是在对话框中输入用于配对过程的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.

到目前为止,我尝试了以下方法,当我收到PAIRING_REQUEST动作时,这些方法会在广播接收器中调用:

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.

我也尝试了以下代码:

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

哪个崩溃了我的应用程序,因为它说我没有发送PAIRING_REQUEST广播的权限(即使我同时设置了BLUETOOTH和BLUETOOTH_ADMIN权限)

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)

请,我确实需要显示此对话框,如果它是默认对话框,那就更好了.我正在连接BLE设备,连接后需要PIN才能配对,并且能够修改某些特征.

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.

非常感谢您的帮助!

提前谢谢!

推荐答案

尝试使用新的Android BluetoothDevice API:bluetoothdevice.createBond().调用此方法后,系统将自动为您调用配对请求对话框.然后,您可以在弹出的对话框中输入PIN.

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());
    }
}

这篇关于在前面带蓝牙配对请求通知对话框以要求输入PIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆