-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth_battery_status.sh
executable file
·53 lines (44 loc) · 1.29 KB
/
bluetooth_battery_status.sh
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
#!/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=$(bluetoothctl devices Connected | cut -f2 -d' ' | while read uuid; do bluetoothctl info $uuid | grep -B8 -e 'Connected: yes' | grep 'Device' | cut -f2 -d' '; done)
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