Improving battery reading when the machine wakes.

I am using the CFProperty "AppleRawCurrentCapacity" value from IOServiceGetMathcingService( kIOMainPortDefault, IOServiceMatching( "AppleSmartBattery" ) ) to obtain the mAh value of the MacBook's battery. Once when the app receives the NSWorkspace notification of display sleep, and once again when receiving the NSWorkspace notification of display wake.

I then calculate how much battery was expended during sleep by deducting the pre value from post value.

The results can wildly vary, from it using 16 mAh per hour, to gaining 10 mAh an hour (without being plugged in). Sometimes a 10 hour sleep doesn't use any mAh... Which is clearly wrong.

When my app detects the key kIOPMPSInvalidWakeSecondsKey is present, I set a timer for that period + 3 seconds and attempt to read the battery values again. Yet the results can still wildly vary.

I see on Intel Macs there is a kIOPMPSMaxErrKey (set to 9 on a 2019 16"), however I don't see this on M1, and don't know how it can help, if it does exist.

Can anyone give me any pointers as to what I am doing wrong, or in the direction of obtaining reliable mAh values?

I like mAh as it's more precise that the standard integer values of 0 - 100, but I don't have to stick with mAh, I'd just don't like reporting that a 16 hour sleep didn't use any battery, when I know it's not true.

Thanks

Replies

I am using the CFProperty AppleRawCurrentCapacity value from … AppleSmartBattery

Just FYI, none of the above is considered to be supported API.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • Hey Quinn, master of Apple framework knowledge... Is there an approved way of obtaining battery information with more precision than 0-100 integers? I personally don't really care too much (ha) about the units, just don't want to report a 17 hour sleep used zero battery (something or rather).

Add a Comment