| 1 | #!/bin/bash |
| 2 | |
| 3 | # bcn, Bluetooth Connect |
| 4 | |
| 5 | mapfile -t devices < <(bluetoothctl devices) |
| 6 | |
| 7 | if [[ ${#devices[@]} -eq 0 ]]; then |
| 8 | notify-send "Bluetooth" "No devices found" |
| 9 | exit 1 |
| 10 | fi |
| 11 | |
| 12 | if [[ ${#devices[@]} -eq 1 ]]; then |
| 13 | line="${devices[0]}" |
| 14 | else |
| 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 |
| 31 | fi |
| 32 | |
| 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" |
| 51 | fi |