【问题标题】:Bluetooth pairing - how to show the simple Cancel/Pair dialog?蓝牙配对 - 如何显示简单的取消/配对对话框?
【发布时间】: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


【解决方案1】:

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

而不是使用

pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
                BluetoothDevice.PAIRING_VARIANT_PIN);
pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, 1234);

使用

pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PASSKEY_CONFIRMATION);

here所述,

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

【讨论】:

  • 谢谢 (+1) 现在显示简单的取消/配对对话框。我想知道接下来要做什么来完成配对 - 我应该为某些广播事件实现 onReceive 方法吗?
  • 由于您正在为Result启动Activity,您将在onActivityResult中获得它的结果。
  • 你也可以玩这里提到的各种意图:developer.android.com/reference/android/bluetooth/…
  • 不幸的是,onActivityResult 甚至在用户触摸对话框中的“取消”或“配对”按钮之前就被调用了 - 请参阅我更新的问题。
  • @unrealsoul007 我需要它在用户接受配对对话框时发生,你知道吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多