On the MacEnterprise list (http://lists.psu.edu/archives/macenterprise.html), Michael Edwards posed an interesting question:
Anyone know of a handy way to monitor the status of bluetooth keyboard/trackpad battery levels on remote machines? We have several Mac Mini’s that use them, that are machines with no primary individual user – so it would be handy to get notified (email or similar) when the batteries need to be changed.
So I thought it might be fun to figure out how to answer this.
I first looked at system_profiler:
system_profiler -listDataTypes
lists SPBluetoothDataType.
So:
system_profiler SPBluetoothDataType Bluetooth: Apple Bluetooth Software Version: 4.0.8f17 Hardware Settings: Address: 00-26-B0-F5-7B-72 Manufacturer: Broadcom Name: mini Firmware Version: v180 c367 Bluetooth Power: On Discoverable: Yes Vendor ID: 0x5ac Product ID: 0x8216 (snip) Devices (Paired, Favorites, etc): Apple Wireless Mouse: Address: 7c-6d-62-f2-47-2a Type: Mouse Firmware Version: 0x84 Services: Apple Wireless Mouse Paired: Yes Favorite: No Connected: Yes Manufacturer: Apple (0x3, 0x31c) Vendor ID: 0x5ac Product ID: 0x30d EDR Supported: No eSCO Supported: No gneagle’s keyboard: Address: 00-25-bc-fc-29-01 Type: Keyboard Firmware Version: 0x50 Services: Apple Wireless Keyboard Paired: Yes Favorite: No Connected: Yes Manufacturer: Apple (0x3, 0x31c) Vendor ID: 0x5ac Product ID: 0x239 EDR Supported: No eSCO Supported: No (snip)
That doesn’t seem to contain the info we’re looking for.
Next I looked at ioreg
. It doesn’t have very friendly output, but we do what we can.
I first looked for info on anything with “Keyboard” in the name:
ioreg | grep Keyboard | | | | | | +-o AppleBluetoothHIDKeyboard | | | | | | | +-o AppleEmbeddedKeyboard | | | | | | | +-o IOHIDKeyboard
The -c option allows us to get more detail on anything with a specific class. ioreg -c AppleBluetoothHIDKeyboard
returns lots of data, most of which we don’t care about. Visually looking through it, it looked like grepping for “Battery” would narrow things down a bit:
ioreg -c AppleBluetoothHIDKeyboard | grep Battery | | | | | | | "BatteryPanic" = Yes | | | | | | | "BatteryLow" = Yes | | | | | | | "BatteryLowNotificationType" = "LowBattery" | | | | | | | "BatteryDangerouslyLowNotificationType" = "CriticallyLowBattery" | | | | | | | "BatteryPercent" = 12 | | | | | | | "Battery" = <"MVLT0x8f9UT
We can do something similar for “Mouse”:
ioreg | grep Mouse | | | | | +-o BNBMouseDevice
ioreg -c BNBMouseDevice | grep Battery | | | | | | "BatteryPanic" = No | | | | | | "Battery" = <"CHCT0x1CA$ | | | | | | "BatteryLow" = No | | | | | | "BatteryLowNotificationType" = "LowBattery" | | | | | | "BatteryPercent" = 56 | | | | | | "BatteryDangerouslyLowNotificationType" = "CriticallyLowBattery"
I’ll leave it as an exercise for the reader to parse this info even further to get, say, just the BatteryPercent as a number, or just the value of BatteryLow. You could then send an email, open a ticket in your tracking system, update a web page, or whatever makes sense for your organization.
Greg, yet again you’ve posted something incredibly useful. I’m trying to write this as a Customer Info Field in Absolute Manage so I can display the results. However, I’m not having much luck. Any hints on how to grep for a negative result in Bash? This script I’m writing returns the result “BatterLow command” not found. Any hints?
BatteryLow = Yes
ioreg -c BNBMouseDevice | grep BatteryLow $BatteryLow 1>/dev/null
if [ $? -eq 0 ]; then
result=Yes
else
result=No
fi
echo $result
Not really sure what you are trying to do here:
ioreg -c BNBMouseDevice | grep BatteryLow $BatteryLow 1>/dev/null
Maybe you want:
/usr/sbin/ioreg -c BNBMouseDevice | grep '"BatteryLow" = Yes'
?
I think I got it.
if ioreg -c BNBMouseDevice | grep BatteryLow;
[ “BatteryLow” = Yes ]; then
result=Replace
else
result=Fine
fi
echo $result
Basically Absolute Manage will tell me if the Result=Replace.
Hi Greg! I get differents values from ioreg and from the drop down menu at bluetooth. The difference is big 37% at ioreg and 60% at bluetooth drop down menu…
btw “system_profile SPBluetoothDataType” showed me the Battery Level and it’s the same from the bluetooth drop down menu
If you use munkireport-php there is now a Bluetooth module that reports on the Bluetooth and battery status. Details:
https://github.com/munkireport/munkireport-php/tree/master/app/modules/bluetooth
I actually use nagios to check and alert us for Mac minis we use in our conference rooms at work. Here’s the source for any that are interested. https://github.com/kkunkel/OSX-Nagios