fix unpair feature

This commit is contained in:
Alexander Bocken 2020-06-08 21:53:13 +02:00
parent c18a45b38b
commit 5d2affc8e0

16
bt
View File

@ -4,7 +4,10 @@ SCAN_PERIOD=5
#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)
BLACKLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/blacklist BLACKLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/blacklist
PAIRLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/paired PAIRLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/paired
actions='turn off\nturn on\npair\nunpair' actions="turn off
turn on
pair
unpair"
#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.
for prog in dmenu bluetoothctl awk cat; do for prog in dmenu bluetoothctl awk cat; do
@ -21,11 +24,10 @@ bt_IDS="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{
paired_devices="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' )" paired_devices="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' )"
disp_devices="$( echo "$paired_devices" | grep -vf "$BLACKLIST" )" disp_devices="$( echo "$paired_devices" | grep -vf "$BLACKLIST" )"
#echo disp_devices:"$disp_devices"test
#Don't print empty device list, removes unnecessary empty choice in dmenu #Don't print empty device list, removes unnecessary empty choice in dmenu
[ "$disp_devices" = "" ] && choice=$( printf $actions | dmenu -i -p 'What BT action would you like to perform:' ) [ "$disp_devices" = "" ] && choice=$( echo "$actions" | dmenu -i -p 'What BT action would you like to perform:' )
[ "$disp_devices" != "" ] && choice=$( printf "%s\n$actions" "$disp_devices" | dmenu -i -p 'What BT action would you like to perform:' ) [ "$disp_devices" != "" ] && choice=$( echo "$disp_devices" "$actions" | dmenu -i -p 'What BT action would you like to perform:' )
cleanup(){ cleanup(){
bluetoothctl scan off > /dev/null bluetoothctl scan off > /dev/null
@ -65,8 +67,8 @@ case $choice in
cleanup;; cleanup;;
"unpair") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?') "unpair") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?')
if [ -n "$choice" ]; then if [ -n "$choice" ]; then
dev_no=$(cat -n /tmp/paired_devices | grep -E "^[0-9]*\tDevice [0-9:].* $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 -E "$dev_no""[[:space:]]" | awk '{print $2}' ) dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' )
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" )"
@ -77,7 +79,7 @@ esac
if [ -n "$choice" ]; then if [ -n "$choice" ]; then
#echo paired_devices: "$( echo "$paired_devices" | nl )" #echo paired_devices: "$( echo "$paired_devices" | nl )"
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_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}')
#echo dev_no:"$dev_no" #echo dev_no:"$dev_no"
#echo dev_id:"$dev_id" #echo dev_id:"$dev_id"