Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
11.97k stars 581 forks source link

Should `hs.battery.otherBatteryInfo()` provide battery info for connected AirPods? #1608

Closed jasonrudolph closed 6 years ago

jasonrudolph commented 6 years ago

I see that the recently-added hs.battery.otherBatteryInfo() function can be used to get battery info for attached bluetooth devices:

https://github.com/Hammerspoon/hammerspoon/blob/590ea5125fed7be9258cb0f4edf1710f42f727bb/extensions/battery/internal.m#L391-L399

Should Apple AirPods be included in the battery info returned by hs.battery.otherBatteryInfo()? I ask, because I can see the battery info for Apple AirPods in System Preferences, but I don't see any battery info reported by hs.battery.otherBatteryInfo().

bluetooth
> hs.inspect(hs.battery.otherBatteryInfo())
{}

Is there a way to fetch the battery info for AirPods via Hammerspoon?


Refs: 590ea5125fed7be9258cb0f4edf1710f42f727bb Refs: #939

asmagill commented 6 years ago

If it's not showing up in the others, then at present, no, I don't believe Hammerspoon currently can detect these. The "otherBatteryInfo" function iterates through the IOKit service plane registry and looks for items which have a "BatteryPercent" field, so if the key name is different or if the air pods are registered in a different plane, the current code would miss them.

If you know of sample code online which does return the battery level for the airpods, I'll be happy to compare them and see what changes we can make to also capture the information, but as I don't have them myself, I can't really test it. If you have XCode installed on your computer, you might do some digging with the "ioRegistryExplorer" application and see if you can identify the field name we should be looking for.

(as an aside, this is not the first time Apple has changed field names, sometimes even between different firmware versions of the same product... it makes targeted IOKit queries problematic, but my first attempts at a generic IOKit module didn't work so well... it may be time to think about trying again because investigating this in the Hammerspoon console would be soooooo much easier!)

jasonrudolph commented 6 years ago

@asmagill: Thanks for the reply! :zap:

If you have XCode installed on your computer, you might do some digging with the "ioRegistryExplorer" application and see if you can identify the field name we should be looking for.

I see the AirPods in the IORegistryExplorer, but I don't see the battery info:

screen shot 2017-11-26 at 4 39 48 pm screen shot 2017-11-26 at 4 39 51 pm

If you know of sample code online which does return the battery level for the airpods, I'll be happy to compare them and see what changes we can make to also capture the information

duk242/AirPodsBatteryCLI does the trick. Example usage:

$ ./AirPodsPower.sh
🎧 S: 97% C: 0% L: 99% R: 100%

To get those results, AirPodsBatteryCLI shells out to run a system_profiler command and a defaults command. I'm guessing that you wouldn't want Hammerspoon to have to shell out to gather this info, but if there's more info that I can provide here, please let me know. :bow:

cmsj commented 6 years ago

Unfortunately it involves using private Apple APIs, but they are sufficiently rich that I couldn't resist adding it. Please do be aware that this can/will break in any future macOS update, but it works at least for 10.13.1 and currently has no kind of safety net. If people report crashes, the latter can change

jasonrudolph commented 6 years ago

@cmsj: Thanks for the lightning fast turnaround time on this request! I'm looking forward to trying it out :soon:.

:bow: