/ de-omarchy / bin / omarchy-bluetooth-device
swadhinbiswas Initial de-omarchy: Omarchy UI layer for Arch-based distros · 8/22/2026
9f393b2
53 lines · 1.5 KB
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
#!/bin/bash

# omarchy:summary=Control a Bluetooth device
# omarchy:group=bluetooth
# omarchy:args=[pair|connect|disconnect|forget] <address>
# omarchy:examples=omarchy bluetooth device connect 00:11:22:33:44:55

set -e

usage() {
  echo "Usage: omarchy-bluetooth-device [pair|connect|disconnect|forget] <address>" >&2
  exit 1
}

action=${1:-}
address=${2:-}

[[ $action == "pair" || $action == "connect" || $action == "disconnect" || $action == "forget" ]] || usage
[[ $address =~ ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$ ]] || usage

power_on() {
  [[ $(timeout 2s bluetoothctl show 2>/dev/null) == *"Powered: yes"* ]] && return
  # Not bluetoothctl directly: Bluetooth is turned off by an rfkill soft block,
  # and BlueZ refuses to power an adapter up while one is set.
  omarchy-bluetooth-power on || true
}

trust_device() {
  bluetoothctl trust "$address" >/dev/null 2>&1 || true
}

case "$action" in
  pair)
    power_on
    timeout 20s bluetoothctl pair "$address" >/dev/null 2>&1 || true
    trust_device
    timeout 20s bluetoothctl connect "$address" >/dev/null 2>&1 || true
    ;;
  connect)
    power_on
    trust_device
    timeout 20s bluetoothctl connect "$address" >/dev/null 2>&1 || true
    ;;
  disconnect)
    timeout 10s bluetoothctl disconnect "$address" >/dev/null 2>&1 || true
    ;;
  forget)
    power_on
    timeout 10s bluetoothctl disconnect "$address" >/dev/null 2>&1 || true
    timeout 10s bluetoothctl remove "$address" >/dev/null 2>&1 || true
    ;;
esac