2017-04-18 75 views
1

有人可以告訴我如何使用DBUS api發送GATT通知。目前我正在使用bluez5.43。我正在嘗試註冊一項服務併發送通知。我已經拿到了工具目錄下的gatt-service.c的引用。當我查看源代碼時,該特性具有多種註冊的特徵方法。掉那些之一是dbus api發送GATT通知,藍色

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服務器通知?

回答

0

我有同樣的問題,我發現我的解決方法。

如果你的客戶是啓用通知在你的特點,以下兩行會設定特徵電流值和的BlueZ會處理它在堆棧,並通知所有用戶

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

可以,作爲一個例子,運行每隔X秒調用一次該函數的線程,並且每隔X秒會通知您的客戶端。

編輯:

GattCharacteristic1是C的DBus對象通過gdbus-CODEGEN從XML文件創建。 https://developer.gnome.org/gio/stable/gdbus-codegen.html

爲了幫助你,這是我根據BlueZ API文檔編寫的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> 

一旦你有你的XML文件(名爲org.bluez.GattCharacteristic1.xml),這說明你的BlueZ GATT對象,使用GBUS,代碼生成,生成一個 「C的DBus對象」

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

現在添加c和h文件到您的源代碼

以下行顯示如何,我呼籲的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); 
創建一個GATT特徵的BlueZ

請根據需要編輯標誌。在接口對象上保留一個指針,並使用我在第一個答案中提供的行。 GBus doc有很好的文檔記錄,所以我希望你能找到你需要的一切。

+0

嘿弗朗索瓦,感謝您的信息。你能提供更多細節,我找不到gatt_characteristic1_set_value api,它的聲明或定義在哪裏?你在這裏的界面是什麼意思? –

+0

我目前使用bluez5.43,我無法在代碼中找到gatt_characteristic1_set_value聲明 –