increased reliability of pairing and unpairing

the pairlist now works again as intended.
furthermore more correct handling for special
cases like first/last device added.
This commit is contained in:
Alexander Bocken 2020-07-04 20:27:19 +02:00
parent 427ff66213
commit 2736f9bd99

80
bt
View File

@ -1,5 +1,5 @@
#!/bin/bash #!/bin/sh
#wait time to discover new devices in seconds
SCAN_PERIOD=5 SCAN_PERIOD=5
AUTOTRUST=false AUTOTRUST=false
#locations of blacklist and hard coded list of paired devices (watch out, need to modify Makefile as well if you want to change these values and still use make install) #locations of blacklist and hard coded list of paired devices (watch out, need to modify Makefile as well if you want to change these values and still use make install)
@ -10,7 +10,7 @@ actions="pair
disconnect disconnect
unpair" unpair"
[ "$AUTOTRUST" = false ] && actions+=" [ "$AUTOTRUST" = false ] && actions="${actions}
trust" trust"
#Checks for necessary programs to be present. Very unlikely not to be present but let's just err on the safer side. #Checks for necessary programs to be present. Very unlikely not to be present but let's just err on the safer side.
@ -28,7 +28,7 @@ power(){
} }
scan(){ scan(){
scanstatus="$( bluetoothctl show | grep Discovering | awk '{print $2}' )" scanstatus="$( bluetoothctl show | grep Discovering | awk '{print $2}' )"
if [ "$1" = "on" ]; then if [ "$1" = on ]; then
#sets variable in case scanning was already on before the start of bt #sets variable in case scanning was already on before the start of bt
[ "$start_scan" = "" ] && start_scan="$( date +'%s' )" [ "$start_scan" = "" ] && start_scan="$( date +'%s' )"
if [ "$scanstatus" = "no" ]; then if [ "$scanstatus" = "no" ]; then
@ -81,35 +81,44 @@ pair(){
sleep "$sleep_period" sleep "$sleep_period"
fi fi
if [ $paired_devices = "" ]; then all_devices="$( bluetoothctl devices )"
echo "$paired_devices" > /tmp/paired_devices if [ "$paired_devices" = "" ]; then
new_devices="$( bluetoothctl devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' | grep -vf /tmp/paired_devices )" new_devices="$( echo "$all_devices" | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' )"
rm -f /tmp/paired_devices bt_IDS="$( echo "$all_devices" | awk '{print $2}' )"
else else
new_devices="$( bluetoothctl devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' )" echo "$paired_devices" > /tmp/paired_devices$$
filtered_devices="$( echo "$all_devices" | grep -vf "/tmp/paired_devices$$")"
bt_IDS="$( echo "$filtered_devices" | awk '{print $2}' )"
new_devices="$( echo "$filtered_devices" | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' )"
rm /tmp/paired_devices$$
fi fi
[ $new_devices = "" ] && options="rescan" || options=$(echo "$new_devices" && echo 'rescan') [ "$new_devices" = "" ] && options="rescan" || options=$(echo "$new_devices" && echo 'rescan')
choice=$( echo "$options" | dmenu -l 10 -i -p 'pair with which device?' ) choice=$( echo "$options" | dmenu -l 10 -i -p 'pair with which device?' )
if [ -n "$choice" ]; then if [ -n "$choice" ]; then
if [ "$choice" = "rescan" ]; then if [ "$choice" = "rescan" ]; then
start_scan="$( date +'%s')" start_scan="$( date +'%s')"
pair pair
fi else
bt_IDS="$( bluetoothctl devices | awk '{print $2}' )" echo all:"$all_devices"
dev_no=$( echo "$new_devices" | nl | grep "$choice" | awk '{print $1}') echo choice:"$choice"
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' ) echo new:"$new_devices"
dev_no=$( echo "$new_devices" | nl | grep "$choice" | awk '{print $1}')
bluetoothctl pair "$dev_id" && sleep 2 echo dev_no:$dev_no
bluetoothctl connect "$dev_id" dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' )
if $AUTOTRUST; then echo bt_IDS:"$bt_IDS"
bluetoothctl trust "$dev_id" echo devid:$dev_id
fi #only attempt to connect if pairing succeeds
#if device is not already hard coded as paired, add to paired devices list bluetoothctl pair "$dev_id" && ( pair_succesful=true && bluetoothctl connect "$dev_id" ) || pair_succesful=false
if [ "$( grep "$dev_id" "$PAIRLIST")" != "" ] if $AUTOTRUST; then
then bluetoothctl trust "$dev_id"
echo to be added to $PAIRLIST fi
echo Device "$dev_id" "$choice" #if device is not already hard coded as paired, add to paired devices list
echo Device "$dev_id" "$choice" >> "$PAIRLIST" if [ "$( grep "$dev_id" "$PAIRLIST")" = "" ] || [ "$(wc -l "$PAIRLIST")" -eq 0 ] && $pair_succesful
then
echo to be added to "$PAIRLIST":
echo Device "$dev_id" "$choice"
echo Device "$dev_id" "$choice" >> "$PAIRLIST"
fi
fi fi
fi fi
} }
@ -127,7 +136,13 @@ case $choice in
bluetoothctl remove "$dev_id" bluetoothctl remove "$dev_id"
#remove device to unpair from hard coded paired devices list #remove device to unpair from hard coded paired devices list
new_paired_list="$( grep -v "$dev_id" "$PAIRLIST" )" new_paired_list="$( grep -v "$dev_id" "$PAIRLIST" )"
echo "$new_paired_list" > "$PAIRLIST" if [ "$new_paired_list" != "" ]
then
echo "$new_paired_list" > "$PAIRLIST"
else
rm -rf "$PAIRLIST"
touch "$PAIRLIST"
fi
fi;; fi;;
"trust") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?') "trust") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?')
if [ -n "$choice" ]; then if [ -n "$choice" ]; then
@ -144,9 +159,10 @@ case $choice in
*) *)
echo "$choice" echo "$choice"
dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}') dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}')
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}') [ "$dev_no" != "" ] && dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}')
bluetoothctl power on if [ -n "$dev_id" ]; then
bluetoothctl pair "$dev_id" power on
bluetoothctl connect "$dev_id";; bluetoothctl pair "$dev_id"
bluetoothctl connect "$dev_id"
fi;;
esac esac
cleanup