My Mac Mini reports that my keyboard is powered at 1%, which is obviously wrong; I charge it almost daily. Do you have a wireless keyboard or mouse? I don't have AirPods.
I do. But I don't care too much about keyboard or mouse battery level. Airpods lasts only a couple of hours. I'm more concerned about its battery level.
@martin I also tried this but gave up after I found on the app AirBuddy.
When I open the case of the AirPods I get on the desktop the current battery level
Left: AirPods
right: Case
is displayed. As soon as I plug in the AirPods, the display changes to the battery status of the individual AirPods.
Then I can call up the battery display on the desktop at any time via a shortcut.
A notification about an individual battery level is also on board. You can choose between macOS notification or the AirBuddy Dispaly display.
But you can also get the battery levels via a widget.
#!/usr/bin/env bash
# Airpods.sh
# Output connected Airpods battery levels via CLI
BT_DEFAULTS=$(defaults read /Library/Preferences/com.apple.Bluetooth)
SYS_PROFILE=$(system_profiler SPBluetoothDataType 2>/dev/null)
MAC_ADDR=$(grep -b2 "Minor Type: "<<<"${SYS_PROFILE}"|awk '/Address/{print $3}')
regex_connected="(Connected:.+Yes)"
if [[ $SYS_PROFILE =~ $regex_connected ]]
then
#this regex won't work because of PRCE not working with some bash version (Connected:.Yes).(Vendor ID:.0x004C.)(Product ID:.*(Case.+%).+(Firmware Version:.[A-Z-a-z-0-9]+))
patwithCase="(.+)(Connected:.Yes).(Vendor ID:.0x004C.)(Product ID.*(Case.+%).+(Firmware.+Version:.+))"
patwithoutCase="(.+)(Connected:.Yes).(Vendor ID:.0x004C.)(Product ID.*.+(Firmware.+Version:.+))"
replace="?"
comp=$(echo ${SYS_PROFILE} | sed "s/Address:/$replace/g")
set -f
IFS='?'
ary=($comp)
for key in "${!ary[@]}";
do
d=$(echo "${ary[$key]}")
data=""
macAddress=""
connectedStatus=""
vendorID=""
batteryLevel=""
firmwareVersion=""
if [[ $d =~ $patwithCase ]]
then
macAddress=$( echo "${BASH_REMATCH[1]}" | sed 's/ *$//g')
connectedStatus="${BASH_REMATCH[2]}"
vendorID="${BASH_REMATCH[3]}"
data="${BASH_REMATCH[4]}"
firmwareVersion=$(echo ${BASH_REMATCH[6]} | awk '{print $3}')
batterylevelregex="Case Battery Level: (.+%) Left Battery Level: (.+%) Right Battery Level: (.+%)"
batterySingleRegex="(BatteryPercentSingle) = ([0-9]+)"
if [[ $data =~ $batterylevelregex ]]
then
caseBattery="${BASH_REMATCH[1]}"
leftBattery="${BASH_REMATCH[2]}"
rightBattery="${BASH_REMATCH[3]}"
batteryLevel="${caseBattery} ${leftBattery} ${rightBattery}"
if [ -z "$batteryLevel" ]
then
echo ""
else
echo $macAddress"@@""$batteryLevel"
fi
elif [[ $data =~ $batterySingleRegex ]]
then
#IN PROGRESS - AIRPODS MAX (TO VERIFY)
batteryLevel=$macAddress"@@"${BASH_REMATCH[2]}
echo $batteryLevel
fi
elif [[ $d =~ $patwithoutCase ]]
then
macAddress=$( echo "${BASH_REMATCH[1]}" | sed 's/ *$//g')
connectedStatus="${BASH_REMATCH[2]}"
vendorID="${BASH_REMATCH[3]}"
data="${BASH_REMATCH[4]}"
firmwareVersion=$(echo ${BASH_REMATCH[6]} | awk '{print $3}')
batterylevelregex="Left Battery Level: (.+%) Right Battery Level: (.+%)"
if [[ $data =~ $batterylevelregex ]]
then
caseBattery="-1"
leftBattery="${BASH_REMATCH[1]}"
rightBattery="${BASH_REMATCH[2]}"
batteryLevel="${caseBattery} ${leftBattery} ${rightBattery}"
if [ -z "$batteryLevel" ]
then
echo ""
else
echo $macAddress"@@""$batteryLevel"
fi
fi
fi
done
else
echo "nc"
fi
exit 0
Unfortunately my script knowledge is not sufficient to create the variables (Case, Left, Right) for a notification.
Maybe there is someone here who can do that.
That's interesting. For some reason, the returned string on your Mac has a space between the digits and the % sign.
Add a space to the RegEx and see if it can capture it.
Try this: Airpods Pro Battery Level.kmmacros (6.5 KB)
@martin thank you for the new macro. Unfortunately, I also get the same error message with this one (with the space).
Somehow the worm is in there for me.
I have also restarted the MBP. Still the same error message. I will try tomorrow to find the error.
Sorry to hear that.
Can you paste the output string to the forum?
If the characters are not changed by browser or forum server, then we can see what are the characters in the string.