Battery status API in macOS?

本小妞迷上赌 提交于 2019-12-27 17:30:35

问题


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.


回答1:


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




回答2:


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



回答3:


Maybe help extracted text into script app

pmset -g batt | head -n 1 | cut -c19- | rev | cut -c 2- | rev

output

Battery Power
AC Power



回答4:


Look at the System Management Controller. I don't have my MBP handy, but I believe you need to look at smc.h



来源:https://stackoverflow.com/questions/272552/battery-status-api-in-macos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!