我目前正在使用C语言中GATT中的bluez5 api实现 BLE 服务器。我需要使用自己的具有自定义特征的服务。

问题是bluez5没有安装GATT api的所有 header 。 libbluetooth中的相同问题并未提供所有外部GATT API。

我使用了错误的API吗?编译代码的技巧是什么?
当前的肮脏解决方案是用我自己的代码替换btgatt-server.c源工具目录中的bluez,以便能够对我的实现进行dev/test

编辑 :
我正在使用bluez的最后一个稳定版本:5.32
我需要编译的bluez头文件:

#include "lib/bluetooth.h"
#include "lib/hci.h"
#include "lib/hci_lib.h"
#include "lib/l2cap.h"
#include "lib/uuid.h"

#include "src/shared/mainloop.h"
#include "src/shared/util.h"
#include "src/shared/att.h"
#include "src/shared/queue.h"
#include "src/shared/timeout.h"
#include "src/shared/gatt-db.h"
#include "src/shared/gatt-server.h"

职能 :
[arthur ] make 2>&1 | grep gatt_ | grep implicit
tools/btgatt-server.c:32:2: error: implicit declaration of function ‘gatt_db_attribute_read_result’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:60:2: error: implicit declaration of function ‘gatt_db_add_service’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:67:2: error: implicit declaration of function ‘gatt_db_service_add_characteristic’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:94:2: error: implicit declaration of function ‘gatt_db_service_set_active’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:110:2: error: implicit declaration of function ‘bt_gatt_server_unref’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:111:2: error: implicit declaration of function ‘gatt_db_unref’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:186:2: error: implicit declaration of function ‘gatt_db_new’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:192:2: error: implicit declaration of function ‘bt_gatt_server_new’ [-Werror=implicit-function-declaration]
tools/btgatt-server.c:202:2: error: implicit declaration of function ‘bt_gatt_server_set_debug’ [-Werror=implicit-function-declaration]

这些包括不是由bluez的Makefile在我的系统上安装的。而且库文件也不包含我需要的功能。

最佳答案

稍后我可能会使用dbus API。但是现在我一直在使用bluez5的低c API(bluez5的示例路径:bluez5_utils-5.31/tools/btgatt-server.c)。为了在bluez5源目录之外编译此独立代码,我使用了bluez5的一些include/library:

  • libshared-mainloop.a
  • libbluetooth-internal.a

  • 就我而言,我正在使用buildroot。所以我在bluez5 mk文件中添加了一些代码来获取这个缺少的库:
    define BLUEZ5_UTILS_EXTERNALIZE_GATT
        mkdir -p $(STAGING_DIR)/usr/local/bluez5/gatt
        cp $(BLUEZ5_UTILS_SRCDIR)/src/uuid-helper.o $(STAGING_DIR)/usr/local/bluez5/gatt/.
        cp $(BLUEZ5_UTILS_SRCDIR)/src/.libs/libshared-mainloop.a $(STAGING_DIR)/usr/local/bluez5/gatt/.
        cp $(BLUEZ5_UTILS_SRCDIR)/lib/.libs/libbluetooth-internal.a $(STAGING_DIR)/usr/local/bluez5/gatt/.
    
        $(INSTALL) -D -m 0755 $(@D)/tools/btmgmt $(STAGING_DIR)/usr/bin/btmgmt
        $(INSTALL) -D -m 0755 $(@D)/tools/btmgmt $(TARGET_DIR)/usr/bin/btmgmt
    endef
    

    然后在我的Makefile中,我只需要链接这些库+ add -I即可找到位于bluez5源目录中的丢失的头文件:
    # find bluez5 header
    HEADER += -I$(BLUEZ_PATH)
    
    # link bluez5 libraries
    BLUEZ5_LIB = $(STAGING_DIR)/usr/local/bluez5/gatt/libshared-mainloop.a BLUEZ5_LIB += $(STAGING_DIR)/usr/local/bluez5/gatt/libbluetooth-internal.a
    

    无论如何,这可能不是最好的方法,但是效果很好。此外you don't need to do anything if you upgrade your bluez5 version。我找到了其他一些解决方案,例如fork bluez5,并在Makefile中进行了一些修改,以构建包含我需要的所有内容的静态库。但是此解决方案确实很痛苦,您需要在每个新的bluez5版本中更新您的工作。

    10-01 06:42