蓝牙配对-如何显示简单的“取消/配对”对话框?

Ale*_*ber 4 android bluetooth android-intent android-bluetooth bluetooth-oob

我已经在GitHub上为这个问题准备了一个简单的测试项目

我正在尝试创建一个Android应用,该应用将从计算机屏幕扫描QR码,然后使用数据(MAC地址和PIN或哈希)与蓝牙设备轻松配对(绑定)。

与流行的InstaWifi应用类似-但适用于经典蓝牙。

出于测试目的,我尚未执行任何条形码扫描,而仅显示设备列表:

设备清单

用户触摸其中一台设备后,将在MainActivity.java中尝试配对:

private void startBluetoothPairing(BluetoothDevice device) {
    Intent pairingIntent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
    pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
                BluetoothDevice.PAIRING_VARIANT_PIN);
    pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, 1234);
    //device.setPin(new byte[]{1,2,3,4});  <- DOES NOT CHANGE ANYTHING
    //device.setPairingConfirmation(false);
    startActivityForResult(pairingIntent, REQUEST_BT_SETTINGS);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,弹出窗口仍然要求输入PIN:

销对话框

因为我实际上已经在源代码中指定了PIN,所以我实际上希望显示另一个更简单的系统对话框(在进行NFC OOB配对时会显示该对话框):

配对对话框

通过搜索解决方案,我知道有一个setPin()方法,但是它在这里不适用(或者是?)-因为我正在尝试将整个智能手机与蓝牙设备配对,而不仅是与应用程序配对...

我的问题:如何使Android OS显示简单的“取消/配对”对话框?

在GitHub上搜索蓝牙配对请求字符串未显示任何提示...

更新:根据unrealsoul007的建议(感谢),我已经在MainActivity.java中更新了源代码,现在显示了简单的“取消/配对”对话框:

private void startBluetoothPairing(BluetoothDevice device) {
    Intent pairingIntent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
    pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
        BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION);
    pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivityForResult(pairingIntent, REQUEST_BT_PAIRING);
}
Run Code Online (Sandbox Code Playgroud)

但是我不确定如何完成配对过程-因为甚至在关闭对话框之前onActivityResult都会调用resultCode=0

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // this is called before user clicks Cancel or Pair in the dialog
    if (requestCode == REQUEST_BT_PAIRING) { 
        if (resultCode == Activity.RESULT_OK) {  // 0 != -1
            Log.d("XXX", "Let#s pair!!!!"); // NOT CALLED
        }

        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

unr*_*007 5

系统会提示您输入密码,因为这是您在密码中所要求的pairingIntent

而不是使用

pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
                BluetoothDevice.PAIRING_VARIANT_PIN);
pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, 1234);
Run Code Online (Sandbox Code Playgroud)

使用

pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PASSKEY_CONFIRMATION);
Run Code Online (Sandbox Code Playgroud)

如前所述这里

系统将提示用户确认屏幕上显示的密码,或者应用程序将为用户确认密码。