2

我正在做一个项目,我需要使用 C++ 代码在两个 Raspberry Pi 3 on stretch (RPi) 之间无线创建通信,并具有回调功能。第一个应该向第二个发送数据。我决定使用板载蓝牙芯片在两者之间建立蓝牙连接。

所以首先我决定从源代码版本 5.50 在两个 RPi 上编译 Linux Bluetooth Stack: Bluez。遵循本教程:https : //learn.adafruit.com/install-bluez-on-the-raspberry-pi/installation on Adafruit。我设法使用以下代码从一个 pi 向另一个 pi 发送消息:https ://people.csail.mit.edu/albert/bluez-intro/x502.html 。但是要读取客户端发送的消息,我必须调用函数“read(client, buf, sizeof(buf))”,如服务器代码所示,这有点限制。我想在收到消息时收到通知,以便可以调用回调函数。我看到可以通过启用有关用于通信的 GATT 特性的通知来完成。

我从https://people.csail.mit.edu/albert/bluez-intro/x502.html获得的 C++ 代码并没有真正显示这种级别的规范。在通过以下方式提高了我对 GATT 的了解后:https ://www.novelbits.io/bluetooth-gatt-services-characteristics/ ,我决定按照 Tony D 的 youtube 教程了解如何使用 Bluez APi https://www.youtube .com/watch?v=5fQR2PHMDWE

为了方便起见,我决定将一个 RPi 放在一边,并尝试连接一个使用蓝牙 4.0 的蓝牙音频耳机 JBL T450BT,因此它应该支持蓝牙低功耗。

在 bluetoothctl 上,这是问题所在,我设法打开控制器电源,扫描并检测耳机。然后我信任、配对和连接。在 GATT 菜单中,我输入命令:list-attributes [dev address]。Bluetoothctl 不打印任何错误但不返回任何内容,并准备接收其他命令行。

sudo bluetoothctl
[bluetooth]# show
Controller XX:XX:XX:XX:XX:XX (public)
Name: VoitureRPi
Alias: VoitureRPi
Class: 0x004c0000
Powered: yes
Discoverable: no
Pairable: yes
UUID: Headset AG                (00001112-0000-1000-8000-00805f9b34fb)
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
UUID: Handsfree Audio Gateway   (0000111f-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0532
Discovering: no
[bluetooth]# scan on
Device 78:44:05:BC:12:0C JBL T450BT
[bluetooth]# trust 78:44:05:BC:12:0C 
[CHG] Device 78:44:05:BC:12:0C Trusted: yes
Changing 78:44:05:BC:12:0C trust succeeded
[bluetooth]# pair 78:44:05:BC:12:0C 
Attempting to pair with 78:44:05:BC:12:0C
[CHG] Device 78:44:05:BC:12:0C Connected: yes
[CHG] Device 78:44:05:BC:12:0C UUIDs: 00001108-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device 78:44:05:BC:12:0C ServicesResolved: yes
[CHG] Device 78:44:05:BC:12:0C Paired: yes
Pairing successful
[CHG] Device 78:44:05:BC:12:0C ServicesResolved: no
[CHG] Device 78:44:05:BC:12:0C Connected: no
[bluetooth]# connect 78:44:05:BC:12:0C 
Attempting to connect to 78:44:05:BC:12:0C
[CHG] Device 78:44:05:BC:12:0C Connected: yes
Connection successful
[CHG] Device 78:44:05:BC:12:0C ServicesResolved: yes
[JBL T450BT]# menu gatt
[JBL T450BT]# list-attributes 78:44:05:BC:12:0C 
[JBL T450BT]# 

正如我告诉你的那样,从那里我没有得到 bluetoothctl 的答案,但也没有错误。我无法发现服务/特征。在我关注的 youtube tuto 上,有人告诉我 GATT 服务和特征发现是在配对/第一次连接期间完成的。我假设 Pi 打算这样做但失败了。这可能就是我得到序列的原因:已连接:是,ServicesResolved:是配对成功,然后是 ServicesResolved:否,已连接否。没有类似:[NEW] XXX 服务或 Characteristcs 显示。我认为问题不在于耳机,因为它在我连接的其他设备上做同样的事情。我在 RPi 上安装了 pulseaudio 和 bluealsa 软件包。

正如本教程中提到的

这里是蓝牙状态:

systemctl status bluetooth
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-11-08 11:29:38 CET; 27min ago
Docs: man:bluetoothd(8)
Main PID: 2646 (bluetoothd)
Status: "Running"
CGroup: /system.slice/bluetooth.service
     └─2646 /usr/local/libexec/bluetooth/bluetoothd --experimental

这是 hciconfig 给出的:

hciconfig -a
hci0:   Type: Primary  Bus: UART
BD Address: B8:27:EB:A8:0C:20  ACL MTU: 1021:8  SCO MTU: 64:1
UP RUNNING 
RX bytes:11205 acl:76 sco:0 events:520 errors:0
TX bytes:219869 acl:433 sco:0 commands:150 errors:0
Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 
Link policy: RSWITCH SNIFF 
Link mode: SLAVE ACCEPT 
Name: 'VoitureRPi'
Class: 0x4c0000
Service Classes: Rendering, Capturing, Telephony
Device Class: Miscellaneous, 
HCI Version: 4.1 (0x7)  Revision: 0x168
LMP Version: 4.1 (0x7)  Subversion: 0x2209
Manufacturer: Broadcom Corporation (15)

这也是 D-Bus 内省工具返回的内容:

busctl tree org.bluez
└─/org
  └─/org/bluez
     └─/org/bluez/hci0
        └─/org/bluez/hci0/dev_78_44_05_BC_12_0C
          ├─/org/bluez/hci0/dev_78_44_05_BC_12_0C/fd1
          └─/org/bluez/hci0/dev_78_44_05_BC_12_0C/player0

对于在配对和连接方面遇到问题的人来说,这个来源是一个巨大的帮助:https ://www.sigmdel.ca/michel/ha/rpi/bluetooth_02_en.html

无论如何,这是我的问题。我想访问特性及其 UUID,以便能够修改他们的权限或创建具有适合我项目的自身特性的新服务。到目前为止,我认为这是在蓝牙通信中实现带有回调的通知器的唯一方法。也许我错了。你有什么线索吗?:)

4

2 回答 2

1

感谢 A.Baur,“--experimental”选项是最后一个缺失的东西。
我的系统:Debian 10 + KDE
已安装:
- bluez-*
- bluetooth
- pulseaudio-module-bluetooth

步骤:
1. 停止蓝牙服务:>sudo systemctl stop bluetooth
2. 修改: /etc/init.d/bluetooth ;通过添加到该行:

SSD_OPTIONS="**--experimental** --oknodo --quiet --exec $DAEMON -- $NOPLUGIN_OPTION"

3. 启动蓝牙服务: 4. 通过或其他 GUI 工具>sudo systemctl start bluetooth
连接到 Bose QC35 II 。bluetoothctl

于 2019-08-19T22:30:27.950 回答
0

我使用来自 bluez-5.50 的广告和 gatt 服务器的 python 示例在 RPi 中工作。我使用 nRF 工具对其进行了测试。

于 2019-12-26T04:43:27.140 回答