Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Battery status API in macOS?

Tags:

macos

battery

How can I read the status of the battery on my MacBookPro from my own application?

Googling has so far only revealed APIs for device drivers to handle power events - there's nothing about user-land processes accessing this information.

thanks.

like image 233
Alnitak Avatar asked Nov 07 '08 16:11

Alnitak


People also ask

What is battery API?

The Battery Status API, more often referred to as the Battery API, provides information about the system's battery charge level and lets you be notified by events that are sent when the battery level or charging status change.

How do I check the battery life on my Mac update?

To view your battery's condition, choose Apple menu > System Preferences, click Battery , then click Battery again. In the lower right corner, click Battery Health.

How do I show battery percentage in Swift?

Xcode 11 • Swift 5.1 Then you can create a computed property to return the battery level: Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled. If battery monitoring is not enabled, battery state is UIDevice.


2 Answers

If you're looking for a quick way to query it from the command line, you'll find the pmset command helpful. To query the battery status, specifically, use:

$ pmset -g batt
like image 190
Gary Chambers Avatar answered Oct 17 '22 18:10

Gary Chambers


You'll want to use IOKit for this, specifically the IOPowerSources functions. You can use IOPSCopyPowerSourcesInfo() to get a blob, and IOPSCopyPowerSourcesList() to then extract a CFArray out of that, listing the power sources. Then use IOPSGetPowerSourceDescription() to pull out a dictionary (see IOPSKeys.h for the contents of the dictionary).

like image 42
Ben Gottlieb Avatar answered Oct 17 '22 17:10

Ben Gottlieb