低功耗蓝牙:在 linux 中监听通知/指示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15657007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:34:33  来源:igfitidea点击:

Bluetooth Low Energy: listening for notifications/indications in linux

linuxbluetoothbluetooth-lowenergybluezgatt

提问by Youssif Saeed

I'm trying to communicate with a BLE module through a Linux machine (the module is running a heart rate profile). So far, I've been able to do everything I need except listening for Notifications and indications (e.g. listening for the Heart Rate Measurement Notification). I'm using kernel version 3.5 and bluez-5.3.

我正在尝试通过 Linux 机器与 BLE 模块通信(该模块正在运行心率配置文件)。到目前为止,除了收听通知和指示(例如收听心率测量通知)之外,我已经能够做所有我需要的事情。我正在使用内核版本 3.5 和 bluez-5.3。

Succcessful commands used so far:

到目前为止使用的成功命令:

hcitool lescan
hcitool lecc
gatttool -b <Mac Address> --primary
gatttool -b <MAC Address> --characteristics
gatttool -b <MAC Address> --char-read
gatttool -b <MAC Address> --char-desc
gatttool -b <MAC Address> --interactive

Failed commands:

失败的命令:

gatttool -b <MAC Address> --listen

Any help is greatly appreciated.

任何帮助是极大的赞赏。

采纳答案by Tim Tisdall

Try this...

尝试这个...

Run gatttool -b <MAC Address> --interactivelike you did before. You'll get a prompt and then you type connect. You should see a CONin the prompt indicating that you've connected to the device. Then type char-read-uuid 2902. You should get a list of all CCC (Client Characteristic Configuration) attributes on the device. You can try setting them all to 0100 to get notifications, 0200 for indications, 0300 for both, or 0000 for everything off.Type helpto see all the commands and their arguments.

gatttool -b <MAC Address> --interactive像以前一样跑。你会得到一个提示,然后你输入connect. 您应该会CON在提示中看到,表明您已连接到设备。然后键入char-read-uuid 2902。您应该获得设备上所有 CCC(客户端特征配置)属性的列表。 您可以尝试将它们全部设置为 0100 以获取通知,0200 表示指示,0300 表示两者,或 0000 表示所有关闭。键入help以查看所有命令及其参数。

EDIT:

编辑:

The use of the --listenargument requires you to couple it with other commands to turn on the notifications and/or indications. So here's an example that works in Bluez 4.101:

--listen参数的使用要求您将其与其他命令结合使用以打开通知和/或指示。下面是一个适用于 Bluez 4.101 的示例:

gatttool -b <MAC Address> --char-write-req --handle=0x0031 --value=0100 --listen

Obviously you need to change the handle to the handle of the CCC that you want to turn on notifications for. However, I still find it way easier to just use the interactive mode.

显然,您需要将句柄更改为要为其打开通知的 CCC 的句柄。但是,我仍然发现仅使用交互模式更容易。

回答by RobinSingh

to retain the CCC value you need to pair the two device. once they are paired you do not need to set the CCC again. on next reconnection it will find the setting , which will be saved in thec FLASH of Key fob. try to configure after pairing via SMP.

要保留 CCC 值,您需要将两个设备配对。配对后,您无需再次设置 CCC。在下次重新连接时,它会找到设置,该设置将保存在密钥卡的 c FLASH 中。尝试通过 SMP 配对后进行配置。

回答by Fred.Taiwan

Final answer for reading heart rate on Mio Alpha :

在 Mio Alpha 上阅读心率的最终答案:

gatttool -b xx:xx:xx:xx:xx:xx -t random --char-write-req -a 0x0025 -n 0100 --listen

Characteristic value was written successfully
Notification handle = 0x0024 value: 10 4b 33 03 
Notification handle = 0x0024 value: 10 4b 33 03 
Notification handle = 0x0024 value: 10 4b 33 03 
Notification handle = 0x0024 value: 10 4a 3e 03 
Notification handle = 0x0024 value: 10 4c 28 03 28 03 
Notification handle = 0x0024 value: 10 4c 28 03 
Notification handle = 0x0024 value: 10 4b 33 03 
Notification handle = 0x0024 value: 10 4a 3e 03 3e 03

回答by jaredwolff

Looks like the older version of Bluez (hcitool & gatttool) don't allow you to write to Bluetooth Low Energy devices. I ended up installing a newer version (5.17 as of this writing) in order to enabled notifications, etc.

看起来旧版本的 Bluez(hcitool 和 gatttool)不允许您写入低功耗蓝牙设备。我最终安装了一个更新的版本(撰写本文时为 5.17)以启用通知等。

To get a list of all your handles you can run the following:

要获取所有句柄的列表,您可以运行以下命令:

char-desc

You can then read from a handle:

然后您可以从句柄中读取:

char-read-hnd 0x000e

(the above handle is for my nrf51822 battery level)

(以上手柄是我的nrf51822电池电量)

Where the handle is one from the list you got from char-desc.

句柄是您从列表中获得的一个 char-desc.

Just like Tim said above, you can write to the notification related handle to get indications or notifications. (in my case my device only had notifications)

就像上面 Tim 说的,你可以写通知相关的句柄来获取指示或通知。(就我而言,我的设备只有通知)

char-write-req 0x000f 0100

(the above handle is for my nrf51822 battery level notification)

(以上手柄是我的 nrf51822 电池电量通知)

In my case the battery notification shouldn't send anything until the battery level has changed.

就我而言,在电池电量发生变化之前,电池通知不应发送任何内容。

I wrote a pretty lengthy blog post on getting setup with Bluez. You can find it here: Get Started with Bluetooth Low EnergyFeel free to check it out!

我写了一篇关于使用 Bluez 进行设置的长篇博文。您可以在此处找到:低功耗蓝牙入门随时查看!