【发布时间】:2015-06-22 10:59:47
【问题描述】:
我已经在 GitHub 上为这个问题准备了a simple test project。
我正在尝试创建一个 Android 应用程序,它会扫描计算机屏幕上的二维码,然后使用数据(MAC 地址和 PIN 或哈希)与蓝牙设备。
类似于流行的InstaWifi app - 但适用于经典蓝牙。
出于测试目的,我还没有进行任何条码扫描,只是显示设备列表:
用户触摸其中一台设备后,在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);
}
不幸的是,弹出窗口仍然要求输入 PIN:
因为我实际上已经在我的源代码中指定了一个 PIN,我实际上希望显示另一个更简单的系统对话框(在进行 NFC OOB 配对时会显示这个对话框):
通过搜索解决方案,我知道有一个setPin() 方法,但在这里不适用(或者是吗?) - 因为我正在尝试将整个智能手机与蓝牙设备配对,而不仅仅是应用程序...
我的问题:如何让 Android 操作系统显示简单的取消/配对对话框?
在 GitHub 上搜索 Bluetooth pairing request 字符串没有显示任何提示...
更新: 根据 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);
}
但是我不确定如何完成配对过程 - 因为即使在对话框关闭之前,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;
}
}
【问题讨论】:
-
点击配对后
onActivityResult是否也会被调用..? -
onActivityResult 是在挑选配对后调用还是仅在弹出窗口出现时调用?
-
只有当您接受配对时,您才能做到这一点?
-
不,我最终使用了 createBond()
标签: android android-intent bluetooth android-bluetooth bluetooth-oob