最近的BlueZ 5.46 发布有一个新功能“让应用程序获取专用文件描述符来从 GATT 特征读取数据/向 GATT 特征写入数据”。查看 bluetoothctl 命令帮助,看起来这应该可以使用新的获取-写入或者获取-通知命令,但到目前为止我还无法让这些工作。尝试将带有 bluez.x86_64 版本 5.46-1.fc26 的 Fedora 26(带有 bluetoothd 服务上的 --experimental 标志)连接到 HM-10 低功耗蓝牙模块的示例:
bluetoothctl -v
5.46
sudo bluetoothctl
[bluetooth]# scan on
Discovery started
[CHG] Controller 28:B2:BD:07:04:49 Discovering: yes
[NEW] Device D4:36:39:DE:61:6B KeyDuino
[bluetooth]# scan off
[bluetooth]# connect D4:36:39:DE:61:6B
Attempting to connect to D4:36:39:DE:61:6B
[CHG] Device D4:36:39:DE:61:6B Connected: yes
...
[KeyDuino]# select-attribute 0000ffe1-0000-1000-8000-00805f9b34fb
[KeyDuino:/service0010/char0011]# acquire-write
Failed to acquire write: org.freedesktop.DBus.Error.UnknownMethod
[KeyDuino:/service0010/char0011]# acquire-notify
Failed to acquire notify: org.freedesktop.DBus.Error.UnknownMethod
我不断得到无法获取写入:org.freedesktop.DBus.Error.UnknownMethod当我尝试使用新命令时出现错误消息。如何获取 BLE 特性的文件描述符?
否则,连接可以使用通知和写入命令进行工作:
[KeyDuino:/service0010/char0011]# notify on
[CHG] Attribute /org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011 Notifying: yes
Notify started
[CHG] Attribute /org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011 Value: 0x33
[CHG] Attribute /org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011 Value: 0x32
[CHG] Attribute /org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011 Value: 0x31
[KeyDuino:/service0010/char0011]# write 0x30 0x31 0x32
Attempting to write /org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011
答案1
嗯...所以当我认为我正在使用 --experimental 标志运行蓝牙时,我一定没有保存更改。要启动具有实验性功能的守护进程:
- 编辑 .service 文件以在启动命令中包含 --experimental 或 -E
- systemctl 守护进程重新加载
- systemctl 重新启动蓝牙
接下来确保守护进程正在使用正确的标志运行:
然后,当我尝试使用 acquire-write 或 acquire-notify 时,我收到一条新的错误消息,并且守护进程似乎崩溃了:
[KeyDuino:/service0010/char0011]# acquire-write
Agent unregistered
[DEL] Controller 28:B2:BD:07:04:49 GBFedora01 [default]
[DEL] Primary Service0/char0011]#
/org/bluez/hci0/dev_D4_36_39_DE_61_6B/service000c
00001801-0000-1000-8000-00805f9b34fb
Generic Attribute Profile
[DEL] Characteristic
/org/bluez/hci0/dev_D4_36_39_DE_61_6B/service000c/char000d
00002a05-0000-1000-8000-00805f9b34fb
Service Changed
[DEL] Descriptor
/org/bluez/hci0/dev_D4_36_39_DE_61_6B/service000c/char000d/desc000f
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[DEL] Primary Service
/org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010
0000ffe0-0000-1000-8000-00805f9b34fb
Unknown
[DEL] Characteristic
/org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011
0000ffe1-0000-1000-8000-00805f9b34fb
Unknown
[DEL] Descriptor
/org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011/desc0013
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
[DEL] Descriptor
/org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011/desc0014
00002901-0000-1000-8000-00805f9b34fb
Characteristic User Description
Failed to acquire write: org.freedesktop.DBus.Error.NoReply
Waiting to connect to bluetoothd...
我还收到一个弹出窗口,指示检测到两个 SELinux 问题:
故障排除步骤提供了启用请求的权限的命令:
ausearch -c 'dbus-daemon' --raw | audit2allow -M my-dbusdaemon
semodule -X 300 -i my-dbusdaemon.pp
重新启动蓝牙服务后,我可以成功获取文件读/写描述符:
[KeyDuino:/service0010/char0011]# acquire-write
[CHG] Attribute /org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011 WriteAcquired: yes
AcquireWrite success: fd 7 MTU 23
[KeyDuino:/service0010/char0011]# acquire-notify
[CHG] Attribute /org/bluez/hci0/dev_D4_36_39_DE_61_6B/service0010/char0011 NotifyAcquired: yes
AcquireNotify success: fd 8 MTU 23
然后,您可以在 bluetoothctl 的 /proc/(pid)/fd/ 文件夹中找到这些文件描述符。
#Use cat to read output
sudo cat /proc/$(pgrep bluetoothctl)/fd/8
#In another root shell use cat to write to BLE characteristic
sudo -sE
cat > /proc/$(pgrep bluetoothctl)/fd/7