1. 简介
需求是:使用笔记本蓝牙直接连接树莓派蓝牙,然后发送命令操作树莓派。将蓝牙作为console,对树莓派进行操作。
2. 步骤
2.1. 蓝牙互联
sudo bluetoothctl #打开蓝牙
scan on #开始扫描
power on #给芯片上电,可以看到mac和设备名称
agent on #代理
pair 04:D3:B5:F0:4A:74 #设备配对,完成互联
2.2. 测试蓝牙连接及能否传输命令
sudo systemctl status bluetooth # 查看蓝牙是否正常启动
sudo sdptool add SP #服务发现协议(SDP)是用于搜索服务的蓝牙协议
pi@raspberrypi:~ $ man sdptool # SDP协议,全称Session Description Protocol,翻译过来就是会话描述协议。
NAME
sdptool — control and interrogate查询 SDP servers
SYNOPSIS
sdptool [options] {command} [command parameters ...]
DESCRIPTION
sdptool provides the interface for performing SDP queries on Bluetooth devices, and administering a local SDP database.
Known service names are DID, SP, DUN, LAN, FAX, OPUSH, FTP, HS, HF, HFAG, SAP, NAP, GN, PANU, HCRP,HID, CIP, A2SRC, A2SNK, AVRCT, AVRTG, UDIUE, UDITE and SYNCML.
sudo sdptool add SP #添加serial port服务
sudo sdptool brower local # 查看适配器提供的功能
pi@raspberrypi:~ $ hciconfig #检查是否识别并启用了我们的蓝牙适配器。蓝牙堆栈已将其命名为“ hci0
hci0: Type: Primary Bus: UART
BD Address: B8:27:EB:C2:69:13 ACL MTU: 1021:7 SCO MTU: 64:1
UP RUNNING PSCAN ISCAN
RX bytes:29014 acl:1136 sco:0 events:1205 errors:0
TX bytes:72815 acl:1881 sco:0 commands:98 errors:0
pi@raspberrypi:~ $
sudo hciconfig hci0 piscan # 恢复扫描
pi@raspberrypi:~ $ man rfcomm #RFCOMM是为了兼容传统的串口应用,同时取代有线的通信方式,蓝牙协议栈需要提供与有线串口一致的通信接口而开发出的协议。
RFCOMM(1)
NAME
rfcomm - RFCOMM configuration utility
SYNOPSIS
rfcomm [ options ] < command > < dev >
DESCRIPTION
rfcomm is used to set up, maintain, and inspect the RFCOMM configuration of the Bluetooth subsystem in the Linux kernel. If no command is given, or if the option -a is used, rfcomm prints information about the configured RFCOMM devices.
sudo rfcomm watch hci0 1 getty rfcomm0 115200 vt100 -a root #让其他蓝牙能连接到树莓派的蓝牙的指定端口
1.在开始菜单中打开“蓝牙和其他设备”--》“更多蓝牙设置”--》“COM 端口”—--》“添加”——》选择“传出”--->确认退出,记下虚拟出的蓝牙端口号
2.打开putty,选择端口方式登录,输入刚才创建好的虚拟蓝牙端口,波特率115200,点击连接,电脑会以蓝牙连接的方式连接linux开发板。
2.3. 完成配置文件及测试
配置成开机启动,对/lib/systemd/system/bluetooth.service进行修改
pi@raspberrypi:~ $ sudo cat /lib/systemd/system/bluetooth.service #修改这个文件,修改一行,添加2行,共3行
[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth
[Service]
Type=dbus
BusName=org.bluez
#ExecStart=/usr/libexec/bluetooth/bluetoothd
ExecStart=/usr/libexec/bluetooth/bluetoothd -E -C #修改这里
ExecStartPost=/usr/bin/sdptool add SP #这里是新添加的行
ExecStartPost=/bin/hciconfig hci0 piscan #这里是新添加的行
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
ProtectHome=true
ProtectSystem=full
[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service
pi@raspberrypi:~ $
配置成开机启动,新增配置文件/etc/systemd/system/rfcomm.service
pi@raspberrypi:~ $ sudo cat /etc/systemd/system/rfcomm.service
[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service
# remap bluetooth channel 1 to getty
[Service]
ExecStart=/usr/bin/rfcomm watch hci0 1 getty rfcomm0 115200 vt100 -a root
[Install]
WantedBy=multi-user.target
pi@raspberrypi:~ $
配置成开机启动
sudo systemctl enable rfcomm
sudo systemctl restart rfcomm
参考
-
命令和故障解决参考:https://blog.csdn.net/qq21497936/article/details/110940484
-
树莓派通过蓝牙进行串口通讯:https://blog.csdn.net/qq_42747705/article/details/116948131
-
故障解决参考:https://www.jianshu.com/p/a301717a396f
-
树莓派systemctl文件配置参考:https://blog.csdn.net/lxh1205509119/article/details/114226999
版本
- 2022-11-17 第一版