树莓派笔记--蓝牙应用
文章摘要: 蓝牙通信分为两种:经典蓝牙和低功耗蓝牙。
早期的蓝牙通信方式称为经典蓝牙(classic bluetooth),经典蓝牙中的数据传输协议是串行仿真协议RFCOMM,RFCOMM仿真了常见的串口连接,数据从一端输入,从另一端取出。经典蓝牙的开发非常简单。基于串口开发的有线键鼠程序,就可以直接用于RFCOMM连接的无线键鼠。此外,经典蓝牙可以快速传输数据。因此,诺基亚N95这样的早期智能手机,也用RFCOMM来互传图片和文件。
经典蓝牙的缺点是比较耗电。后来,诺基亚发明了一种可以降低功耗的蓝牙通信方式。2010年出台的蓝牙4.0把这种通信方式规范为“低功耗蓝牙”(BLE,Bluetooth Low Energy)。BLE把通信双方分为非对称的双方,尽量让其中的一方承担主要的开销,减少另一方的负担。举例来说,手环电量少,而且需要长时间待机。BLE通信的主要负担可以放在电量较充裕且充电方便的手机一侧,从而减少手环的能耗。
硬件平台: raspi4
蓝牙模块:
命令操作: bluetoothctl命令进入蓝牙命令模式,显示蓝牙提示符
$ bluetoothctl
Agent registered
[bluetooth]#
命令列表:
打开蓝牙电源: power on
关闭蓝牙电源: power off
设置搜索可见: discoverable on
显示设备列表:
[bluetooth]# list
Controller DC:A6:32:A5:58:F2 raspberrypi [default]
搜索周边设备:
[bluetooth]# scan on
Discovery started
[CHG] Controller DC:A6:32:A5:58:F2 Discovering: yes
[NEW] Device 6A:DA:D3:A0:EC:91 6A-DA-D3-A0-EC-91
[NEW] Device F0:63:F9:4D:E1:39 SMU********000206
[NEW] Device FC:58:FA:E2:E4:3C Baseus COVO S17 Pro
[NEW] Device 4B:6B:12:4F:CE:8A 4B-6B-12-4F-CE-8A
[NEW] Device 50:EB:71:14:75:D4 DESKTOP-5N3BTUQ
[NEW] Device 90:F0:52:68:E5:95 @xiaoxin
配对设备:
[bluetooth]# pair FC:58:FA:E2:E4:3C
查看已配对设备:
[bluetooth]# paired-devices
Device FC:58:FA:E2:E4:3C Baseus COVO S17 Pro
Device 94:0E:6B:63:22:F9 Honor 8 Lite
连接设备: 连接成功后更改命令提示符
[bluetooth]# connect FC:58:FA:E2:E4:3C
Attempting to connect to FC:58:FA:E2:E4:3C
[CHG] Device FC:58:FA:E2:E4:3C Connected: yes
Connection successful
[CHG] Device FC:58:FA:E2:E4:3C ServicesResolved: yes
[Baseus COVO S17 Pro]#
断开连接:断开连接后,恢复命令提示符
[Baseus COVO S17 Pro]# disconnect FC:58:FA:E2:E4:3C
Attempting to disconnect from FC:58:FA:E2:E4:3C
[CHG] Device FC:58:FA:E2:E4:3C ServicesResolved: no
Successful disconnected
[CHG] Device FC:58:FA:E2:E4:3C Connected: no
[bluetooth]#
命令清单:
[bluetooth]# help
Menu main:
Available commands:
-------------------
advertise Advertise Options Submenu
scan Scan Options Submenu
gatt Generic Attribute Submenu
list List available controllers
show [ctrl] Controller information
select <ctrl> Select default controller
devices List available devices
paired-devices List paired devices
system-alias <name> Set controller alias
reset-alias Reset controller alias
power <on/off> Set controller power
pairable <on/off> Set controller pairable mode
discoverable <on/off> Set controller discoverable mode
agent <on/off/capability> Enable/disable agent with given capability
default-agent Set agent as the default one
advertise <on/off/type> Enable/disable advertising with given type
set-alias <alias> Set device alias
scan <on/off> Scan for devices
info [dev] Device information
pair [dev] Pair with device
trust [dev] Trust device
untrust [dev] Untrust device
block [dev] Block device
unblock [dev] Unblock device
remove <dev> Remove device
connect <dev> Connect device
disconnect [dev] Disconnect device
menu <name> Select submenu
version Display version
quit Quit program
exit Quit program
help Display help about this program
export Print evironment variables
串口通信模式:
修改/etc/systemd/system/bluetooth.target.wants/bluetooth.service文件
# bluetoothd 命令后添加-C参数
ExecStart=/usr/lib/bluetooth/bluetoothd -C
# 添加下边一行命令(启动后运行)
ExecStartPost=/usr/bin/sdptool add SP
重启服务及系统:
sudo systemctl daemon-reload
reboot
开启蓝牙发现,并允许配对请求
$ sudo hciconfig hci0 piscan
若需关闭发现,使用
$ sudo hciconfig hci0 noscan
$ rfcomm watch hci0
Waiting for connection on channel 1
此时等待设备连接,可用手机搜索连接蓝牙设备,连接后显示:
Connection from 94:0E:6B:63:22:F9 to /dev/rfcomm0
Press CTRL-C for hangup
此时会在/dev目录下产生rfcomm0设备文件,此时可通过对/dev/rfcomm0文件进行读写操作。