My blog has moved!

You should be automatically redirected in 6 seconds. If not, visit
http://www.mattdturner.com/wordpress
and update your bookmarks.

AdSense

Pages

Thursday, April 21, 2011

Mac Bluetooth Keyboard/Mouse Battery Level in Terminal

There are a few reasons why somebody might want to check the current battery percentage of a bluetooth mouse and/or keyboard within Terminal on a mac.  Some that come to mind are for use in GeekTool, if you are connected to the computer remotely through ssh, etc.

Well as it turns out, its actually very easy to do.  The tool that we will use to check the battery percentage is called "ioreg".  The Mac OS X manual describes ioreg as a tool that "displays the I/O Kit registry. It shows the hierarchical registry structure as an inverted tree."

There are many options available with the ioreg tool, however this article will just be discussing how to check the battery percentage of different devices.  See the below text for the code to view the battery percentage:

matthewturner/~:  ioreg -n "IOAppleBluetoothHIDDriver" | grep -i "batterypercent" | sed 's/[^[:digit:]]//g'
49
matthewturner/~:  ioreg -n "BNBMouseDevice" | grep -i "batterypercent" | sed 's/[^[:digit:]]//g'
45
matthewturner/~:  ioreg -n "BNBMouseDevice" | grep -i "batterypercent"
    | |   |     | | |   |   "BatteryPercent" = 45

There are a few things that should be noted about the above commands.

  1. The -n option is used to show the object properties ONLY for the corresponding object name.
  2. The grep command is used to only show lines that contains the term "batterypercent".
    1. The -i in the grep command is used to ignore case in "batterypercent".
  3. The sed command is used to parse the output text so that it only shows the actual number percentage.
If, for some reason, your device names are different then you just need to type ioreg -l to get an output of all objects.  Now you can use the Find utility in terminal to search for "batterypercent".  There should be very few instances of the string.  A few lines above the "batterypercent" property, you should see another property that is called "product".  This property tells you which device you are looking at.  Once you know which device it is, you can continue to browse the lines above until you see a line that starts with +-o.  This is the object name.  See the example below:


    | |   |     | | | +-o BNBMouseDevice  <class BNBMouseDevice, id 0x100001732, registered, matche$
    | |   |     | | |   | {
    | |   |     | | |   |   "PrimaryUsagePage" = 1
    | |   |     | | |   |   "MaxInputReportSize" = 6
    | |   |     | | |   |   "IOUserClientClass" = "IOHIDLibUserClient"
    | |   |     | | |   |   "BatteryPanic" = No
    | |   |     | | |   |   "VendorID" = 1452
    | |   |     | | |   |   "VersionNumber" = 132
    | |   |     | | |   |   "DeviceAddress" = "34-15-9e-d0-27-aa"
    | |   |     | | |   |   "HIDPointerResolution" = 104595456
    | |   |     | | |   |   "HIDDescriptor" = ((34,<05010902a10185100509190129021500250195027501810$
    | |   |     | | |   |   "SuspendSupported" = Yes
    | |   |     | | |   |   "ExtendedFeatures" = {"SuperMode"={"size"=1,"id"=215,"min"=0,"max"=1,"t$
    | |   |     | | |   |   "MaxFeatureReportSize" = 65
    | |   |     | | |   |   "Product" = "Magic Mouse"
    | |   |     | | |   |   "SerialNumber" = "34-15-9e-d0-27-aa"
    | |   |     | | |   |   "PSM" = 17
    | |   |     | | |   |   "BatteryLow" = No
    | |   |     | | |   |   "Transport" = "Bluetooth"
    | |   |     | | |   |   "Elements" = ({"ReportID"=0,"ElementCookie"=1,"CollectionType"=1,"Type"$
    | |   |     | | |   |   "MouseButtonDivision" = 55
    | |   |     | | |   |   "SetReportTimeoutMS" = 3500
    | |   |     | | |   |   "BatteryLowNotificationType" = 1835887727
    | |   |     | | |   |   "Manufacturer" = "Apple"
    | |   |     | | |   |   "ConnectionNotificationType" = 1835885427
    | |   |     | | |   |   "ProductID" = 781
    | |   |     | | |   |   "IOPowerManagement" = {"CurrentPowerState"=1}
    | |   |     | | |   |   "BatteryDangerouslyLowNotificationType" = 1836082284
    | |   |     | | |   |   "DeviceUsagePairs" = ({"DeviceUsagePage"=1,"DeviceUsage"=2},{"DeviceUsa$
    | |   |     | | |   |   "BD_ADDR" = <34159ed027aa>
    | |   |     | | |   |   "BatteryPercent" = 45
    | |   |     | | |   |   "MouseButtonMode" = "TwoButton"
    | |   |     | | |   |   "ClassOfDevice" = 9600
    | |   |     | | |   |   "DefaultMultitouchProperties" = {"parser-type"=2000,"parser-options"=4,$
    | |   |     | | |   |   "BTHIDObjectID" = 148620288
    | |   |     | | |   |   "InputReportElements" = ({"ReportID"=16,"ElementCookie"=73,"Size"=48,"R$
    | |   |     | | |   |   "VendorIDSource" = 2
    | |   |     | | |   |   "ReportInterval" = 11250
    | |   |     | | |   |   "CFBundleIdentifier" = "com.apple.driver.AppleBluetoothMultitouch"
    | |   |     | | |   |   "MaxOutputReportSize" = 1
    | |   |     | | |   |   "IOCFPlugInTypes" = {"7DDEECA8-A7B4-11DA-8A0E-0014519758EF"="IOHIDFamil$
    | |   |     | | |   |   "IOProviderClass" = "IOBluetoothL2CAPChannel"
    | |   |     | | |   |   "LocationID" = 516958122
    | |   |     | | |   |   "IOClass" = "BNBMouseDevice"
    | |   |     | | |   |   "DisconnectionNotificationType" = 1835885683
    | |   |     | | |   |   "DeviceName" = "Magic Mouse"
    | |   |     | | |   |   "HIDDefaultBehavior" = "Mouse"
    | |   |     | | |   |   "DefaultMultitouchPreferences" = {"MouseHorizontalSwipe"=2,"MouseMoment$
    | |   |     | | |   |   "CountryCode" = 0
    | |   |     | | |   |   "IOMatchCategory" = "IODefaultMatchCategory"
    | |   |     | | |   |   "PrimaryUsage" = 2
    | |   |     | | |   |   "IOProbeScore" = 1010
    | |   |     | | |   |   "GetReportTimeoutMS" = 3500
    | |   |     | | |   |   "IOGeneralInterest" = "IOCommand is not serializable"
    | |   |     | | |   | }

The line highlighted in red shows the battery percent of the object.  The line above, highlighted in yellow, shows the product that this ioreg object corresponds to.  Then at the very top you see a line highlighted in green.  This line contains the object name.

For more information on the ioreg I/O kit registry tool in Mac OS X, please see the Mac OS X Manual.

0 comments:

Post a Comment