3

我编写了一些基本代码来使用 dbus api 访问“开始查询”bluez 功能。开始查询正在发生,我可以在 hcidump 上看到。但我没有收到来自 dbus 的任何信号,即“DeviceFound”。我已经尝试了很多。我尝试使用不同的 dbus 工具,例如 d-feet、dbus-monitor、bustle,但我没有任何线索。

下面是我写的代码。任何人都请告诉我为什么这段代码不起作用。

#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus.h>
#include <glib.h>
struct generic_data {
    unsigned int refcount;
    GSList *interfaces;
    char *introspect;
};
static void unregister(DBusConnection *connection, void *user_data)
{
}

static DBusHandlerResult filter_func(DBusConnection *connection,
                    DBusMessage *message, void *user_data)
{
    printf("Signal is called \n");
    if (dbus_message_is_signal(message, "org.bluez.Adapter",
                        "DeviceFound")) 
    {
        const char *adapter, *bdaddr;
        char *name;
        DBusMessageIter iter;

        dbus_message_iter_init(message, &iter);
        dbus_message_iter_get_basic(&iter, &bdaddr);
        printf("Finally found device address is %s\n", bdaddr);
    }
}
static DBusObjectPathVTable generic_table = {
    .unregister_function    = unregister,
    .message_function   = filter_func,
};
int main(int argc, char **argv) {
    DBusConnection *conn;
    DBusError error;
    DBusMessage *msg, *reply,*signal;
    dbus_bool_t hcid_exists,start;
    DBusMessageIter reply_iter;
    const char *name,*address;
    char *adapter, *match;
    DBusMessageIter iter;
    struct generic_data *data;
    va_list var_args;
    static GMainLoop *loop = NULL;
    conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
    dbus_error_init(&error);

    hcid_exists = dbus_bus_name_has_owner(conn, "org.bluez", &error);
    if(hcid_exists)
    printf("good news hurrey\n");

    /* Get the default adapter */
    msg = dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "DefaultAdapter");
    if (msg == NULL) {
        dbus_connection_unref(conn);
        return FALSE;
    }

    reply = dbus_connection_send_with_reply_and_block(conn, msg, -1, &error);

    dbus_message_unref(msg);

    if (dbus_error_is_set(&error)) 
    {
        dbus_connection_unref(conn);
    }

    dbus_message_iter_init(reply, &reply_iter);
    if (dbus_message_iter_get_arg_type(&reply_iter) != DBUS_TYPE_OBJECT_PATH)
    {
        dbus_message_unref(reply);
        dbus_connection_unref(conn);
        return FALSE;
    }

    dbus_message_iter_get_basic(&reply_iter, &adapter);
    adapter = g_strdup(adapter);
    //printf("Ohhhh gooood finally got adapter name %s\n",adapter);
    dbus_message_unref(reply);
    data = g_new0(struct generic_data, 1);
    if (!dbus_connection_register_object_path(conn,adapter,&generic_table, data)) {
        g_free(data->introspect);
        g_free(data);
        return FALSE;
    }
    if (!dbus_connection_add_filter(conn, filter_func, data, g_free))
    {
        g_free(adapter);
        dbus_connection_unref(conn);
        return FALSE;
    }
    if(conn!=NULL)
    msg = dbus_message_new_method_call("org.bluez",adapter,"org.bluez.Adapter", "StartDiscovery");
    else
    printf("conn is failed\n");

    if(msg!=NULL)
    start = dbus_connection_send_with_reply(conn, msg,NULL,-1);
    else
        printf("msg is failed\n");
    if(start)
    {
        //printf("Main llop hasd tp start\n");
        loop = g_main_loop_new(NULL, TRUE);
        g_main_loop_run(loop);
    }

    dbus_message_unref(msg);
    dbus_message_unref(reply);
    dbus_connection_close(conn);
    return 0;
}
4

0 回答 0