Skip navigation.
 
mlPolling battery data, missing cycle count
FROM : Claudio Procida
DATE : Tue Aug 22 21:51:03 2006

I'm trying to retrieve informations from the battery using the 
following code (adapted from an example by A.Hillegass on this list):

- (id)init
{
   self = [super init];
   
   SCDynamicStoreContext context = {0, self, NULL, NULL, NULL};
   
   // Create a dynamic store ref
   dynamicStore = SCDynamicStoreCreate(NULL, (CFStringRef)
@"ThisIsATest", storeCallBack, &context);
   
   // What am I interested in receiving notifications
   // about?
   NSArray *array = [NSArray arrayWithObject:@"State:/IOKit/
PowerSources/InternalBattery-0"];
   
   // Register for the notification
   if (!SCDynamicStoreSetNotificationKeys(dynamicStore,
                                         (CFArrayRef)array, NULL)) {
       NSLog(@"failed to set notification keys");
   }
   
   // Create a run loop source
   CFRunLoopSourceRef runLoopSource =
       SCDynamicStoreCreateRunLoopSource(NULL,dynamicStore,0);
   
   // Get hold of the current run loop
   CFRunLoopRef runLoop = CFRunLoopGetCurrent();
   
   // Add the source to the run loop
   CFRunLoopAddSource(runLoop, runLoopSource, kCFRunLoopDefaultMode);
   
   // The source will be retained by the run loop
   CFRelease(runLoopSource);
   
   return self;
}


void storeCallBack( SCDynamicStoreRef    store,
                   CFArrayRef changedKeys,
                   void *info)
{
   // Do something else
}

Since this code just registers my app as a passive listener of the 
power changes, I also added the following method to directly poll the 
battery data when needed:

- (void)loadBatteryData
{
   CFTypeRef psInfo = IOPSCopyPowerSourcesInfo();
   CFArrayRef psList = IOPSCopyPowerSourcesList(psInfo);
   CFDictionaryRef psDict = IOPSGetPowerSourceDescription(
                                                         psInfo,
                                                         CFArrayGetValueAtIndex(psList,batteryIndex));
   // 0 <= batteryIndex < [(NSArray *)psList count]

   NSArray *keys = [(NSDictionary *)psDict allKeys];    
   NSEnumerator *enumerator = [keys objectEnumerator];
   id obj;
   while (obj = [enumerator nextObject])
   {
       // Do something with obj
   }
   
   CFRelease(psInfo);
   CFRelease(psList);    
}

Anyway I still can't figure how I can get some advanced information 
like the cycles count or the voltage. In IOPSKeys.h I see all the 
relevant keys but the psDict dictionary holds no values for those keys.

Any hints? Thanks in advance, as usual

Claudio
--
http://www.emeraldion.it

Related mailsAuthorDate
mlPolling battery data, missing cycle count Claudio Procida Aug 22, 21:51
mlRe: Polling battery data, missing cycle count Claudio Procida Aug 23, 09:27