前几天试着写了一个解决自己蓝牙音箱连接问题的shell脚本,但是实际运行的时候还是有问题。今天重新整理了一下脚本。
主要问题
主要问题就是,当脚本发现蓝牙音箱且连接异常,重新扫描蓝牙音箱后,不能直接去连接,还要进行一次判断是否扫描到蓝牙音箱,连接后在判断是否连接成功。
问题代码片段如下:
…………
if ! Bluetooths info
then
echo "蓝牙音箱I7连接异常"
echo -e "\n……正在移除问题设备: 蓝牙音箱i7……\n"
Bluetooths remove "$deviceID"
sleep 5s
echo -e "\n"'重新扫描蓝牙设备'"\n"
Bluetooths --timeout 15 scan on
echo -e "\n……正在重新配对和连接蓝牙音箱i7……\n"
Bluetooths pair "$deviceID"
Bluetooths trust "$deviceID"
Bluetooths connect "$deviceID"
echo -e "\n正在重新检查蓝牙音箱i7连接状态\n"
i7.Connect.Status
break
else
if [[ $(Bluetooths info | grep -i "UUID: Advanced Audio Distribu") != "" ]]
then
echo "蓝牙音箱I7虽然连接成功但似乎工作异常"
echo -e "\n……正在移除问题设备: 蓝牙音箱i7……\n"
Bluetooths remove "$deviceID"
sleep 5s
echo -e "\n"'重新扫描蓝牙设备'"\n"
Bluetooths --timeout 15 scan on
echo -e "\n……正在重新配对和连接蓝牙音箱i7……\n"
Bluetooths pair "$deviceID"
Bluetooths trust "$deviceID"
Bluetooths connect "$deviceID"
echo -e "\n正在重新检查蓝牙音箱i7连接状态\n"
i7.Connect.Status
break
…………
问题解决
重新整理后的脚本如下:
#!/bin/bash
I7_ID='00:58:56:6A:C8:2D'
I7_Name='Device: i7 '
IFS=$'\n'
count=0 #记录重试次数
flag=0 #重试标记,flag=0 表示任务正常 flag=1 任务失败需要重试 flag=2 未找到相关设备
Connect() {
Bluetooths() {
$(type -P bluetoothctl) "$@"
}
#连接蓝牙音箱i7
i7.Connect() {
Bluetooths pair "$@"
Bluetooths trust "$@"
Bluetooths connect "$@"
}
# 重新检查蓝牙是否连接成功,当"bluetootctl info"执行失败且"bluetoothctl info | grep -i "UUID: Advanced Audio Distribu"返回为空值时,定义为任务失败需要重试
i7.Connect.Status() {
if [[ $(Bluetooths info | grep -i "UUID: Advanced Audio Distribu") == "" ]] && ! Bluetooths info
then
flag=1
else
flag=0
fi
}
#重新扫描蓝牙设备,如果发蓝牙音箱再连接
local deviceInfo
local key
local deviceName
local deviceID
i7.Scan() {
local i7Info=$(Bluetooths devices)
for N in $i7Info
do
local keyN=$(echo $N | awk '{print $1": "$3" "$4" "$5}')
local NameN="$keyN"
if [ "$NameN" == "$I7_Name" ]
then
local deviceIdN=$(echo $i | awk '{print $2}')
echo -e "\n发现蓝牙音箱$I7_Name现在开始连接\n"
i7.Connect "${deviceIdN}"
echo -e "\n正在重新检查蓝牙音箱i7连接状态\n"
i7.Connect.Status
break
else
flag=2
fi
done
}
#遍历当前设备是否有蓝牙音箱I7
while true
do
deviceInfo=$(Bluetooths devices)
for i in $deviceInfo
do
key=$(echo $i | awk '{print $1": "$3" "$4" "$5}')
deviceName="$key"
if [ "$deviceName" == "$I7_Name" ]
then
deviceID=$(echo $i | awk '{print $2}')
echo -e "\n"'发现蓝牙音箱: '$I7_Name
echo -e "\n"'=================================='
echo '== 正在检查蓝牙音箱i7连接状态 =='
echo -e '=================================='"\n"
if ! Bluetooths info
then
echo -e "\n蓝牙音箱I7连接异常\n"
echo -e "\n……正在移除问题设备: 蓝牙音箱i7……\n"
Bluetooths remove "$deviceID"
sleep 3s
echo -e "\n"'重新扫描蓝牙设备'"\n"
Bluetooths --timeout 15 scan on
i7.Scan
break
else
if [[ $(Bluetooths info | grep -i "UUID: Advanced Audio Distribu") != "" ]]
then
echo "蓝牙音箱I7虽然连接成功但似乎工作异常"
echo -e "\n蓝牙音箱I7连接异常\n"
echo -e "\n……正在移除问题设备: 蓝牙音箱i7……\n"
Bluetooths remove "$deviceID"
sleep 3s
echo -e "\n"'重新扫描蓝牙设备'"\n"
Bluetooths --timeout 15 scan on
i7.Scan
break
else
flag=0
break
fi
fi
else
flag=2
fi
done
if [ ${flag} -eq 0 ]
then
echo -e "\n蓝牙音箱i7已成功连接\n"
exit
elif [ ${flag} -eq 2 ]
then
echo -e "\n未找到蓝牙音箱i7,正在重试\n"
else
echo -e "\n蓝牙音箱i7连接失败,正在重试\n"
fi
count=$[ ${count} + 1 ]
if [ $count -ge 6 ]
then
echo -e "\n未找到蓝牙音箱i7,退出脚本\n"
exit
fi
echo "========================================"
echo "== 开始第$count次重新连接蓝牙音箱i7 =="
echo "== ……请稍等…… =="
echo -e "========================================\n"
echo -e "====== 3秒后重新连接蓝牙音箱i7 ======\n"
sleep 3s
Bluetooths --timeout 15 scan on
done
}
Connect
https://music.163.com/#/song?id=5044239
…………
if ! Bluetooths info
then
echo "蓝牙音箱I7连接异常"
echo -e "\n……正在移除问题设备: 蓝牙音箱i7……\n"
Bluetooths remove "$deviceID"
sleep 5s
echo -e "\n"'重新扫描蓝牙设备'"\n"
Bluetooths --timeout 15 scan on
echo -e "\n……正在重新配对和连接蓝牙音箱i7……\n"
Bluetooths pair "$deviceID"
Bluetooths trust "$deviceID"
Bluetooths connect "$deviceID"
echo -e "\n正在重新检查蓝牙音箱i7连接状态\n"
i7.Connect.Status
break
else
if [[ $(Bluetooths info | grep -i "UUID: Advanced Audio Distribu") != "" ]]
then
echo "蓝牙音箱I7虽然连接成功但似乎工作异常"
echo -e "\n……正在移除问题设备: 蓝牙音箱i7……\n"
Bluetooths remove "$deviceID"
sleep 5s
echo -e "\n"'重新扫描蓝牙设备'"\n"
Bluetooths --timeout 15 scan on
echo -e "\n……正在重新配对和连接蓝牙音箱i7……\n"
Bluetooths pair "$deviceID"
Bluetooths trust "$deviceID"
Bluetooths connect "$deviceID"
echo -e "\n正在重新检查蓝牙音箱i7连接状态\n"
i7.Connect.Status
break
…………
#!/bin/bash
I7_ID='00:58:56:6A:C8:2D'
I7_Name='Device: i7 '
IFS=$'\n'
count=0 #记录重试次数
flag=0 #重试标记,flag=0 表示任务正常 flag=1 任务失败需要重试 flag=2 未找到相关设备
Connect() {
Bluetooths() {
$(type -P bluetoothctl) "$@"
}
#连接蓝牙音箱i7
i7.Connect() {
Bluetooths pair "$@"
Bluetooths trust "$@"
Bluetooths connect "$@"
}
# 重新检查蓝牙是否连接成功,当"bluetootctl info"执行失败且"bluetoothctl info | grep -i "UUID: Advanced Audio Distribu"返回为空值时,定义为任务失败需要重试
i7.Connect.Status() {
if [[ $(Bluetooths info | grep -i "UUID: Advanced Audio Distribu") == "" ]] && ! Bluetooths info
then
flag=1
else
flag=0
fi
}
#重新扫描蓝牙设备,如果发蓝牙音箱再连接
local deviceInfo
local key
local deviceName
local deviceID
i7.Scan() {
local i7Info=$(Bluetooths devices)
for N in $i7Info
do
local keyN=$(echo $N | awk '{print $1": "$3" "$4" "$5}')
local NameN="$keyN"
if [ "$NameN" == "$I7_Name" ]
then
local deviceIdN=$(echo $i | awk '{print $2}')
echo -e "\n发现蓝牙音箱$I7_Name现在开始连接\n"
i7.Connect "${deviceIdN}"
echo -e "\n正在重新检查蓝牙音箱i7连接状态\n"
i7.Connect.Status
break
else
flag=2
fi
done
}
#遍历当前设备是否有蓝牙音箱I7
while true
do
deviceInfo=$(Bluetooths devices)
for i in $deviceInfo
do
key=$(echo $i | awk '{print $1": "$3" "$4" "$5}')
deviceName="$key"
if [ "$deviceName" == "$I7_Name" ]
then
deviceID=$(echo $i | awk '{print $2}')
echo -e "\n"'发现蓝牙音箱: '$I7_Name
echo -e "\n"'=================================='
echo '== 正在检查蓝牙音箱i7连接状态 =='
echo -e '=================================='"\n"
if ! Bluetooths info
then
echo -e "\n蓝牙音箱I7连接异常\n"
echo -e "\n……正在移除问题设备: 蓝牙音箱i7……\n"
Bluetooths remove "$deviceID"
sleep 3s
echo -e "\n"'重新扫描蓝牙设备'"\n"
Bluetooths --timeout 15 scan on
i7.Scan
break
else
if [[ $(Bluetooths info | grep -i "UUID: Advanced Audio Distribu") != "" ]]
then
echo "蓝牙音箱I7虽然连接成功但似乎工作异常"
echo -e "\n蓝牙音箱I7连接异常\n"
echo -e "\n……正在移除问题设备: 蓝牙音箱i7……\n"
Bluetooths remove "$deviceID"
sleep 3s
echo -e "\n"'重新扫描蓝牙设备'"\n"
Bluetooths --timeout 15 scan on
i7.Scan
break
else
flag=0
break
fi
fi
else
flag=2
fi
done
if [ ${flag} -eq 0 ]
then
echo -e "\n蓝牙音箱i7已成功连接\n"
exit
elif [ ${flag} -eq 2 ]
then
echo -e "\n未找到蓝牙音箱i7,正在重试\n"
else
echo -e "\n蓝牙音箱i7连接失败,正在重试\n"
fi
count=$[ ${count} + 1 ]
if [ $count -ge 6 ]
then
echo -e "\n未找到蓝牙音箱i7,退出脚本\n"
exit
fi
echo "========================================"
echo "== 开始第$count次重新连接蓝牙音箱i7 =="
echo "== ……请稍等…… =="
echo -e "========================================\n"
echo -e "====== 3秒后重新连接蓝牙音箱i7 ======\n"
sleep 3s
Bluetooths --timeout 15 scan on
done
}
Connect
https://music.163.com/#/song?id=5044239