无需用户使用 Android API 输入 PIN 和确认的 Android 蓝牙配对

标签 android bluetooth android-bluetooth pairing

我是 Android 编程的初学者,因为我才 3 个月前才开始。我正在做一个使用蓝牙将 android 应用程序连接到 arduino 的项目。我已经有了 android 应用程序的代码(bluetooth.adapter、套接字等)。连接代码已经在工作。目标之一是让 Android 应用程序在与蓝牙设备配对时自动输入密码,而不要求用户输入 PIN。

这个论坛上的旧帖子没有多大帮助。 (许多人建议使用不安全模式,但我确实需要安全模式,在我的情况下,arduino 是服务器,而手机应用程序是客户端,因此 createInsecureRfcommSocketToServiceRecord() 服务器方法对我不起作用)

我在 android 开发者网站上搜索并找到了这个关于蓝牙设备类的内容:

setPairingConfirmation( bool 确认) 确认 PAIRING_VARIANT_PASSKEY_CONFIRMATION 配对的 key 。

PAIRING_VARIANT_PIN = "系统将提示用户输入 PIN 或应用程序将为用户输入 PIN"。

PAIRING_VARIANT_PASSKEY_CONFIRMATION = "系统将提示用户确认屏幕上显示的 key ,或者应用程序将为用户确认 key "

似乎使用了代码,该应用程序将是输入密码并确认的应用程序 密码使它具有“自动连接”功能,但 android 站点没有提供有关如何使用此功能的示例代码。你们中有人有使用此过程或相关过程的示例代码吗?感谢您的帮助!

最佳答案

首先要澄清的是,这个解决方案是为较新版本的 API(15 或更高版本?)设计的

我在另一篇文章中找到了答案(参见 Roldofo 在 Here 中的回答)。这是我用详细代码重新组织的答案。

简而言之,您需要设置一个广播接收器来捕获 ACTION_PAIRING_REQUEST,然后以编程方式传递 PIN 并确认。

注册一个广播接收器:

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    getActivity().registerReceiver(mPairingRequestReceiver, filter);

接收者的定义:

private final BroadcastReceiver mPairingRequestReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
            try {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 1234);
                    //the pin in case you need to accept for an specific pin
                    Log.d(TAG, "Start Auto Pairing. PIN = " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",1234));
                    byte[] pinBytes;
                    pinBytes = (""+pin).getBytes("UTF-8");
                    device.setPin(pinBytes);
                    //setPairing confirmation if neeeded
                    device.setPairingConfirmation(true);
            } catch (Exception e) {
                Log.e(TAG, "Error occurs when trying to auto pair");
                e.printStackTrace();
            }
        }
    }
};

然后在您的 Activity 或 Fragment(无论您希望在何处启动配对)中,您可以调用以下定义的 pairDevice() 方法来调用配对尝试(这将生成一个 ACTION_PAIRING_REQUEST)

private void pairDevice(BluetoothDevice device) {
    try {
        Log.d(TAG, "Start Pairing... with: " + device.getName());
        device.createBond();
        Log.d(TAG, "Pairing finished.");
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

关于无需用户使用 Android API 输入 PIN 和确认的 Android 蓝牙配对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35519321/

相关文章:

Android Bluetooth LE - 读取 float 特性

android - 使用 Android Espresso 框架模拟用户在系统 Activity 中的点击

Android Bluetooth Low Energy 有时会锁定

java - 在 Android Mapsforge map 上添加多个标记

java - android - if语句的switch case返回值

Android 蓝牙快速设备发现

蓝牙 5.1 到达角向后兼容性

android - 即使在提供 "app:layout_scrollFlags"标志后,工具栏也不会滚动

Android - 处理应用程序崩溃

python - 树莓派蓝牙和乐高头脑 Storm