由于有个项目需要调试wifi,之前为了方便一直都是电脑和linux开发板连接到一个固定的wifi然后通过ip地址连接shell的,这就产生了一个问题,在linux开发板切换wifi时会失去连接,又不方便通过串口进行控制,于是想着通过蓝牙对开饭进行登录控制,参考的是树莓派社区的教程。
前提条件:
移植好蓝牙驱动
移植好bluez协议栈
先是创建一个btserial.sh脚本文件。
vim btserial.sh
在脚本文件里输入如下脚本命令,第一行命令是定义蓝牙名称,我这边设置成handy_aml。第二行命令行是打开使能蓝牙,第三行是打开蓝牙的串行通信功能。第四行是将蓝牙收到的数据映射到getty终端,登录用户为root,波特率115200。最后两行为使能打开射频串行口协议功能。
#!/bin/bash -e
#Edit the display name of the amlogic device so you can distinguish
#your unit from others in the Bluetooth console
#(very useful in a class setting)
echo PRETTY_HOSTNAME=handy_aml > /etc/machine-info
# Edit /lib/systemd/system/bluetooth.service to enable BT services
sudo sed -i: 's|^Exec.*toothd$| \
ExecStart=/usr/lib/bluetooth/bluetoothd -C \
ExecStartPost=/usr/bin/sdptool add SP \
ExecStartPost=/bin/hciconfig hci0 piscan \
|g' /lib/systemd/system/bluetooth.service
# create /etc/systemd/system/rfcomm.service to enable
# the Bluetooth serial port from systemctl
sudo cat <<EOF | sudo tee /etc/systemd/system/rfcomm.service > /dev/null
[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
EOF
# enable the new rfcomm service
sudo systemctl enable rfcomm
# start the rfcomm service
sudo systemctl restart rfcomm
保存该脚本到/root路径下。编辑/etc/rc.local文件,使系统在启动时执行该脚本,在exit 0前添加脚本执行命令。sudo /root/btserial.sh &
整个rc.local文件内容如下所示:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sudo /root/btserial.sh &
exit 0
保存退出,重启系统后可通过蓝牙登录linux开发板。我这里使用到的是putty进行登录测试。先打开电脑的蓝牙功能,进入到蓝牙配置页面,打开蓝牙更多选项。
在弹出的窗口下选择COM端口,点击添加按钮,选择传出,添加搜索到的handy_aml设备,确认退出,记下虚拟出的蓝牙端口号。
打开putty,选择端口方式登录,输入刚才创建好的虚拟蓝牙端口,波特率115200,点击连接,电脑会以蓝牙连接的方式连接linux开发板。