【问题标题】:dbus api for sending GATT notification in bluez用于在 bluez 中发送 GATT 通知的 dbus api
【发布时间】:2017-04-18 13:06:23
【问题描述】:

谁能告诉我如何使用 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 服务器通知?

【问题讨论】:

    标签: bluetooth-lowenergy bluez gatt


    【解决方案1】:

    我遇到了同样的问题,我找到了解决方法。

    如果您的客户端启用了您的特征通知,则以下两行将设置特征当前值,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 文档有很好的文档记录,所以我希望你能找到你需要的每一个。

    【讨论】:

    • 您好弗朗索瓦,感谢您提供的信息。你能提供更多细节吗,我找不到 gatt_characteristic1_set_value api,它在哪里声明或定义?还有你这里的界面是什么意思?
    • 我目前使用的是bluez5.43,在代码中找不到gatt_characteristic1_set_value声明
    猜你喜欢
    • 2013-04-12
    • 2016-08-25
    • 2019-05-08
    • 2023-02-11
    • 2015-10-01
    • 2019-02-11
    • 2015-10-18
    • 2016-01-24
    • 1970-01-01
    相关资源
    最近更新 更多