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:

ioreg -n "IOAppleBluetoothHIDDriver" | grep -i "batterypercent" | sed 's/[^[:digit:]]//g'
49
ioreg -n "BNBMouseDevice" | grep -i "batterypercent" | sed 's/[^[:digit:]]//g'
45
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
    | |   |     | | |   | {
    | |   |     | | |   |   "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"
    | |   |     | | |   | }

Line 31 shows the battery percent of the object.  Line 15 shows the product that this ioreg object corresponds to.  Then at the very top you see a line that contains the object name (line 1).

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

14 thoughts on “Mac Bluetooth Keyboard/Mouse Battery Level in Terminal

  1. It’s hard to find educated people in this particular subject, however, you seem like you know what you’re talking about! Thanks

  2. Japan Tsunami

    Cool post. A lot more people really need to read this. I can’t believe I didn’t discovered this sooner. Thanks!

  3. Sam Mason

    I'd personally use sed to do the filtering as well as string extraction; I've got something like this:

    ioreg -n BNBMouseDevice | sed -En 's/.*"BatteryPercent" = ([0-9]+).*/1/p'

    running from cron to see how my battery discharges over time. "-E" says to use more modern regexs, "-n" says not to print lines by default; "s/a/b/p" says to do a search a replace of "a" with "b" and to print the result. The brackets in "a" allow words to be saved, and referred to by the order they appear, so '1' ends up with the battery percentage. You could put an 'i' after the 'p' to make the regex case-insensitive like your grep is.

  4. Donald Sarratt

    Horribly late to the party with this, but testing on a Mac Mini turned up some interesting results.

    `ioreg -n "BNBMouseDevice"` shows BatteryPercent as 52, but the OSX gui interface and the `system_profiler SPBluetoothDataType` command both show the level as 88%. Any idea what could be causing this difference?

    • As the developer of Battery Status (http://itunes.apple.com/us/app/battery-status/id496730170?mt=12) I'm also extremely interested in knowing this. I currently use whatever is returned from BatteryPercent in IOKit, but I have noticed that it isn't always correct. Specifically, it seems to be incorrect about 20% of the time with keyboards, 5% of the time with mice, and 1% of the time with trackpads. I haven't been able to track down what causes it to be wrong, or any way of predicting how wrong it will be.

      Thanks to your comment, I'm now looking to find out if there's some kind of system_profiler framework or library I can use within my application.

  5. With havin so much content do you ever run into any issues of plagorism or copyright violation?

    My site has a lot of unique content I’ve either created myself or outsourced but it seems a lot of it is popping it up all over the web without my authorization.

    Do you know any ways to help prevent content from being ripped off?

    I’d certainly appreciate it.
    online casinos usa home page recently posted..online casinos usa home pageMy Profile

  6. running from cron to see how my battery discharges over time. "-E" says to use more modern regexs, "-n" says not to print lines by default; "s/a/b/p" says to do a search a replace of "a" with "b" and to print the result. The brackets in "a" allow words to be saved, and referred to by the order they appear, so '1' ends up with the battery percentage. You could put an 'i' after the 'p' to make the regex case-insensitive like your grep is.

  7. Cool post. A lot more people really need to read this. I can't believe I didn't discovered this sooner. Thanks!

  8. Education is going fast now days, in which the student’s rate is going to be the increasing. In which the students can make the quality and polish in front of the people. The students can get groom due to the procedure of the educational system.

  9. It is a very priceless and informative blog and presents some as alternative just correct and new procedures about technology. The ever constructing science is making the existence hundreds simpler and beneficial.

  10. Kroger’s Great People website was created for all the Kroger associates, so they could have easier access to work-related information and news they need.

  11. It's very informative article, thanks for sharing here.

  12. Very informative article, you have shared very valuable content, thanks for this great work.

  13. Your website is so informative and this article is very helpful. I will visit again on your site in future.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.