编辑
2025-03-03
工作知识
0
请注意,本文编写于 95 天前,最后修改于 95 天前,其中某些信息可能已经过时。

目录

一:确保系统蓝牙正常存在并工作
1.1 确定蓝牙bluez协议正常安装,并高于5.0
1.2 查看蓝牙控制器是否正常启动
1.3 确定蓝牙功能是否被rfkill block
1.4 测试并验证低功耗蓝牙是否正常
1.5 确定蓝牙正常可以ping通
二:使用bluetoothctl工具
2.1 进入交互终端
2.2 开启扫描
2.3 查看已经扫到的设备
2.4 开启代理
2.5 信任和配对
2.6 连接蓝牙
2.7 查看属性协议
2.8 选择自定义的属性
2.9 读和写
三 使用手机LightBlue测试蓝牙

bluetoothctl是bluez协议的调试工具,用于前期的蓝牙基本功能的验证和测试。本文基于bluetoothctl工具,介绍了如何使用其进行蓝牙的基本通讯

一:确保系统蓝牙正常存在并工作

1.1 确定蓝牙bluez协议正常安装,并高于5.0

root@kylin:/home/kylin# dpkg -l | grep bluez ii bluez 5.53-0kylin3k4 arm64 Bluetooth tools and daemons ii bluez-obexd 5.53-0kylin3k4 arm64 bluez obex daemon

1.2 查看蓝牙控制器是否正常启动

root@kylin:/home/kylin# hciconfig hci0: Type: Primary Bus: UART BD Address: 9C:71:D9:BC:DD:BF ACL MTU: 1021:8 SCO MTU: 64:1 UP RUNNING PSCAN RX bytes:441417 acl:200 sco:0 events:10560 errors:0 TX bytes:12117 acl:184 sco:0 commands:173 errors:0

如果蓝牙状态不是 “UP RUNNING PSCAN” 则需要手动启动蓝牙

hciconfig hci0 up

1.3 确定蓝牙功能是否被rfkill block

root@kylin:/home/kylin# rfkill list 0: bt_default: Bluetooth Soft blocked: no Hard blocked: no 1: phy0: Wireless LAN Soft blocked: no Hard blocked: no 2: brcmfmac-wifi: Wireless LAN Soft blocked: no Hard blocked: no 3: hci0: Bluetooth Soft blocked: no Hard blocked: no

如果蓝牙被rfkill block,则需要通过rfkill命令unblock,否则蓝牙功能仍是被系统关闭的

rfkill unblock hci0

1.4 测试并验证低功耗蓝牙是否正常

hcitool lescan LE Scan ... 21:0E:38:14:CE:18 (unknown) 5F:5A:FC:85:1C:05 (unknown)

如果能够探测到低功耗蓝牙设备,代表蓝牙的基本功能正常

1.5 确定蓝牙正常可以ping通

root@kylin:/home/kylin# l2ping 54:09:10:38:28:97 Ping: 54:09:10:38:28:97 from 9C:71:D9:BC:DD:BF (data size 44) ... 0 bytes from 54:09:10:38:28:97 id 0 time 9.91ms 0 bytes from 54:09:10:38:28:97 id 1 time 8.55ms 0 bytes from 54:09:10:38:28:97 id 2 time 8.48ms

二:使用bluetoothctl工具

2.1 进入交互终端

root@kylin:/home/kylin# bluetoothctl Agent registered [bluetooth]#

2.2 开启扫描

[bluetooth]# scan on

等到扫到自己的蓝牙设备之后,可以关闭蓝牙扫描

[bluetooth]# scan off

2.3 查看已经扫到的设备

[bluetooth]# devices Device 54:09:10:38:28:97 iPhone

由上可以发现,设备已经搜到蓝牙名字为iPhone,MAC为 54:09:10:38:28:97 的设备

2.4 开启代理

[bluetooth]# agent on Agent is already registered [bluetooth]# default-agent Default agent request successful

2.5 信任和配对

[bluetooth]# trust 54:09:10:38:28:97 [CHG] Device 54:09:10:38:28:97 Trusted: yes Changing 54:09:10:38:28:97 trust succeeded [bluetooth]# pair 54:09:10:38:28:97 Attempting to pair with 54:09:10:38:28:97 [CHG] Device 54:09:10:38:28:97 Connected: yes Request confirmation [agent] Confirm passkey 886360 (yes/no): yes

如果之前之前配对过,就无需配对,如果仍选择配对,会报如下错误,但不影响使用

[bluetooth]# pair 54:09:10:38:28:97 Attempting to pair with 54:09:10:38:28:97 Failed to pair: org.bluez.Error.AlreadyExists

查看已经配对过的设备

[iPhone]# paired-devices Device 54:09:10:38:28:97 iPhone

2.6 连接蓝牙

[bluetooth]# connect 54:09:10:38:28:97 Attempting to connect to 54:09:10:38:28:97 [CHG] Device 54:09:10:38:28:97 Connected: yes [iPhone]#

如果蓝牙正常连接成功,会出现蓝牙名字的终端,如 [iPhone]

2.7 查看属性协议

menu gatt [Blank]# list-attributes Characteristic (Handle 0x9240) /org/bluez/hci0/dev_60_B7_4C_51_AC_D7/service0014/char0015 00002222-0000-1000-8000-00805f9b34fb Unknown

2.8 选择自定义的属性

select-attribute /org/bluez/hci0/dev_60_B7_4C_51_AC_D7/service0023/char0024 [iPhone:/service0014/char0015]# attribute-info Characteristic - Unknown UUID: 00002222-0000-1000-8000-00805f9b34fb Service: /org/bluez/hci0/dev_60_B7_4C_51_AC_D7/service0014 Flags: read Flags: write Flags: extended-properties Flags: reliable-write

2.9 读和写

[iPhone:/service0023/char0024]# write 1 Attempting to write /org/bluez/hci0/dev_60_B7_4C_51_AC_D7/service0023/char0024 [iPhone:/service0023/char0024]# read Attempting to read /org/bluez/hci0/dev_60_B7_4C_51_AC_D7/service0023/char0024 [CHG] Attribute /org/bluez/hci0/dev_60_B7_4C_51_AC_D7/service0023/char0024 Value: 01 . 01 .

三 使用手机LightBlue测试蓝牙

1.下载LightBlue 2.选择Virtual Devices 3.点击+ 4.选择默认的profile,这里默认可以是blank 5.点击Blank,进去服务 6.进入默认的uuid 2222 7.修改uuid,修改描述,修改属性 8.利用bluetoothctl进行读写操作验证