首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Android API无需用户输入Pin和确认即可进行Android蓝牙配对

使用Android API无需用户输入Pin和确认即可进行Android蓝牙配对
EN

Stack Overflow用户
提问于 2016-02-20 13:00:11
回答 3查看 32.8K关注 0票数 11

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

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

我在android开发人员站点上搜索并找到了关于bluetoothdevice类的内容:

用于PAIRING_VARIANT_PASSKEY_CONFIRMATION配对的setPairingConfirmation(布尔确认)确认密钥。

PAIRING_VARIANT_PIN =“将提示用户输入个人识别码,或者应用程序将为用户输入个人识别码”。

PAIRING_VARIANT_PASSKEY_CONFIRMATION =“系统将提示用户确认屏幕上显示的密钥,或者应用程序将为用户确认密钥。”

似乎是在使用代码,应用程序将输入密码并确认密码,使其成为“自动连接”功能,但android网站没有给出如何使用的示例代码。你们中有谁有使用这个或相关过程的示例代码吗?非常感谢您的帮助!

EN

回答 3

Stack Overflow用户

发布于 2016-02-21 06:15:43

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

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

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

注册广播接收器:

代码语言:javascript
运行
复制
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    getActivity().registerReceiver(mPairingRequestReceiver, filter);

接收器的定义:

代码语言:javascript
运行
复制
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)

代码语言:javascript
运行
复制
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());
    }
}
票数 14
EN

Stack Overflow用户

发布于 2018-06-13 17:25:00

我也面临着同样的问题,在所有的研究之后,我找到了下面的解决方案。

(已测试并正常工作!)

我基本上是在寻找一个特定的蓝牙设备(我知道MAC地址),一旦找到就与之配对。要做的第一件事是使用boradcast receiver创建配对请求,并按如下方式处理请求。

代码语言:javascript
运行
复制
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
                intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
                registerReceiver(broadCastReceiver,intentFilter);

您需要编写broadcastReceiver并按如下方式处理它。

代码语言:javascript
运行
复制
String BLE_PIN = "1234"
private BroadcastReceiver broadCastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action))
        {
            BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            bluetoothDevice.setPin(BLE_PIN.getBytes());
            Log.e(TAG,"Auto-entering pin: " + BLE_PIN);
            bluetoothDevice.createBond();
            Log.e(TAG,"pin entered and request sent...");
        }
    }
};

瞧!你应该能够在没有任何人工干预的情况下配对到蓝牙设备。

希望这对你有帮助:-)如果对你有用,请给出正确的答案。

票数 3
EN

Stack Overflow用户

发布于 2020-05-05 18:27:03

是的,这可以通过代码来完成

在主活动中,添加以下代码

代码语言:javascript
运行
复制
BluetoothReceiver myreceiver = new BluetoothReceiver();
var intentfilterparingrequest = new IntentFilter(BluetoothDevice.ActionPairingRequest);
RegisterReceiver(myreceiver, intentfilterparingrequest);

在广播接收器中编写以下代码,如果没有,则创建一个新广播接收器

代码语言:javascript
运行
复制
public class BluetoothReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        string BLE_PIN = "0000";
        var action = intent.Action;
        switch (action)
        {
            case BluetoothDevice.ActionPairingRequest:
                BluetoothDevice bluetoothDevice = 
              (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
                bluetoothDevice.SetPin(Encoding.ASCII.GetBytes(BLE_PIN));
                bluetoothDevice.CreateBond();
                break;
        }
    } 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35519321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档