Search code examples
dbusbluez

Device connect/disconnect notification via Bluez DBus API


How can I receive a signal or notification when a connection is created or destroyed through bluez dbus API?

Polling Connected() of all devices under /org/bluez/hci0 works but it is not an efficient way, IMHO.


Solution

  • You need to listen on PropertiesChanged signal on the org.bluez.Device1 interface. I don't have direct example for the device interface, but the template is below,

    static void bluez_signal_device_changed(GDBusConnection *conn,
                        const gchar *sender,
                        const gchar *path,
                        const gchar *interface,
                        const gchar *signal,
                        GVariant *params,
                        void *userdata)
    {
        (void)conn;
        (void)sender;
        (void)path;
        (void)interface;
        (void)userdata;
    
        GVariantIter *properties = NULL;
        GVariantIter *unknown = NULL;
        const char *iface;
        const char *key;
        GVariant *value = NULL;
        const gchar *signature = g_variant_get_type_string(params);
    
        if(strcmp(signature, "(sa{sv}as)") != 0) {
            g_print("Invalid signature for %s: %s != %s", signal, signature, "(sa{sv}as)");
            goto done;
        }
    
        g_variant_get(params, "(&sa{sv}as)", &iface, &properties, &unknown);
        while(g_variant_iter_next(properties, "{&sv}", &key, &value)) {
            if(!g_strcmp0(key, "Connected")) {
                if(!g_variant_is_of_type(value, G_VARIANT_TYPE_BOOLEAN)) {
                    g_print("Invalid argument type for %s: %s != %s", key,
                            g_variant_get_type_string(value), "b");
                    goto done;
                }
                g_print("Device is \"%s\"\n", g_variant_get_boolean(value) ? "Connected" : "Disconnected");
            }
        }
    done:
        if(properties != NULL)
            g_variant_iter_free(properties);
        if(value != NULL)
            g_variant_unref(value);
    }
    
    
    GDBusConnection *con = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
    prop_changed = g_dbus_connection_signal_subscribe(con,
                        "org.bluez",
                        "org.freedesktop.DBus.Properties",
                        "PropertiesChanged",
                        NULL,
                        "org.bluez.Device1",
                        G_DBUS_SIGNAL_FLAGS_NONE,
                        bluez_signal_device_changed,
                        NULL,
                        NULL);
    

    With the above sample code, signal handling function bluez_signal_device_changed is called whenever a property is change in the Device` interface.

    You can find more examples in https://gist.github.com/parthitce and explanation in https://www.linumiz.com/