我正在linux中设置一个ble服务器使用pc,并且希望我们使用dbus-api使用c,问题是我成功设置了电源并且可以发现它,但无法启动广告,并且dbus回复“失败。注册广告:org.bluez.Error.Failed”。我无法找到导致它的原因,也许我错过了一些步骤。
该代码是从bluez-5.5 / client参考的。使用工具“ bluetoothctl”执行“开机”,“可发现”和“发布”是没有问题的。我成功设置了电源并可以通过g_dbus_emit_property_changed对其进行发现。在api文档中,它们是属性。以及方法“ RegisterAdvertisement”中的参数,例如“ RegisterAdvertisement(对象广告,字典选项)”。
这是代码段。
主要:
int main()
{
GDBusClient *client;
GDBusProxy *proxy;
main_loop = g_main_loop_new(NULL, FALSE);
connection = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
g_dbus_attach_object_manager(connection);
printf("gatt-service unique name: %s\n", dbus_bus_get_unique_name(connection));
client = g_dbus_client_new(connection, "org.bluez", "/org/bluez");
set_power_on(client);
set_adv_on(client);
g_main_loop_run(main_loop);
g_main_loop_unref(main_loop);
main_loop = NULL;
g_dbus_client_unref(client);
dbus_connection_unref(connection);
return 0;
}
static void set_adv_on(GDBusClient *client)
{
GDBusProxy *proxy;
const char *interface;
proxy = g_dbus_proxy_new(client,"/org/bluez/hci0","org.bluez.LEAdvertisingManager1");
interface = g_dbus_proxy_get_interface(proxy);
if (!strcmp(interface, "org.bluez.LEAdvertisingManager1")) {
printf("proxy_added_cb: %s\n",interface);
ad_manager_added(proxy);
}
cmd_advertise(TRUE);
}
static void ad_manager_added(GDBusProxy *proxy)
{
struct adapter *adapter;
adapter = find_ctrl(ctrl_list, g_dbus_proxy_get_path(proxy));
if (!adapter)
adapter = adapter_new(proxy);
adapter->ad_proxy = proxy;
}
static void cmd_advertise(dbus_bool_t enable)
{
const char *type;
type = "on";
if (!default_ctrl || !default_ctrl->ad_proxy) {
printf("LEAdvertisingManager not found\n");
return;
}printf("LEAdvertisingManager found\n");
if (enable == TRUE)
ad_register(connection, default_ctrl->ad_proxy, type);
else
ad_unregister(connection, default_ctrl->ad_proxy);
}
void ad_register(DBusConnection *conn, GDBusProxy *manager, const char *type)
{
if (ad.registered) {
printf("Advertisement is already registered\n");
return;
}
g_free(ad.type);
ad.type = g_strdup(type);
if (g_dbus_register_interface(conn, AD_PATH, AD_IFACE, ad_methods,
NULL, ad_props, NULL, NULL) == FALSE) {
printf("Failed to register advertising object\n");
return;
}
if (g_dbus_proxy_method_call(manager, "RegisterAdvertisement",
register_setup, register_reply,
conn, NULL) == FALSE) {
printf("Failed to register advertising\n");
return;
}
}