New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jasonrudolph opened this Issue Nov 23, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@jasonrudolph

jasonrudolph commented Nov 23, 2017

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

/// hs.battery.otherBatteryInfo() -> table
/// Function
/// Returns information about non-PSU batteries (e.g. bluetooth accessories)
///
/// Parameters:
/// * None
///
/// Returns:
/// * A table containing information about other batteries known to the system, or an empty table if no devices were found

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: 590ea51
Refs: #939

@asmagill

This comment has been minimized.

Show comment
Hide comment
@asmagill

asmagill Nov 24, 2017

Member

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!)

Member

asmagill commented Nov 24, 2017

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

This comment has been minimized.

Show comment
Hide comment
@jasonrudolph

jasonrudolph Nov 26, 2017

@asmagill: Thanks for the reply! ⚡️

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. 🙇

jasonrudolph commented Nov 26, 2017

@asmagill: Thanks for the reply! ⚡️

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. 🙇

@cmsj cmsj closed this in e5738e8 Nov 27, 2017

@cmsj

This comment has been minimized.

Show comment
Hide comment
@cmsj

cmsj Nov 27, 2017

Member

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

Member

cmsj commented Nov 27, 2017

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

This comment has been minimized.

Show comment
Hide comment
@jasonrudolph

jasonrudolph Dec 3, 2017

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

🙇

jasonrudolph commented Dec 3, 2017

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

🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment