bluetoothctl是bluez协议的调试工具,用于前期的蓝牙基本功能的验证和测试。本文基于bluetoothctl工具,介绍了如何使用其进行蓝牙的基本通讯
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
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
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
hcitool lescan LE Scan ... 21:0E:38:14:CE:18 (unknown) 5F:5A:FC:85:1C:05 (unknown)
如果能够探测到低功耗蓝牙设备,代表蓝牙的基本功能正常
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
root@kylin:/home/kylin# bluetoothctl Agent registered [bluetooth]#
[bluetooth]# scan on
等到扫到自己的蓝牙设备之后,可以关闭蓝牙扫描
[bluetooth]# scan off
[bluetooth]# devices Device 54:09:10:38:28:97 iPhone
由上可以发现,设备已经搜到蓝牙名字为iPhone,MAC为 54:09:10:38:28:97 的设备
[bluetooth]# agent on Agent is already registered [bluetooth]# default-agent Default agent request successful
[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
[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]
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
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
[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 .
1.下载LightBlue 2.选择Virtual Devices 3.点击+ 4.选择默认的profile,这里默认可以是blank 5.点击Blank,进去服务 6.进入默认的uuid 2222 7.修改uuid,修改描述,修改属性 8.利用bluetoothctl进行读写操作验证