summaryrefslogtreecommitdiff
path: root/.scripts/bt
blob: 2e4203df3587dbdb032c41f4e6f2aa64a174ad86 (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
#!/usr/bin/env bash

set -e

# Check if bluetooth is blocked
(rfkill | grep -E "bluetooth.*\<blocked\>" >/dev/null) && echo "Please check rfkill and unblock bluetooth module." && exit || true

[ "$1" = "stop" ] && systemctl stop bluetooth.service && exit
systemctl is-active bluetooth.service >/dev/null || systemctl start bluetooth.service

[ "$(bluetoothctl show | awk '/Powered/ {print $2}')" = "no" ] && bluetoothctl power on && bluetoothctl agent on

BTID='^([A-F0-9]{2}:){5}[A-F0-9]{2}$'

if [ $# -gt 0 ]; then
  if [ "$#" -eq 1 ]; then
    PAIRED=$(bluetoothctl paired-devices | grep "$1" | awk '/Device/ {print $2}')

    if   [ $(echo "$1"      | grep -E $BTID) ]; then bluetoothctl connect $1
    elif [ $(echo "$PAIRED" | grep -E $BTID) ]; then bluetoothctl connect $PAIRED
    else                                             bluetoothctl $*
    fi
  else bluetoothctl $*
  fi
else
  CONNECTED="$(bluetoothctl info | awk '/Name/ {print $2}')" || true

  bluetoothctl paired-devices | awk "/Device/ {if(\"$CONNECTED\"==\$3) print \"\033[1;32m\" \$2 \"\t\" \$3 \"\033[0m\"; else print \$2 \"\t\" \$3;}"
fi