2

Can someone tell me how to send GATT notifications using DBUS api's. Currently I am using bluez5.43. I am trying to register a service and send notifications. I have taken the reference of gatt-service.c which is present under the tools directory. When i look at the source code the characteristic has several characteristic methods registered with it. Out of those one is

GDBUS_ASYNC_METHOD("StartNotify", NULL, NULL, chr_start_notify)

but when i navigate to chr_start_notify,

I see the following

static DBusMessage *chr_start_notify(DBusConnection *conn, DBusMessage *msg, void *user_data) 
{ 
    return g_dbus_create_error(msg, DBUS_ERROR_NOT_SUPPORTED, "Not Supported"); 
}

Can anyone at least tell me is there any DBUS api for handling this, or dbus doesn't still support GATT server notifications?

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51

1 Answers1

0

I had the same issue and I found I workaround.

IF your client as enable notification upon your characteristic, this two following lines will set characteristic current value and BlueZ will handle it in stack and notify all subscribers

gatt_characteristic1_set_value(interface,value);
g_dbus_interface_skeleton_flush(G_DBUS_INTERFACE_SKELETON(interface));

You can, as an example, run a thread which call this function every X seconds, and your client will be notified every X seconds.

EDIT :

GattCharacteristic1 is a C DBus object create by gdbus-codegen from a xml file. https://developer.gnome.org/gio/stable/gdbus-codegen.html

To help you, this is my xml file that I wrote according to BlueZ API doc.

<?xml version="1.0" encoding="UTF-8"?>
<node xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd">
    <interface name="org.bluez.GattCharacteristic1">
        <property name="UUID" type="s" access="read" />
        <property name="Service" type="o" access="read" />
        <property name="Value" type="ay" access="read" />
        <property name="Notifying" type="b" access="read" />
        <property name="Flags" type="as" access="read" />

        <method name="ReadValue">
            <arg name="options" type="a{sv}" direction="in" />
            <arg name="value" type="ay" direction="out" />
        </method>
        <method name="WriteValue">
            <arg name="value" type="ay" direction="in" />
            <arg name="options" type="a{sv}" direction="in" />
        </method>
        <method name="StartNotify"/>
        <method name="StopNotify"/>
    </interface>
</node>

Once you have your xml file (named org.bluez.GattCharacteristic1.xml) which describe your GATT BlueZ object, use gbus-codegen to generate a "C DBus Object"

gdbus-codegen --generate-c-code org_bluez_gatt_characteristic_interface --interface-prefix org.bluez. org.bluez.GattCharacteristic1.xml

Now add c and h files into your sources codes

The following lines show HOW I create one GATT BlueZ characteristic upon DBus

const char* char_flags[] = {"read", "write", "notify", "indicate", NULL};

GattCharacteristic1* interface = gatt_characteristic1_skeleton_new();
// dbus object properties
gatt_characteristic1_set_uuid(interface,UUID);
gatt_characteristic1_set_service(interface,service_name);
gatt_characteristic1_set_value(interface,value);
gatt_characteristic1_set_notifying(interface,notifying);
gatt_characteristic1_set_flags(interface,flags);
// get handler (for example), please read doc from gdbus-codegen provide above.
g_signal_connect(interface,
        "handle_read_value",
        G_CALLBACK(dbus_client_on_handle_gatt_characteristic_read_value),
        NULL);
// register new interface on object
g_dbus_object_skeleton_add_interface(object,G_DBUS_INTERFACE_SKELETON(interface));
// exports object on manager
g_dbus_object_manager_server_export(server_manager,object);

Please edit flags as you need. Keep a pointer upon interface object and use lines that I provides in the first answer. GBus doc is well documented, so I hope you will find every that you need.

François Dupont
  • 406
  • 1
  • 4
  • 19
  • Hey Francois, thanks for the info. Can u give more details, i couldn't find the gatt_characteristic1_set_value api, where is its declared or defined? also what do u mean by the interface here? – Sayan Dasgupta Apr 25 '17 at 07:03
  • I am currently using bluez5.43, I was not able to find gatt_characteristic1_set_value declaration in the code – Sayan Dasgupta Apr 25 '17 at 07:20