views:

79

answers:

3

Hello Everyone,

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!

+1  A: 

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.

NSResponder
CoconutBattery (http://www.coconut-flavour.com/coconutbattery/) gets the current capacity and charge level in mAh. Perhaps contacting the developer would be a good idea.
Dave DeLong
@Dave. Thanks! I definitely will ;)
Eric Brotto
A: 

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.

Alex
My answer to [this question](http://stackoverflow.com/questions/3276975/getting-iphones-battery-level/3277140#3277140) gives some details about using IOKit to get the mac battery level that might help you.
drawnonward
@drawnonward. Great answer! You should have posted it as an answer so I could have accepted it. I've taken the snippet that is placed in the Edit of your answer and ran it. Works just fine. Now what if I want to print just certain values of the properites, such as DesignCapacity. Thanks for the help!
Eric Brotto
A: 

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;

    }
Eric Brotto