bluetooth-spmenu/bluetooth-spmenu

78 lines
2 KiB
Bash
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
# bluetooth-spmenu
# Basic spmenu Bluetooth manager
# Licensed under GNU GPLv3.
[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER=spmenu
# Bluetooth device control functions
trust_dev() { bluetoothctl trust "$dev" && return 0 || return 1; }
pair_dev() { bluetoothctl pair "$dev" && return 0 || return 1; }
remove_dev() { bluetoothctl remove "$dev" && return 0 || return 1; }
untrust_dev() { bluetoothctl untrust "$dev" && return 0 || return 1; }
connect_dev() { bluetoothctl connect "$dev"; }
disconnect_dev() { bluetoothctl disconnect "$dev"; }
# Misc functions
list_opts() { seldev_o="$(printf -- "<b>Options</b>\nConnect\nDisconnect\n<b>Toggle</b>\nPair\nRemove\nTrust\nUntrust\n\nExit" | sed "s/-e //g" | $RUNLAUNCHER -g 1 -l 20 -p "What do you want to do with this device?" | awk '{ print $1 }')"; }
list_dev() {
printf "%s\n\nExit" "$(bluetoothctl devices | cut -d\ -f3-)" > /tmp/btctrl-file
sleep 1
printf "update" > /tmp/spmenu.fifo
list_dev
}
scan_dev() {
bluetoothctl power on
bluetoothctl scan on
}
# Enable bluetooth and scan for devices
enable_bt() {
scan_dev &
bluetoothctl devices | grep -q "No default controller" && printf "No Bluetooth controller was found.\n" && exit 1
list_dev &
seldev="$($RUNLAUNCHER -lf /tmp/btctrl-file -l 12 -g 1 -p "Select a device")"
# Check what to do
case "$seldev" in
"") exit 0 ;;
"Exit") exit 0 ;;
esac
dev="$(bluetoothctl devices | grep "$seldev$" | awk '{ print $2 }')"
# Check if a MAC was grabbed
[ -z "$dev" ] && printf "Could not get MAC\n" && exit 1
}
# Perform actions as per user choice
perform() {
case "$seldev_o" in
"Trust") trust_dev && list_opts ;;
"Pair") pair_dev && list_opts ;;
"Connect") connect_dev ;;
"Disconnect") disconnect_dev ;;
"Untrust") untrust_dev && list_opts ;;
"Remove") remove_dev ;;
"Exit") pkill bluetoothctl; exit 0 ;;
"") pkill bluetoothctl; exit 0 ;;
esac
}
main() {
enable_bt
list_opts
perform
pkill bluetoothctl
return 0
}
main