aboutsummaryrefslogtreecommitdiff
path: root/extras/HOME/.local/bin/bluetooth-select.sh
blob: d01aa4c6be16555f1a0db82db9333ca560c3ce90 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash

run() {
  out=$("$@" 2>&1)
  if [ "$?" -ne 0 ] ; then
    gxmessage "$out"
  fi
}

disconnect() {
  echo "Disconnecting"
  run bluetoothctl -- disconnect
}

power_off() {
  echo "Power Off"
  run bluetoothctl -- power off
}

power_on() {
  echo "Power On"
  run bluetoothctl -- power on
}

scan_on() {
  echo "Scan On"
  run bluetoothctl -- scan on
}

scan_off() {
  echo "Scan Off"
  run bluetoothctl -- scan off
}

declare -A ops

ops["Disconnect"]=disconnect
ops["Power On"]=power_on
ops["Power Off"]=power_off
ops["Scan On"]=scan_on
ops["Scan Off"]=scan_off

ops_keys=""
for k in "${!ops[@]}" ; do
  ops_keys="$ops_keys\n$k"
done


if [[ -z "$ROFI" ]] ; then
  ROFI='rofi -dmenu'
fi

devices="$(bluetoothctl -- devices | sed 's#^Device ##')"
selection="$(
  echo -e "$devices\n$ops_keys" | $ROFI -i -p "Connect Bluetooth" \
    -theme-str '* {theme-color: #8888ff;}' \
    -dmenu)"

if [ -z "${selection}" ] ; then
  exit 1
fi

echo "selection=${selection}"

op="${ops[$selection]}"
if [[ -z "$op" ]] ; then
  macaddr="${selection%% *}"
  echo "macaddr=${macaddr}"
  run bluetoothctl -- connect "$macaddr"
else
  eval "$op"
fi