|
|
@ -1,21 +1,4 @@ |
|
|
|
#!/usr/bin/env bash |
|
|
|
# _ _ _ _ _ _ |
|
|
|
# __| |_ __ ___ ___ _ __ _ _ | |__ | |_ _ ___| |_ ___ ___ | |_ | |__ |
|
|
|
# / _` | '_ ` _ \ / _ \ '_ \| | | |_____| '_ \| | | | |/ _ \ __/ _ \ / _ \| __|| '_ \ |
|
|
|
# | (_| | | | | | | __/ | | | |_| |_____| |_) | | |_| | __/ || (_) | (_) | |_ | | | | |
|
|
|
# \__,_|_| |_| |_|\___|_| |_|\__,_| |_.__/|_|\__,_|\___|\__\___/ \___/ \__||_| |_| |
|
|
|
# |
|
|
|
# Author: Nick Clyde (clydedroid) |
|
|
|
# dmenu support by: Layerex |
|
|
|
# |
|
|
|
# A script that generates a dmenu menu that uses bluetoothctl to |
|
|
|
# connect to bluetooth devices and display status info. |
|
|
|
# |
|
|
|
# Inspired by networkmanager-dmenu (https://github.com/firecat53/networkmanager-dmenu) |
|
|
|
# Thanks to x70b1 (https://github.com/polybar/polybar-scripts/tree/master/polybar-scripts/system-bluetooth-bluetoothctl) |
|
|
|
# |
|
|
|
# Depends on: |
|
|
|
# Arch repositories: dmenu, bluez-utils (contains bluetoothctl) |
|
|
|
#!/bin/bash |
|
|
|
|
|
|
|
# Checks if bluetooth controller is powered on |
|
|
|
power_on() { |
|
|
@ -98,8 +81,8 @@ toggle_discoverable() { |
|
|
|
|
|
|
|
# Checks if a device is connected |
|
|
|
device_connected() { |
|
|
|
device_info=$(bluetoothctl info "$1") |
|
|
|
if echo "$device_info" | grep -q "Connected: yes"; then |
|
|
|
device_info=$(bluetoothctl info ""$1"") |
|
|
|
if echo ""$device"_info" | grep -q "Connected: yes"; then |
|
|
|
return 0 |
|
|
|
else |
|
|
|
return 1 |
|
|
@ -108,10 +91,10 @@ device_connected() { |
|
|
|
|
|
|
|
# Toggles device connection |
|
|
|
toggle_connection() { |
|
|
|
if device_connected $1; then |
|
|
|
bluetoothctl disconnect $1 |
|
|
|
if device_connected "$1"; then |
|
|
|
bluetoothctl disconnect "$1" |
|
|
|
else |
|
|
|
bluetoothctl connect $1 |
|
|
|
bluetoothctl connect "$1" |
|
|
|
fi |
|
|
|
} |
|
|
|
|
|
|
@ -129,10 +112,10 @@ device_paired() { |
|
|
|
|
|
|
|
# Toggles device paired state |
|
|
|
toggle_paired() { |
|
|
|
if device_paired $1; then |
|
|
|
bluetoothctl remove $1 |
|
|
|
if device_paired "$1"; then |
|
|
|
bluetoothctl remove "$1" |
|
|
|
else |
|
|
|
bluetoothctl pair $1 |
|
|
|
bluetoothctl pair "$1" |
|
|
|
fi |
|
|
|
} |
|
|
|
|
|
|
@ -150,10 +133,10 @@ device_trusted() { |
|
|
|
|
|
|
|
# Toggles device connection |
|
|
|
toggle_trust() { |
|
|
|
if device_trusted $1; then |
|
|
|
bluetoothctl untrust $1 |
|
|
|
if device_trusted "$1"; then |
|
|
|
bluetoothctl untrust "$1" |
|
|
|
else |
|
|
|
bluetoothctl trust $1 |
|
|
|
bluetoothctl trust "$1" |
|
|
|
fi |
|
|
|
} |
|
|
|
|
|
|
@ -167,8 +150,8 @@ print_status() { |
|
|
|
counter=0 |
|
|
|
|
|
|
|
for device in "${paired_devices[@]}"; do |
|
|
|
if device_connected $device; then |
|
|
|
device_alias=$(bluetoothctl info $device | grep "Alias" | cut -d ' ' -f 2-) |
|
|
|
if device_connected "$device"; then |
|
|
|
device_alias=$(bluetoothctl info "$device" | grep "Alias" | cut -d ' ' -f 2-) |
|
|
|
|
|
|
|
if [ $counter -gt 0 ]; then |
|
|
|
printf ", %s" "$device_alias" |
|
|
@ -190,11 +173,11 @@ print_status() { |
|
|
|
|
|
|
|
# A submenu for a specific device that allows connecting, pairing, and trusting |
|
|
|
device_menu() { |
|
|
|
device=$1 |
|
|
|
device="$1" |
|
|
|
|
|
|
|
# Get device name and mac address |
|
|
|
device_name=$(echo $device | cut -d ' ' -f 3-) |
|
|
|
mac=$(echo $device | cut -d ' ' -f 2) |
|
|
|
device_name=$(echo "$device" | cut -d ' ' -f 3-) |
|
|
|
mac=$(echo "$device" | cut -d ' ' -f 2) |
|
|
|
|
|
|
|
# Build options |
|
|
|
if device_connected $mac; then |
|
|
@ -272,7 +255,7 @@ show_menu() { |
|
|
|
*) |
|
|
|
device=$(bluetoothctl devices | grep "$chosen") |
|
|
|
# Open a submenu if a device is selected |
|
|
|
if [[ $device ]]; then device_menu "$device"; fi |
|
|
|
if [[ "$device" ]]; then device_menu "$device"; fi |
|
|
|
;; |
|
|
|
esac |
|
|
|
} |
|
|
|