How to get the battery charge level specifically in mWh (not a percentage) for Mac (Cocoa or C)

The name pretty much explains it all. I am creating a mac application and I need a battery level , especially in mWh (not a percentage). I would like to do this in C or Objective C.

Thank!

+3
source share
3 answers

So, here is some code that directly answers my question, which gets the Battery Level in mWh. This is not perfect, but he 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;

    }
+1
source

, , . , , , , , .

+1

, , , , ioreg , , . (ioreg -l ). , API , .

.

0
source

Source: https://habr.com/ru/post/1755522/


All Articles