首页 > 解决方案 > 将 Android 与 Arduino (HC-05) 配对,无需手动输入密码

问题描述

我正在将一些字符串数据从 android 发送到 HC-05 的 arduino。

目前我可以成功地将数据从android发送到hc-05(有或没有配对),我已经使用这种方法来创建套接字

private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {

            try {
                final Method  m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
                return (BluetoothSocket) m.invoke(device, MY_UUID);
            } catch (Exception e) {
                Log.e(TAG, "Could not create Insecure RFComm Connection",e);
            }

        return  device.createRfcommSocketToServiceRecord(MY_UUID);
    }

如您所见。它首先尝试创建InsecureRfcommSocket. 它使用不安全的连接来连接。AFAIK 它可能会导致中间人攻击,所以我想使用安全通信createRfcommSocketToServiceRecord。但我也不希望用户在配对时输入密码(如果没有密码交换,安全通信插座就不可能吗?:如果我错了,请纠正我)。所以

我的问题是:

  1. 如何配对 hc-05 而无需用户干预引脚输入?(我尝试过使用意图过滤器和广播接收器,但它不起作用(系统总是优先于我的接收器))
  2. 有没有办法在不配对的情况下创建安全的通信频道?因为我只想要安全的通道和数据交换。
  3. 如果我想以编程方式输入 pin,我可以使用device.setpin()还是应该为此触发 AT-Command?

标签: androidarduinobluetooth-lowenergy

解决方案


推荐阅读