Android配对到自定义USB蓝牙适配器



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

我想保证,在没有固定的预设PIN的情况下,当USB设备被发现时,任何人都无法尝试与它配对。我在使用bluetoothctl和Bluez配置控制器时遇到问题。我能得到的最好的是6位数的密钥比较,但我不会在usb连接的盒子上显示代码。

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

我在tinycore上使用Bluez。

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

Bluez总是要求最少有6位数的PIN密钥,如果你想将自己的PIN限制在4位数,那么你应该使用这里指定的"0"来填充。

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

如果只限制您的Android设备,您可以在DisplayPinCode/DisplayPasskey中比较您的设备MAC地址,它会将请求配对的设备的MAC地址作为"First Argumentobject device"。

注意,"对象设备"是作为对象路径的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: %sn", 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"中基于"method"名称实现"DisplayPinCode/DisplayPasskey",该名称作为参数。

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

最新更新