admin管理员组

文章数量:1318055

android 取消蓝牙配对框,android

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

//device.setPairingConfirmation(false);

startActivityForResult(pairingIntent, REQUEST_BT_SETTINGS);

}

不幸的是,弹出窗口仍然要求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);

}

但是我不确定如何完成配对过程 - 因为即使在关闭对话框之前,也会使用resultCode=0调用onActivityResult :

@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;

}

}

本文标签: android 取消蓝牙配对框Android