要实现这种情况,您可能需要实现自定义代理。由于您没有显示,您必须从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 中回答了更多详细信息。添加它以供将来参考和完整性。