如何获得电池充电水平专门在mWh(不是百分比)Mac(可可或C) [英] How to get the Battery Charge Level specifically in mWh (not percentage) for Mac (Cocoa or C)

查看:334
本文介绍了如何获得电池充电水平专门在mWh(不是百分比)Mac(可可或C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎解释了这一切。我正在创建一个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.

谢谢!

推荐答案

所以这里是一个代码,直接回答我的问题,这是得到电池充电水平mWh。这不是完美的,但它做的工作。

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;

    }

这篇关于如何获得电池充电水平专门在mWh(不是百分比)Mac(可可或C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆