commit 9d730c0a84a7ca6044b3d4c3ad85215e7bd95ba7 Author: Alexander Bocken Date: Mon Jun 8 22:10:40 2020 +0200 added example image diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b9f7490 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Alexander Bocken + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8f5835c --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +# bthandler +# See LICENSE file for copyright and license details. + +VERSION = 1.1 + +# paths +PREFIX = ~/.local/bin +DATA_DIR = ~/.local/share/bt#if changed, needs adjustment in bt as well (defined in the beginning) +SRC = bt + + +install: + mkdir -p $(DESTDIR)$(PREFIX) + mkdir -p $(DATA_DIR) + cp -f bt $(DESTDIR)$(PREFIX) + chmod 755 $(DESTDIR)$(PREFIX)/bt + touch $(DATA_DIR)/blacklist + touch $(DATA_DIR)/paired + +uninstall: + rm -f $(DESTDIR)$(PREFIX)/bt + rm -rf $(DATA_DIR) + +#clears manual paired devices list and blacklist +clear: + rm -rf $(DATA_DIR) + mkdir -p $(DATA_DIR) + touch $(DATA_DIR)/paired + touch $(DATA_DIR)/blacklist + +.PHONY: install uninstall clear diff --git a/README.md b/README.md new file mode 100644 index 0000000..91b5bb7 --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# bthandler +A simple interactive tool to: +- connect to already paired devices +- pair new ones +- unpair already paired devices +- turn bluetooth on/off +- blacklist paired devices to not be listed in connection selection (useful for auto-connecting devices like some mice) + +all via dmenu. Should be easily extendable by editing the `actions` string. + +# Installation +Edit the Makefile to reflect your preffered installation destination. Then, simply + +```bash +make install +``` + +# Clear blacklist/pairlist + +Since `bluetoothctl paired-devices` seems to be hugely unreliable in listing paired devices, bthandler has a seperate list for all paired devices through bthandler. +There is also a blacklist available to not display certain devices, which might be useful for autoconnection Bluetooth mice for example. + +To clear these files, simply run +```bash +make clear +``` +and these files will be reset to their inital, empty state. + +# Uninstall + +To uninstall you might follow that it's a simple +```bash +make uninstall +``` + +# Pending features + +Here's a growing list of features that are not yet actively worked on but might be nice in the future. Currently I'm more concerned with reliability that fulfilling feature requests, but I'm still open to put some on this list. + +- start scanning for new devices immediately at execution to save time when pairing new devices +- auto-trust newly paired devices/trust device via dmenu +- blacklist devices via dmenu diff --git a/bt b/bt new file mode 100755 index 0000000..633fee5 --- /dev/null +++ b/bt @@ -0,0 +1,92 @@ +#!/bin/bash +#wait time to discover new devices in seconds +SCAN_PERIOD=5 +#locations of blacklist and hard coded list of paired devices (watch out, need to modify Makefile as well if you want to change these values and still use make install) +BLACKLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/blacklist +PAIRLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/paired +actions="turn off +turn on +pair +unpair" + +#Checks for necessary programs to be present. Very unlikely not to be present but let's just err on the safer side. +for prog in dmenu bluetoothctl awk cat; do + if ! hash "$prog" 2>/dev/null; then + printf 'bthandler: %s: command not found\n' "$prog" >&2 + exit 127 + fi +done + +#Compile list of all Bluetooth IDS of paired devices (from bluetoothctl and from hardcoded list) +bt_IDS="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{print $2}' )" + +#Compile list of all device Names of paired devices (from bluetoothctl and from hardcoded list) +paired_devices="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{for (i=3; i /dev/null + exit 0 +} + +case $choice in + "turn on") bluetoothctl power on + cleanup;; + "turn off") bluetoothctl power off + cleanup;; + "scan on") bluetoothctl power on && echo power on && sleep 2 + ([ -n "$TERMINAL" ] && $TERMINAL -e bluetoothctl scan on ) || st bluetoothctl scan on + cleanup;; + "pair") bluetoothctl power on + bluetoothctl scan on & disown + notify-send "Bluetooth" "Searching for devices, please wait a bit" + sleep $SCAN_PERIOD + echo "$paired_devices" > /tmp/paired_devices + new_devices="$( bluetoothctl devices | awk '{for (i=3; i> "$PAIRLIST" + fi + fi + cleanup;; + "unpair") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?') + if [ -n "$choice" ]; then + dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}') + dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' ) + bluetoothctl remove "$dev_id" + #remove device to unpair from hard coded paired devices list + new_paired_list="$( grep -v "$dev_id" "$PAIRLIST" )" + echo "$new_paired_list" > "$PAIRLIST" + fi + cleanup;; +esac + +if [ -n "$choice" ]; then + #echo paired_devices: "$( echo "$paired_devices" | nl )" + dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}') + dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}') + #echo dev_no:"$dev_no" + #echo dev_id:"$dev_id" + #echo choice:"$choice" + bluetoothctl power on + bluetoothctl pair "$dev_id" + bluetoothctl connect "$dev_id" +fi + +cleanup diff --git a/cropped.png b/cropped.png new file mode 100644 index 0000000..54359d5 Binary files /dev/null and b/cropped.png differ