05 蓝牙
GM-3568JHF 开发板集成了 RTL8723DU 无线通信模块,该模块不仅提供 WIFI 功能,同时还集成了蓝牙功能。蓝牙模块支持经典蓝牙和低功耗蓝牙(BLE)协议,为开发板提供了丰富的短距离无线通信能力。
1 蓝牙模块概述
1.1 RTL8723DU 蓝牙技术规格
| 参数 | 规格 |
|---|---|
| 芯片型号 | Realtek RTL8723DU |
| 蓝牙版本 | Bluetooth 4.2 |
| 协议支持 | Classic Bluetooth + BLE (Bluetooth Low Energy) |
| 工作频段 | 2.4GHz ISM 频段 (2.402~2.480GHz) |
| 调制方式 | FHSS (跳频扩频) |
| 发射功率 | Class 2 (最大 4dBm) |
| 接收灵敏度 | -90dBm @ 0.1% BER |
| 传输距离 | 10 米 (Class 2) |
| 数据速率 | 最大 3Mbps (EDR) |
1.2 驱动和软件支持
Linux 蓝牙协议栈
- BlueZ: Linux 标准蓝牙协议栈
- 内核支持: 内核原生驱动支持
- 用户空间工具: bluetoothctl, hciconfig, hcitool 等
- D-Bus 接口: 标准 D-Bus API 接口
支持的应用场景
- 音频设备: 蓝牙耳机、音箱连接
- 输入设备: 蓝牙键盘、鼠标
- 文件传输: 与手机、电脑文件互传
- 串口通信: 蓝牙串口数据传输
- IoT 应用: BLE 传感器数据采集
2 蓝牙连接配置
开发板已适配 RTL8723DU 模块,蓝牙驱动和协议栈已完成适配配置。
2.1 连接测试
测试需要一部智能手机(该小节测试使用的是安卓手机),打开手机蓝牙功能,并且打开开发板蓝牙功能,然后进行连接,如下图所示:

开发板端:

手机端:

2.2 数据收发测试
首先在 home/linaro 文件夹下新建一个 bluetooth_test.txt 文件,内容为:bluetooth_test

1)文件发送
开发板通过蓝牙将 bluetooth_test.txt 发送到手机,手机接收
开发板端:

手机端:

查看文件内容是否一致:

2)文件接收
手机通过蓝牙将 bluetooth_test.txt 发送到开发板,开发板接收
手机端:


开发板端:


2.3 命令行蓝牙配置
hciconfig 和 hcitool
# 查看蓝牙适配器信息
hciconfig -a
# 启用蓝牙适配器
sudo hciconfig hci0 up
# 设置可发现模式
sudo hciconfig hci0 piscan
# 扫描附近设备
hcitool scan
# 查看设备信息
hcitool info XX:XX:XX:XX:XX:XX
# 检查连接状态
hcitool con配对和认证
# 使用 simple-agent 配对
sudo simple-agent hci0 XX:XX:XX:XX:XX:XX
# 或使用 bluez-simple-agent
bluez-simple-agent hci0 XX:XX:XX:XX:XX:XX3 蓝牙音频应用
3.1 连接蓝牙音频设备
安装音频支持包
# 安装 PulseAudio 蓝牙模块
sudo apt-get install pulseaudio-module-bluetooth
# 重启 PulseAudio
pulseaudio -k
pulseaudio --start配置音频设备
# 查看音频设备
pactl list short sinks
# 设置默认音频输出
pactl set-default-sink bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink
# 播放测试音频
speaker-test -t wav -c 2
# 使用 aplay 播放音频文件
aplay /usr/share/sounds/alsa/Front_Left.wav3.2 A2DP音频流传输
# 连接 A2DP 设备后,检查音频配置
bluetoothctl
[bluetooth]# info XX:XX:XX:XX:XX:XX
# 在 PulseAudio 中查看蓝牙音频设备
pactl list sinks | grep -A 10 bluez
# 播放音频到蓝牙设备
mpg123 -a bluez_sink.XX_XX_XX_XX_XX_XX.a2dp_sink music.mp34 BLE (低功耗蓝牙) 应用
BLE 设备扫描和连接
使用 gatttool
# 扫描 BLE 设备
sudo hcitool lescan
# 连接 BLE 设备
gatttool -b XX:XX:XX:XX:XX:XX -I
# 在 gatttool 交互模式中:
[XX:XX:XX:XX:XX:XX][LE]> connect
[XX:XX:XX:XX:XX:XX][LE]> primary # 列出主要服务
[XX:XX:XX:XX:XX:XX][LE]> characteristics # 列出特征值
[XX:XX:XX:XX:XX:XX][LE]> char-read-hnd 0x0010 # 读取特征值
[XX:XX:XX:XX:XX:XX][LE]> char-write-req 0x0010 0100 # 写入特征值使用 bluetoothctl 进行 BLE 操作
bluetoothctl
[bluetooth]# scan on
[bluetooth]# connect XX:XX:XX:XX:XX:XX
[bluetooth]# gatt list-attributes
[bluetooth]# gatt select-attribute /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0010/char0011
[bluetooth]# gatt read
[bluetooth]# gatt write "0x01 0x02"5 故障排除
5.1 蓝牙模块无法识别
检查步骤:
# 检查 USB 设备
lsusb | grep Realtek
# 检查蓝牙适配器
hciconfig -a
# 检查内核模块
lsmod | grep bluetooth
# 手动加载蓝牙模块
sudo modprobe bluetooth
sudo modprobe btusb
# 检查蓝牙服务状态
sudo systemctl status bluetooth5.2 设备配对失败
可能原因和解决方案:
# 重置蓝牙适配器
sudo hciconfig hci0 reset
# 清除配对缓存
sudo rm -rf /var/lib/bluetooth/*/cache
# 重启蓝牙服务
sudo systemctl restart bluetooth
# 检查认证代理
ps aux | grep agent5.3 音频连接问题
解决步骤:
# 检查 PulseAudio 模块
pactl list modules | grep bluetooth
# 重新加载蓝牙模块
pactl unload-module module-bluetooth-discover
pactl load-module module-bluetooth-discover
# 检查音频配置文件
bluetoothctl
[bluetooth]# info XX:XX:XX:XX:XX:XX5.4 连接不稳定
优化建议:
# 检查信号强度
hcitool rssi XX:XX:XX:XX:XX:XX
# 调整功率管理
echo 'SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="d723", ATTR{power/autosuspend}="-1"' | sudo tee /etc/udev/rules.d/50-usb-bluetooth.rules
# 重启 udev 服务
sudo udevadm control --reload-rules5.5 文件传输失败
检查和修复:
# 检查 OBEX 服务
sudo systemctl status obex
# 安装 OBEX 工具
sudo apt-get install obexftp obex-data-server
# 启动 OBEX 服务
obex-data-server --auto-accept