aboutsummaryrefslogtreecommitdiff
path: root/bluetooth/btConnect.sh
blob: 9d9793836a4a83c470735106d7f6a8cf2da16109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/sh

# btConnect.sh
#
# Selects an available device to connect to or disconnect from.

title='btConnect.sh'

if [ "$1" = '-d' ]; then
	if ! bluetoothctl info | grep -Fq 'Connected: yes'; then
		notify-send "$title" "Not connected to any device."
		exit 4
	fi
	bluetoothctl disconnect
	exit 0
fi

if ! systemctl show bluetooth | grep -Fq 'StatusText=Running'; then
	sudo systemctl start bluetooth || exit 1
	notify-send "$title" "Started bluetooth.service"
fi

if bluetoothctl show | grep -Fq 'Powered: no'; then
	bluetoothctl power on || exit 1
	notify-send "$title" "Bluetooth controller powered on."
fi

lines=$(bluetoothctl devices Paired | wc -l)
selected=$(bluetoothctl devices Paired | dmenu -l "$lines" -p "💓") || exit 1
mac=$(echo "$selected" | cut -d' ' -f2)
name=$(echo "$selected" | cut -d' ' -f3-)

if bluetoothctl info "$mac" | grep -F 'Connected' | grep -Fq 'yes'; then
	notify-send "$title" "Already connected to ${name}\n(${mac})"
	exit 2
fi

if ! bluetoothctl connect "$mac"; then
	notify-send "$title" "Unable to connect to ${name}\n(${mac})"
	exit 3
fi

notify-send "$title" "Connected to ${name}\n(${mac})"