~tardypad/dotfiles

ref: 8a1a01060b09bdf89a8188f0cd84af082ef52dbb dotfiles/files/scripts/shell/bluetooth -rwxr-xr-x 1.4 KiB
8a1a0106 — Damien Tardy-Panis Add bluetooth script 2 months ago
                                                                                
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
74
75
76
#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  ACTION=
  DEVICE_ALIAS=
  FILE=
}

parse_command_line() {
  ACTION="$1"
  DEVICE_ALIAS="$2"

  if [ "${ACTION}" = 'send' ]; then
    FILE="$3"
  fi
}

validate_command_line() {
  if [ "${ACTION}" != 'connect' ] \
     && [ "${ACTION}" != 'disconnect' ] \
     && [ "${ACTION}" != 'receive' ] \
     && [ "${ACTION}" != 'send' ]; then
    exit_error "Invalid action '${ACTION}'"
  fi

  if [ "${ACTION}" = 'connect' ] || [ "${ACTION}" = 'send' ]; then
    if [ -z "${DEVICE_ALIAS}" ]; then
      exit_error "Missing device alias"
    fi
  fi

  if [ "${ACTION}" = 'send' ]; then
    if [ -z "${FILE}" ]; then
      exit_error "Missing file argument"
    fi
  fi
}

exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  exit 1
} >&2

device_mac() {
  bluetoothctl paired-devices \
    | grep "$1$" \
    | cut -d' ' -f2
}

bluetooth_connect() {
  bluetoothctl connect "$( device_mac "${DEVICE_ALIAS}" )"
}

bluetooth_disconnect() {
  if [ -n "${DEVICE_ALIAS}" ]; then
    bluetoothctl disconnect "$( device_mac "${DEVICE_ALIAS}" )"
  else
    bluetoothctl disconnect
  fi
}

bluetooth_receive() {
  bt-obex -y -s "${XDG_DOWNLOAD_DIR:-$HOME/Downloads}" 2> /dev/null
}

bluetooth_send() {
  bt-obex -p "$( device_mac "${DEVICE_ALIAS}" )" "${FILE}" 2> /dev/null
}

init_variables
parse_command_line "$@"
validate_command_line

"bluetooth_${ACTION}"