Krolyxon
Commit 99b642b

Branches

Refs →

aurora.localbinbcn

51 lines · 1.2 KB · Bourne Again Shell (bash)

1#!/bin/bash
2
3# bcn, Bluetooth Connect
4
5mapfile -t devices < <(bluetoothctl devices)
6
7if [[ ${#devices[@]} -eq 0 ]]; then
8 notify-send "Bluetooth" "No devices found"
9 exit 1
10fi
11
12if [[ ${#devices[@]} -eq 1 ]]; then
13 line="${devices[0]}"
14else
15 names=$(printf '%s\n' "${devices[@]}" | sed 's/^Device [^ ]* //')
16
17 selected=$(printf '%s\n' "$names" |
18 aurora-run --dmenu -l 10)
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
31fi
32
33MAC=$(echo "$line" | awk '{print $2}')
34NAME=$(echo "$line" | cut -d' ' -f3-)
35
36connect=$(bluetoothctl info "$MAC" |
37 awk '/Connected:/ {print $2}')
38
39if [[ "$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
48elif [[ "$connect" == "yes" ]]; then
49 notify-send "Bluetooth" "Disconnecting from $NAME..."
50 bluetoothctl disconnect "$MAC"
51fi