| @@ -1,24 +1,51 @@ |
| 1 | 1 | | #!/bin/bash |
| 2 | 2 | | |
| 3 | | − | #bcn, Bluetooth Connect |
| 3 | + | # bcn, Bluetooth Connect |
| 4 | 4 | | |
| 5 | + | mapfile -t devices < <(bluetoothctl devices) |
| 5 | 6 | | |
| 6 | | − | device_amount=$(bluetoothctl devices | wc -l) |
| 7 | + | if [[ ${#devices[@]} -eq 0 ]]; then |
| 8 | + | notify-send "Bluetooth" "No devices found" |
| 9 | + | exit 1 |
| 10 | + | fi |
| 7 | 11 | | |
| 8 | | − | if [[ $device_amount = 1 ]]; then |
| 9 | | − | MAC=$(bluetoothctl devices | awk {'print $2'}) |
| 10 | | − | [ -z $MAC ] && MAC=NoDeviceFound # Prevents accidental disconnect error |
| 12 | + | if [[ ${#devices[@]} -eq 1 ]]; then |
| 13 | + | line="${devices[0]}" |
| 11 | 14 | | else |
| 12 | | − | select=$(bluetoothctl devices | awk {'print $3'} | fuzzel --dmenu -l 10 -fn Monospace-15) |
| 13 | | − | MAC=$(bluetoothctl devices | grep $select | awk {'print $2'}) |
| 14 | | − | [ -z $MAC ] && MAC=NoDeviceFound # Prevents accidental disconnect error |
| 15 | + | names=$(printf '%s\n' "${devices[@]}" | sed 's/^Device [^ ]* //') |
| 16 | + | |
| 17 | + | selected=$(printf '%s\n' "$names" | |
| 18 | + | fuzzel --dmenu -l 10 -fn Monospace-15) |
| 19 | + | |
| 20 | + | [[ -z "$selected" ]] && exit 0 |
| 21 | + | |
| 22 | + | # Find MAC corresponding to selected full name |
| 23 | + | for device in "${devices[@]}"; do |
| 24 | + | name=$(echo "$device" | cut -d' ' -f3-) |
| 25 | + | |
| 26 | + | if [[ "$name" == "$selected" ]]; then |
| 27 | + | line="$device" |
| 28 | + | break |
| 29 | + | fi |
| 30 | + | done |
| 15 | 31 | | fi |
| 16 | 32 | | |
| 17 | | − | connect=$(bluetoothctl info $MAC | grep Connected: | awk '{print $2}') |
| 18 | | − | if [[ $connect = no ]]; then |
| 19 | | − | notify-send "Attempting to connect to $select" |
| 20 | | − | bluetoothctl connect $MAC || notify-send "Failed to Connect" |
| 21 | | − | elif [[ $connect = yes ]]; then |
| 22 | | − | notify-send "Attempting to disconnect $select" |
| 23 | | − | bluetoothctl disconnect $MAC |
| 33 | + | MAC=$(echo "$line" | awk '{print $2}') |
| 34 | + | NAME=$(echo "$line" | cut -d' ' -f3-) |
| 35 | + | |
| 36 | + | connect=$(bluetoothctl info "$MAC" | |
| 37 | + | awk '/Connected:/ {print $2}') |
| 38 | + | |
| 39 | + | if [[ "$connect" == "no" ]]; then |
| 40 | + | notify-send "Bluetooth" "Connecting to $NAME..." |
| 41 | + | |
| 42 | + | if bluetoothctl connect "$MAC"; then |
| 43 | + | notify-send "Bluetooth" "Connected to $NAME" |
| 44 | + | else |
| 45 | + | notify-send "Bluetooth" "Failed to connect to $NAME" |
| 46 | + | fi |
| 47 | + | |
| 48 | + | elif [[ "$connect" == "yes" ]]; then |
| 49 | + | notify-send "Bluetooth" "Disconnecting from $NAME..." |
| 50 | + | bluetoothctl disconnect "$MAC" |
| 24 | 51 | | fi |