如何获取 Mac(Cocoa 或 C)的电池电量(以 mWh(而非百分比)为单位)

发布于 2024-09-10 18:39:22 字数 113 浏览 9 评论 0原文

标题几乎解释了一切。我正在创建一个 Mac 应用程序,我需要电池电量具体以 mWh 为单位(而不是百分比)。最好用 C 或 Objective C 来做。

谢谢!

The title pretty much explains it all. I'm creating a mac app and I need the battery charge level specifically in mWh (not percentage). Would like to do it in C or Objective C preferably.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩于岁月 2024-09-17 18:39:23

因此,这里有一些代码可以直接回答我的问题,即获取以 mWh 为单位的电池充电水平。它并不完美,但它可以完成工作。

 + (NSString* ) chargeInMWH 
  { 



CFMutableDictionaryRef matching , properties = NULL;
io_registry_entry_t entry = 0;
matching = IOServiceMatching( "IOPMPowerSource" );
entry = IOServiceGetMatchingService( kIOMasterPortDefault , matching );
IORegistryEntryCreateCFProperties( entry , &properties , NULL , 0 );



NSNumber * voltage = [[NSNumber alloc] initWithFloat:1.0];
NSNumber * currentCapacity= [[NSNumber alloc] initWithFloat:1.0];

voltage =  [properties objectForKey:@"Voltage"];    
currentCapacity =  [properties objectForKey:@"CurrentCapacity"];


int floatValue = [voltage intValue];
int floatValue2 = [currentCapacity intValue];
int answer = floatValue * floatValue2;

NSString *theCompleteAnswer = [[NSString alloc] initWithFormat:@"%i",answer];


CFRelease( properties );
IOObjectRelease( entry );


return theCompleteAnswer;

    }

So here is a bit of code that directly answers my question which is getting the Battery Charge Level in mWh. It's not perfect, but it does the job.

 + (NSString* ) chargeInMWH 
  { 



CFMutableDictionaryRef matching , properties = NULL;
io_registry_entry_t entry = 0;
matching = IOServiceMatching( "IOPMPowerSource" );
entry = IOServiceGetMatchingService( kIOMasterPortDefault , matching );
IORegistryEntryCreateCFProperties( entry , &properties , NULL , 0 );



NSNumber * voltage = [[NSNumber alloc] initWithFloat:1.0];
NSNumber * currentCapacity= [[NSNumber alloc] initWithFloat:1.0];

voltage =  [properties objectForKey:@"Voltage"];    
currentCapacity =  [properties objectForKey:@"CurrentCapacity"];


int floatValue = [voltage intValue];
int floatValue2 = [currentCapacity intValue];
int answer = floatValue * floatValue2;

NSString *theCompleteAnswer = [[NSString alloc] initWithFormat:@"%i",answer];


CFRelease( properties );
IOObjectRelease( entry );


return theCompleteAnswer;

    }
左耳近心 2024-09-17 18:39:23

这可能对您没有帮助,但我确实知道 ioreg 命令行程序可以转储有关计算机电池的各种精彩统计数据,包括设计容量、当前容量和当前充电水平毫安时。 (ioreg -l 为您提供了非常详细的输出,所有这些)如果您能弄清楚该程序使用什么 API,您也可以做同样的事情。

或者您可以从您的应用程序调用该程序并捕获输出。

This probably doesn't help you, but I do know that the ioreg command line program can dump out all kinds of wonderful stats about your computer's battery, including design capacity, current capacity, and current charge level in mAh. (ioreg -l gives you really verbose output with all of this) If you can figure out what API that program uses, you could do the same.

Or you could just call the program from your app and capture the output.

清风夜微凉 2024-09-17 18:39:22

抱歉,但我认为硬件没有办法报告这一点。随着电池进入其生命周期,它会随着时间的推移而发生变化,而您一开始所能知道的只是电池安装时的标称容量。

Sorry, but I don't think the hardware has a way to report that. It's going to change over time as the battery gets on in its life cycle, and all you could know at the outset is the nominal capacity of the battery when it was installed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文