【问题标题】:Android pairing to custom USB bluetooth adapterAndroid 与自定义 USB 蓝牙适配器配对
【发布时间】:2019-02-06 20:14:03
【问题描述】:

在将 android 与自定义 USB 适配器(例如 DKBT111)配对时,是否可以强制输入传统的固定引脚。查看 SSP 和“Just works”配对,这似乎不能满足我正在使用的安全要求。

我想保证在没有固定预设 PIN 的情况下,没有人可以尝试与 USB 设备配对。我在使用 bluetoothctl 和 Bluez 配置控制器时遇到问题。我能得到的最好结果是 6 位密钥比较,但我不会在 USB 连接的盒子上显示代码。

我必须更改哪些设置才能为服务器设置 PIN,并且任何手机都需要输入它才能配对?

我在 tinycore 上使用 Bluez。

【问题讨论】:

    标签: bluetooth android-bluetooth bluez


    【解决方案1】:

    要实现这种情况,您可能需要实现自定义代理。由于您没有显示,您必须从options 中选择您设备的一项功能。

    Bluez 总是要求至少 6 位 PIN 密钥,如果您想将自己限制为 4 位 PIN,那么您应该使用指定的“0”填充 here

    对于您的自定义代理,您需要实现“DisplayPinCode”或“DisplayPasskey”方法返回固定 PIN 并使用“RegisterAgent”进行注册。

    为了仅限制您的 Android 设备,您可以在 DisplayPinCode/DisplayPasskey 中比较您的设备 MAC 地址,该地址将请求配对的设备的 MAC 地址作为“第一个参数对象设备”。

    请注意,“对象设备”是 MAC 地址作为对象路径,即 /org/bluez/hciX/dev_AA_BB_CC_XX_YY_ZZ 格式。

    #include <glib.h>
    #include <gio/gio.h>
    #include "agent.h"
    
    #define AGENT_PATH  "/org/bluez/AutoPinAgent"
    
    static void bluez_agent_method_call(GDBusConnection *conn,
                        const gchar *sender,
                        const gchar *path,
                        const gchar *interface,
                        const gchar *method,
                        GVariant *params,
                        GDBusMethodInvocation *invocation,
                        void *userdata)
    {
        g_print("Agent method call: %s.%s()", interface, method);
    }
    
    static const GDBusInterfaceVTable agent_method_table = {
        .method_call = bluez_agent_method_call,
    };
    
    int bluez_register_agent(GDBusConnection *con)
    {
        GError *error = NULL;
        guint id = 0;
        GDBusNodeInfo *info = NULL;
    
        static const gchar bluez_agent_introspection_xml[] =
            "<node name='/org/bluez/SampleAgent'>"
            "   <interface name='org.bluez.Agent1'>"
            "       <method name='Release'>"
            "       </method>"
            "       <method name='RequestPinCode'>"
            "           <arg type='o' name='device' direction='in' />"
            "           <arg type='s' name='pincode' direction='out' />"
            "       </method>"
            "       <method name='DisplayPinCode'>"
            "           <arg type='o' name='device' direction='in' />"
            "           <arg type='s' name='pincode' direction='in' />"
            "       </method>"
            "       <method name='RequestPasskey'>"
            "           <arg type='o' name='device' direction='in' />"
            "           <arg type='u' name='passkey' direction='out' />"
            "       </method>"
            "       <method name='DisplayPasskey'>"
            "           <arg type='o' name='device' direction='in' />"
            "           <arg type='u' name='passkey' direction='in' />"
            "           <arg type='q' name='entered' direction='in' />"
            "       </method>"
            "       <method name='RequestConfirmation'>"
            "           <arg type='o' name='device' direction='in' />"
            "           <arg type='u' name='passkey' direction='in' />"
            "       </method>"
            "       <method name='RequestAuthorization'>"
            "           <arg type='o' name='device' direction='in' />"
            "       </method>"
            "       <method name='AuthorizeService'>"
            "           <arg type='o' name='device' direction='in' />"
            "           <arg type='s' name='uuid' direction='in' />"
            "       </method>"
            "       <method name='Cancel'>"
            "       </method>"
            "   </interface>"
            "</node>";
    
        info = g_dbus_node_info_new_for_xml(bluez_agent_introspection_xml, &error);
        if(error) {
            g_printerr("Unable to create node: %s\n", error->message);
            g_clear_error(&error);
            return 0;
        }
    
        id = g_dbus_connection_register_object(con, 
                AGENT_PATH,
                info->interfaces[0],
                &agent_method_table,
                NULL, NULL, &error);
        g_dbus_node_info_unref(info);
        //g_dbus_connection_unregister_object(con, id);
        return id;
    }
    

    上面的例子是不完整的模板,没有任何具体的方法。您需要根据作为参数的“方法”名称在“bluez_agent_method_call”中实现“DisplayPinCode/DisplayPasskey”。

    编辑: 固定 PIN 的相同示例,在此 question 中回答了更多详细信息。添加它以供将来参考和完整性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      相关资源
      最近更新 更多