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:
parent
427ff66213
commit
2736f9bd99
58
bt
58
bt
@ -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,37 +81,46 @@ 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"
|
||||||
|
echo choice:"$choice"
|
||||||
|
echo new:"$new_devices"
|
||||||
dev_no=$( echo "$new_devices" | nl | grep "$choice" | awk '{print $1}')
|
dev_no=$( echo "$new_devices" | nl | grep "$choice" | awk '{print $1}')
|
||||||
|
echo dev_no:$dev_no
|
||||||
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' )
|
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' )
|
||||||
|
echo bt_IDS:"$bt_IDS"
|
||||||
bluetoothctl pair "$dev_id" && sleep 2
|
echo devid:$dev_id
|
||||||
bluetoothctl connect "$dev_id"
|
#only attempt to connect if pairing succeeds
|
||||||
|
bluetoothctl pair "$dev_id" && ( pair_succesful=true && bluetoothctl connect "$dev_id" ) || pair_succesful=false
|
||||||
if $AUTOTRUST; then
|
if $AUTOTRUST; then
|
||||||
bluetoothctl trust "$dev_id"
|
bluetoothctl trust "$dev_id"
|
||||||
fi
|
fi
|
||||||
#if device is not already hard coded as paired, add to paired devices list
|
#if device is not already hard coded as paired, add to paired devices list
|
||||||
if [ "$( grep "$dev_id" "$PAIRLIST")" != "" ]
|
if [ "$( grep "$dev_id" "$PAIRLIST")" = "" ] || [ "$(wc -l "$PAIRLIST")" -eq 0 ] && $pair_succesful
|
||||||
then
|
then
|
||||||
echo to be added to $PAIRLIST
|
echo to be added to "$PAIRLIST":
|
||||||
echo Device "$dev_id" "$choice"
|
echo Device "$dev_id" "$choice"
|
||||||
echo Device "$dev_id" "$choice" >> "$PAIRLIST"
|
echo Device "$dev_id" "$choice" >> "$PAIRLIST"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
case $choice in
|
case $choice in
|
||||||
@ -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" )"
|
||||||
|
if [ "$new_paired_list" != "" ]
|
||||||
|
then
|
||||||
echo "$new_paired_list" > "$PAIRLIST"
|
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
|
||||||
|
power on
|
||||||
bluetoothctl pair "$dev_id"
|
bluetoothctl pair "$dev_id"
|
||||||
bluetoothctl connect "$dev_id";;
|
bluetoothctl connect "$dev_id"
|
||||||
|
fi;;
|
||||||
esac
|
esac
|
||||||
cleanup
|
|
||||||
|
Loading…
Reference in New Issue
Block a user