macOS 中的电池状态 API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/272552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Battery status API in macOS?
提问by Alnitak
How can I read the status of the battery on my MacBookPro from my own application?
如何从我自己的应用程序读取 MacBookPro 上的电池状态?
Googling has so far only revealed APIs for device drivers to handle power events - there's nothing about user-land processes accessing this information.
到目前为止,谷歌搜索只揭示了设备驱动程序处理电源事件的 API - 没有关于访问这些信息的用户级进程。
thanks.
谢谢。
采纳答案by Ben Gottlieb
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.hfor the contents of the dictionary).
为此,您需要使用 IOKit,特别是IOPowerSources 函数。您可以使用IOPSCopyPowerSourcesInfo()
获取一个 blob,IOPSCopyPowerSourcesList()
然后从中提取一个 CFArray,列出电源。然后IOPSGetPowerSourceDescription()
用来拉出一个字典(字典内容见IOPSKeys.h)。
回答by Gary Chambers
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 命令很有帮助。查询电池状态,具体使用:
$ pmset -g batt
回答by BG Bruno
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
回答by Terry Wilcox
Look at the System Management Controller. I don't have my MBP handy, but I believe you need to look at smc.h
查看系统管理控制器。我手边没有 MBP,但我相信您需要查看 smc.h