Using Extension Attributes to report Bluetooth battery levels

Caswell
New Contributor

Hi All.

I made a couple of extension attributes for querying the battery levels of Bluetooth Trackpads and Keyboards that may be useful to others.
Bluetooth Keyboard:

#!/bin/sh

kresult=`ioreg -r -c "AppleBluetoothHIDKeyboard" | grep -w BatteryPercent | sed 1d | awk '{print $4}' | sed s/"//g`
echo "<result>$kresult</result>"

Magic trackpad:

#!/bin/sh

tpBatLevel=`ioreg -r -c "BNBTrackpadDevice" | grep -w BatteryPercent | sed 1d | awk '{print $4}' | sed s/"//g`
echo "<result>$tpBatLevel</result>"

You can then create smart groups using these to generate notifications for low batteries. One thing to be aware of: If the keyboard or trackpad is asleep as the time the value is connected you will not get a value returned , so when creating a smart group you will need to add a is not and leave the field blank, otherwise you will get a lot of incorrect notifications.

This is also possible for bluetooth mice but I don't have one handy to find out the device name.

4 REPLIES 4

easyedc
Valued Contributor II

Just happened to come across this and wanted to pass along my Mouse script with a slight modification to return output for our users who don't have bluetooth mice yet.

#!/bin/sh

mouseBatLevel=`ioreg -r -c "BNBMouseDevice" | grep -w BatteryPercent | sed 1d | awk '{print $4}' | sed s/"//g`
if [[ "${mouseBatLevel}" == "" ]] ; then 
    echo "<result>No Magic Mouse</result>"    
 else
    echo "<result>$mouseBatLevel%</result>"
fi
exit 0

dderusha
Contributor
You can then create smart groups using these to generate notifications for low batteries.

when I try to make a smart group with the bluetooth extension attribute, I get a error stating "Bluetooth Mouse Battery is not a valid number"
Bluetooth Mouse Battery is the name of my extension attribute.

any suggestions?
thank you Dan

dderusha
Contributor

I had to comment out

#else # echo "<result>$mouseBatLevel%</result>"

and then I could get a smart group to report on the results.

sfgcasper
New Contributor

Has anyone noticed that what the command spits out is different than what the GUI says it is? I event looked up in System Information and it also doesn't match up with what the command spits out.