dbus api用于在bluez

时间:2017-04-18 13:06:24

标签: bluetooth-lowenergy bluez gatt

有人可以告诉我如何使用DBUS api发送GATT通知。目前我正在使用bluez5.43。我正在尝试注册服务并发送通知。我已经参考了工具目录下的gatt-service.c。当我查看源代码时,该特征具有几个注册的特征方法。其中一个是

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

但是当我导航到chr_start_notify时,

我看到以下

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

任何人至少可以告诉我有没有DBUS api来处理这个,或者dbus还不支持GATT服务器通知?

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我发现我解决了问题。

如果您的客户端根据您的特征启用通知,则以下两行将设置特征当前值,BlueZ将在堆栈中处理它并通知所有订阅者

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

作为示例,您可以运行一个每X秒调用此函数的线程,并且每隔X秒就会通知您的客户端。

编辑:

GattCharacteristic1是由gdbus-codegen从xml文件创建的C DBus对象。 https://developer.gnome.org/gio/stable/gdbus-codegen.html

为了帮助您,这是我根据BlueZ API doc编写的xml文件。

<?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>

获得描述GATT BlueZ对象的xml文件(名为org.bluez.GattCharacteristic1.xml)后,使用gbus-codegen生成“C DBus对象”

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

现在将c和h文件添加到源代码中

以下几行显示我如何在DBus上创建一个GATT BlueZ特征

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);

请根据需要编辑标记。在接口对象上保留一个指针,并使用我在第一个答案中提供的行。 GBus doc已有详细记录,因此我希望您能找到所需的一切。