aboutsummarylogtreecommitdiffstats
path: root/toggle-bluetooth.sh
blob: 5618e110097ae62c96836848eb7448e932e59755 (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/bash

function main() {
  local lines
  lines="$(bluetoothctl -- show | grep '^\s*Powered:')" || return $?

  local status='off'  # bluetooth status
  local line
  # Iterate over all bluetooth devices:
  # If at least one is powered on, then set status to "on"
  # otherwise leave status at "off"
  while read -r line; do
    case "${line}" in
      *'Powered: no')
        ;;
      *'Powered: yes')
        status='on';
        ;;
      *)
        echo 'Unknown power state: '"${line}" >&2;
        notify-send 'Bluetooth' 'Unknown power state!' --icon=dialog-information --category=device.error --urgency=critical;
        return 1;
    esac
  done <<< "${lines}"

  if [ "${status}" = 'off' ]; then
    power_on || return $?
    notify-send 'Bluetooth' 'Enabled'  --icon=dialog-information --category=device.added || return $?
  else
    power_off || return $?
    notify-send 'Bluetooth' 'Disabled' --icon=dialog-information --category=device.removed || return $?
  fi
}

function power_on() {
  bluetoothctl -- power on
}

function power_off() {
  bluetoothctl -- power off
}

main "$@"