gpt4 book ai didi

使用 bluez gatt api 编译代码

转载 作者:太空狗 更新时间:2023-10-29 11:18:25 60 4
gpt4 key购买 nike

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

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

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

编辑:我使用的是 bluez 的最新稳定版本:5.32

来自 bluez 的 header ,我需要编译我的代码:

#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 的包含/库:

  • 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 中,我只需要链接这些库 + 添加 -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

无论如何,这可能不是最好的方法,但效果很好。此外,如果升级 bluez5 版本,则无需执行任何操作。我找到了一些其他解决方案,例如 fork bluez5,并在 Makefile 中进行了一些修改以构建一个静态库,其中包含我需要的所有内容。但是这个解决方案真的很痛苦,你需要在每个新的 bluez5 版本中更新你的工作。

关于使用 bluez gatt api 编译代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31672687/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com