首页 > 解决方案 > Android 与自定义 USB 蓝牙适配器配对

问题描述

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

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

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

我在 tinycore 上使用 Bluez。

标签: bluetoothandroid-bluetoothbluez

解决方案


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

Bluez 始终要求至少 6 位 PIN 密钥,如果您想将自己限制为 4 位 PIN,那么您应该使用此处指定的“0”进行填充。

对于您的自定义代理,您需要实现“ 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 的相同示例,在此问题中回答了更多详细信息。添加它以供将来参考和完整性。


推荐阅读