蓝牙低功耗:使用BlueZ堆栈作为外设(具有自定义服务和特性)

时间:2014-01-29 10:37:08

标签: linux bluetooth bluetooth-lowenergy bluez gatt

我正在尝试在Linux机器上使用BlueZ堆栈来创建具有自定义服务和特征的GATT服务器。最终目标是使用任何中央设备(例如iOS或Android设备)连接到GATT服务器,发现服务和特征,并操纵特征中的数据。

示例:

  • 具有1项服务的外围设备,包含3个特征。
  • 服务uuid​​ = 0xFFFF
  • Char 1 uuid = 0xAAAA,properties = readable
  • Char 2 uuid = 0xBBBB,properties = readable&可写
  • Char 3 uuid = 0xCCCC,properties = notifiable

从中央设备,我应该看到外围设备,连接到它并发现一个具有三个特征(0xAAAA,0xBBBB,0xCCCC)的服务(0xFFFF)。然后,我应该能够读取0xAAAA的值,读取和写入0xBBBB的值,并在0xCCCC上启用通知。

请注意,我知道存在similar question,但它只解释了如何将外围设备用作广告客户。 Another solved question解释了如何创建GATT服务器,但没有解释如何使用特征的属性(例如可读,可通知等),或者我可能遗漏了某些东西。

提前谢谢你。

3 个答案:

答案 0 :(得分:9)

您可以在profile / server.c下查看gatt-example practice或定义的配置文件,例如alert / server.c。基本上,您只需使用gatt_service_add()函数注册您的服务,遵循现有代码。例如:

 gatt_service_add(adapter, GATT_PRIM_SVC_UUID, 0xFFFF,
    /* Char 1 */
    GATT_OPT_CHR_UUID16, 0xAAAA,
    GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_READ,
    GATT_OPT_CHR_VALUE_CB, ATTRIB_READ, read_func_callback,

    /* Char 2 Define here */
    ...
    /* Char 3 Define here */
    ...
    GATT_OPT_INVALID);
 }

另外,我忘记了细节,但为了让警报服务器正常工作,您需要在配置期间启用实验(和维护者模式?),添加“--enable-maintainer-mode”和“--enable-experimental”

要运行,请使用-n和-d选项运行已编译的“bluetoothd”进行调试(同时启用-E以启用实验服务)。您可能希望在运行bluetoothd后再次重置适配器。然后你可以使用gatttool从远程设备连接(也可以在远程设备上运行蓝牙)。

答案 1 :(得分:4)

1)转到Bluez文件夹

2)sudo ./configure --prefix = / usr --mandir = / usr / share / man --sysconfdir = / etc --localstatedir = / var --disable-systemd --enable-experimental --enable -maintainer模式

3)sudo make all

4)发布可连接数据包

# activate bluetooth
sudo hciconfig hci0 up                                             
# set advertise data: "hello world"
sudo hcitool -i hci0 cmd 0x08 0x0008 48 45 4c 4c 4f 57 4f 52 4c 44
# start advertising as connectable
sudo hciconfig hci0 leadv 0

5)sudo service bluetooth stop

6)sudo src / bluetoothd -d -n

7)从其他PC输入(更改MAC id gatt服务器mac)

gatttool -b  gatt_server_mac --interactive

第6步是为了您要编译 plugins / gatt-example.c

如果您想从个人资料/时间个人/警告编译服务器 c (替换为警示代替时间)或个人资料文件夹中的任何其他文件替换第6步

sudo src / bluetoothd --plugin = time -n

答案 2 :(得分:1)

Bluetoothctl命令行工具现在提供了另一个解决方案。可以在这里找到更多详细信息:-

BlueZ: How to set up a GATT server from the command line

我不确定何时引入Bluetoothctl,但是在撰写本文时它是相对较新的,因此在以前的BlueZ版本中不存在。我正在使用BlueZ v5.50,这是我对其进行测试的版本。