简体   繁体   English

在前面带蓝牙配对请求通知对话框以询问PIN

[英]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. 我要做的是在对话框中输入PIN以进行配对过程。

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: 到目前为止,我尝试了以下方法,当我得到PAIRING_REQUEST动作时,在广播接收器中调用这些方法:

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) 哪个崩溃我的应用程序,因为它说我没有权限为PAIRING_REQUEST发送广播(即使我设置了BLUETOOTH和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. 我正在连接到BLE设备,连接后需要PIN才能配对,并且能够修改某些特性。

Your help would be much appreciated! 非常感谢您的帮助!

Thanks in advance! 提前致谢!

Try to use new Android BluetoothDevice API: bluetoothdevice.createBond(). 尝试使用新的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. 然后,您可以在该弹出对话框中输入PIN。

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

I had a similar issue, make sure your bluetooth user permissions are set within the manifest xml tag not under the application tag - in the AndroidManifest (that is). 我遇到了类似的问题,请确保您的蓝牙用户权限设置在清单 xml标记内,而不是在应用程序标记下 - 在AndroidManifest中(即)。 Hope this helps. 希望这可以帮助。

You can use connect method directly to pair the device.If the two devices have not been previously paired, then the Android framework will automatically show a pairing request notification or dialog to the user during the connection procedure. 您可以直接使用connect方法配对设备。如果两个设备之前没有配对,那么Android框架将在连接过程中自动向用户显示配对请求通知或对话框。 So when attempting to connect devices, your application does not need to be concerned about whether or not the devices are paired. 因此,在尝试连接设备时,您的应用程序无需担心设备是否已配对。

Try to use this for all API levels: 尝试将此用于所有API级别:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM