Skip to content

Instantly share code, notes, and snippets.

@sveatlo
Created November 20, 2021 17:23
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sveatlo/5608645659788ea0d13028f399ad9423 to your computer and use it in GitHub Desktop.
Save sveatlo/5608645659788ea0d13028f399ad9423 to your computer and use it in GitHub Desktop.
waybar bluetooth battery status
#!/bin/bash
ICON_BLUETOOTH=""
ICON_BATTERY_FULL=""
ICON_BATTERY_THREE_QUARTERS=""
ICON_BATTERY_HALF=""
ICON_BATTERY_QUARTER=""
ICON_BATTERY_EMPTY=""
BLUETOOTH_BATTERY_STATUS=$HOME/src/scripts/bluetooth_battery_status.sh
tooltip=""
statuses="$($BLUETOOTH_BATTERY_STATUS)"
if [[ $? != 0 ]]; then
exit 1
fi
n=0
sum=0
IFS=$'\n'
for status in $statuses; do
uuid=$(echo "$status" | cut -d' ' -f1)
name=$(bluetoothctl info "$uuid" | grep -oe 'Name: .*' | awk 'match($0, "Name: (.*)", m){print m[1]}')
percent=$(echo "$status" | cut -d' ' -f2 | grep -oe '[0-9]*')
tooltip="$tooltip$name: $percent%\n"
n=$((n+1))
sum=$((sum+percent))
done
IFS=' '
avg=$((sum / n))
ICON_BATTERY=""
if [[ $avg -ge 90 ]]; then
ICON_BATTERY=$ICON_BATTERY_FULL
elif [[ $avg -ge 75 ]]; then
ICON_BATTERY=$ICON_BATTERY_THREE_QUARTERS
elif [[ $avg -ge 50 ]]; then
ICON_BATTERY=$ICON_BATTERY_HALF
elif [[ $avg -ge 25 ]]; then
ICON_BATTERY=$ICON_BATTERY_QUARTER
else
ICON_BATTERY=$ICON_BATTERY_EMPTY
fi
tooltip=${tooltip%\\n}
echo "{\"text\": \"$ICON_BLUETOOTH $ICON_BATTERY\", \"tooltip\": \"$tooltip\"}"
#!/bin/bash
SCRIPTS_DIR=~/src/scripts
get_battery_status() {
device_uuid=$1
device_uuid_slug=$(echo "$device_uuid" | sed 's/:/_/g')
device_upower_path=$(upower -e | grep "$device_uuid_slug")
if [[ "$?" != 0 ]]; then
return 1
fi
upower_out=$(upower -i "$device_upower_path")
percentage=$(echo "$upower_out" | grep 'percentage:' | grep -oe '[0-9]*%')
if [[ "$?" != 0 ]]; then
return 1
fi
echo $percentage
}
# prepare devices to process
connected_devices=$1
if [[ "$connected_devices" == "" ]]; then
connected_devices=$($SCRIPTS_DIR/connected_bluetooth_devices.sh)
fi
if [[ "$connected_devices" == "" ]]; then
echo "no bluetooth devices connected" >&2
exit 1
fi
n=0
declare -A device_batteries
for device in ${connected_devices//\\n/ }; do
device_battery_percentage=$(get_battery_status "$device")
if [[ $? != 0 ]]; then
continue
fi
n=$((n+1))
device_batteries[$device]=$device_battery_percentage
# echo "$device: $device_battery_percentage"
done
if [[ $n -eq 0 ]]; then
echo "cannot get battery status for any specified bluetooth device" >&2
exit 1
fi
# TODO: do something else?
for device in ${!device_batteries[*]}; do
echo "$device ${device_batteries[$device]}"
done
#!/bin/bash
bluetoothctl paired-devices | cut -f2 -d' ' | while read uuid; do bluetoothctl info $uuid | grep -B8 -e 'Connected: yes' | grep 'Device' | cut -f2 -d' '; done
...
"custom/bluetooth-battery": {
"format": "{}",
"return-type": "json",
"interval": 5,
"exec": "~/.config/waybar/modules/bluetooth-battery.sh",
"exec-if": "bluetooth_battery_status.sh >/dev/null 2>&1",
"signal": 9
},
...
@sveatlo
Copy link
Author

sveatlo commented Nov 20, 2021

this uses bluetoothctl and upower

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment