低功耗蓝牙:使用 BlueZ 堆栈作为外围设备(具有自定义服务和特性) [英] Bluetooth Low Energy: Use BlueZ stack as a peripheral (with custom services and characteristics)

查看:17
本文介绍了低功耗蓝牙:使用 BlueZ 堆栈作为外围设备(具有自定义服务和特性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use the BlueZ stack on a Linux machine to create a GATT server with custom services and characteristics. The final goal is to use any central device (e.g. iOS or Android device) to connect to the GATT server, discover the services and characteristics, and manipulate the data in the characteristics.

示例:

  • 具有 1 个服务的外设,其中包含 3 个特征.
  • 服务 uuid = 0xFFFF
  • 字符 1 uuid = 0xAAAA,属性 = 可读
  • Char 2 uuid = 0xBBBB,属性 = 可读 &可写
  • 字符 3 uuid = 0xCCCC,属性 = 可通知

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

From the central device, I should see the the peripheral device, connect to it and discover one service (0xFFFF) which has three characteristics (0xAAAA, 0xBBBB, 0xCCCC). I should then be able to read the value of 0xAAAA, read and write to the value of 0xBBBB, and enable notifications on 0xCCCC.

请注意,我知道有一个类似的问题 存在,但它仅解释了如何将外围设备用作广告商.另一个已解决的问题解释了如何创建 GATT 服务器,但没有解释如何使用特征的属性(例如可读性、可通知性等),或者我可能遗漏了一些东西.

Please note that I am aware that a similar question exists, but it only explains how to use the peripheral as an advertiser. Another solved question explains how to create a GATT server, but does not explain how to play with the properties of the characteristics (e.g. readable, notifiable, etc.), or maybe I'm missing something.

先谢谢你.

推荐答案

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

You can see gatt-example practice, or defined profiles under profile/ directory such as alert/server.c. Basically, you just have to register your service using gatt_service_add() function, following the existing code. For example :

 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"来启用实验(和维护者模式?)

Also, I forgot the details but in order to get alert server working, you need to enable experimental (and maintainer mode?) during configuration by adding "--enable-maintainer-mode" and "--enable-experimental"

要运行,请使用 -n 和 -d 选项运行已编译的bluetoothd"以进行调试(还有 -E 用于启用实验性服务).您可能想在运行 bluetoothd 后再次重置您的适配器.然后您可以使用 gatttool 从远程设备连接(也可以在远程设备上运行 bluetoothd).

To run, run the compiled "bluetoothd" with -n and -d options to debug (also -E for enabling experimental services). You may want to reset your adapter again after running bluetoothd. And then you can connect from remote device using gatttool (also with bluetoothd running on remote device).

这篇关于低功耗蓝牙:使用 BlueZ 堆栈作为外围设备(具有自定义服务和特性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆