开发者

How to get the Battery Charge Level specifically in mWh (not percentage) for Mac (Cocoa or C)

The title pretty much explai开发者_如何学JAVAns 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!


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.


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;

    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜