无需用户输入密码并使用 Android API 确认的 Android 蓝牙配对 [英] Android Bluetooth Pairing without User Enter Pin and Confirmation Using Android API

查看:43
本文介绍了无需用户输入密码并使用 Android API 确认的 Android 蓝牙配对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a beginner in Android programming since I only started 3 months ago. I'm doing a project which connects the android app to arduino using bluetooth. I already have a code for the android app (bluetooth.adapter,sockets,.etc.). The code for connection is already working. One of the goal is for the android app to automatically input the password when pairing with the bluetooth device without asking user to input the PIN.

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

The old posts on this forum do not help much. (many suggested using insecure mode, but I do need secure mode, also in my case, the arduino is the server while cellphone app is the client, so the createInsecureRfcommSocketToServiceRecord() server method does not work for me)

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

I searched and found this in android developer site about bluetoothdevice class:

setPairingConfirmation(布尔确认)确认 PAIRING_VARIANT_PASSKEY_CONFIRMATION 配对的密钥.

setPairingConfirmation(boolean confirm) Confirm passkey for PAIRING_VARIANT_PASSKEY_CONFIRMATION pairing.

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

PAIRING_VARIANT_PIN = "The user will be prompted to enter a pin or an app will enter a pin for user".

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

PAIRING_VARIANT_PASSKEY_CONFIRMATION = "The user will be prompted to confirm the passkey displayed on the screen or an app will confirm the passkey for the user"

好像是在用code,app会是输入密码确认的密码使其成为自动连接"功能,但 android 站点没有提供有关如何使用它的示例代码.你们中有人有使用这个或相关过程的示例代码吗?感谢您的帮助!

Seems using the code, the app will be the one to input the password and confirm the password making it an "auto-connect" features but the android site does not give a sample code on how to use this. Does any of you have a sample code in using this or related process? I appreciate your help!

推荐答案

首先澄清一下,此解决方案是为较新版本的 API(15 或更高版本?)而设计的

First to clarify, this solution is designed for newer version of API (15 or later?)

我在另一篇文章中找到了答案(请参阅 这里).这是我用详细代码重新组织的答案.

I found the answer written in another post (see Roldofo's answer in Here). Here is my reorganized answer with detailed code.

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

In a nutshell, you need to setup a broadcast receiver to trap the ACTION_PAIRING_REQUEST, and then programmatically pass the PIN and confirm.

注册一个广播接收器:

    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();
            }
        }
    }
};

然后在您的活动或片段(无论您想启动配对的任何地方),您都可以调用以下定义的 pairDevice() 方法来调用配对尝试(这将生成一个 ACTION_PAIRING_REQUEST)

Then at your activity or fragment (wherever you want to initiate the pairing), you can call the following defined pairDevice() method to invoke pairing attempt (which will generate a 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 确认的 Android 蓝牙配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆