Skip to main content
Question

Using Extension Attributes to report Bluetooth battery levels

  • November 6, 2012
  • 4 replies
  • 26 views

Forum|alt.badge.img+2

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

easyedc
Forum|alt.badge.img+16
  • Esteemed Contributor
  • May 6, 2013

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

Forum|alt.badge.img+15
  • Contributor
  • May 29, 2014
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


Forum|alt.badge.img+15
  • Contributor
  • June 2, 2014

I had to comment out

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

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


Forum|alt.badge.img+3
  • New Contributor
  • October 5, 2015

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.