diff --git a/bt b/bt index 8af1003..1a93a24 100755 --- a/bt +++ b/bt @@ -38,15 +38,13 @@ scan(){ #sets variable in case scanning was no already on before the start of bt [ -f /tmp/bt_start_scan$$ ] || start_scan="$( date +'%s' )" if [ "$scanstatus" = "no" ]; then - echo "off" - bluetoothctl scan on 2>&1 /dev/null & + bluetoothctl scan on & start_scan="$( date +'%s' )" fi echo "$start_scan" > /tmp/bt_start_scan$$ elif [ "$1" = off ]; then if [ "$scanstatus" = "yes" ]; then - echo "on" - bluetoothctl scan off 2>&1 /dev/null + bluetoothctl scan off fi fi } @@ -85,19 +83,22 @@ fi startup & update_pair_list & -# include head command here to speed up launch if too many devices are listed (long uptime and long scanning will lead to this depending on your surroundings) -# shellcheck disable=SC1091 -if bluetoothctl devices | head | awk '{print "bluetoothctl info "$2}' | . /dev/stdin | grep -q "Connected: yes" -then - actions="$(printf "disconnect\n%s" "$actions")" -fi +connected_ids="$(bluetoothctl devices | awk '{print "bluetoothctl info "$2}' | . /dev/stdin | grep -E '(^Device |Connected:)' | sed -e 'N;s/\n/;/' | grep 'Connected: yes' | cut -d' ' -f2,5)" +connected_devices="$(echo "$connected_ids" | while read -r id; do + if grep -q "$id" "$ALIASLIST"; then + grep "$id" "$ALIASLIST" | cut -d' ' --complement -f1 + else + grep "$id" "$PAIRLIST" | cut -d' ' --complement -f1,2 + fi +done)" +[ -z "$connected_devices" ] && actions="$(printf "disconnect\n%s" "$actions")" #Compile list of all Bluetooth IDS of paired devices (from bluetoothctl and from hardcoded list) bt_IDS="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | awk '{print $2}' | sort -u )" #Compile list of all device Names of paired devices (from bluetoothctl and from hardcoded list) -paired_devices_with_id="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{for (i=2; i> "$BLACKLIST" fi;; *) + echo "choice: $choice" dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}') [ "$dev_no" != "" ] && dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}') + echo "dev_id: $dev_id" if [ -n "$dev_id" ]; then power on if bluetoothctl info "$dev_id" | grep -q "Connected: yes" then + echo Was connceted... disconnecting bluetoothctl disconnect "$dev_id" else - if bluetoothctl paired-devices | grep -q "$dev_id"; then - old_scan_state="$(scan on)" + if ! bluetoothctl paired-devices | grep -q "$dev_id"; then + echo "not in paired-devices list, re-adding (device needs to be in pairing mode for this)" + notify-send "bt" "device not in paired-devices list, re-adding (device needs to be in pairing mode for this)" + scan on + echo "Scan on" + sleep 5 + echo Attempting to pair... bluetoothctl pair "$dev_id" - scan "$old_scan_state" + scan off fi + echo connecting... bluetoothctl connect "$dev_id" fi fi;;