views:

282

answers:

3

Using this method here how would I return the curCapacity and maxCapacity mAh values rather than a percent?

http://blog.coriolis.ch/2009/02/14/reading-the-battery-level-programmatically/comment-page-1/#comment-6089

No matter what I try my curCapacity and maxCapacity values match my battery percentage!

Edit // My 1st attempt

#include "IOPowerSources.h"
#include "IOPSKeys.h"

- (double) batteryLevel // Find current charge percentage
{
#if TARGET_IPHONE_SIMULATOR
    return 80.0f;
#else
    CFTypeRef blob = IOPSCopyPowerSourcesInfo();
    CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

    CFDictionaryRef pSource = NULL;
    const void *psValue;

    int numOfSources = CFArrayGetCount(sources);
    if (numOfSources == 0) {
        NSLog(@"Error in CFArrayGetCount");
        return -1.0f;
    }

    for (int i = 0 ; i < numOfSources ; i++)
    {
            pSource = IOPSGetPowerSourceDescription(blob,     CFArrayGetValueAtIndex(sources, i));
        if (!pSource) {
            NSLog(@"Error in IOPSGetPowerSourceDescription");
            return -1.0f;
        }
        psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));

        int curCapacity = 0;
        int maxCapacity = 0;
        double percent;

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);

        percent = ((double)curCapacity/(double)maxCapacity * 100.0f);

        return percent;
    }
    return -1.0f;
#endif
}




#include "IOPowerSources.h"
#include "IOPSKeys.h"

- (double) curCapacity // Find current capacity
{
#if TARGET_IPHONE_SIMULATOR
    return 460;
    #else
    CFTypeRef blob = IOPSCopyPowerSourcesInfo();
    CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

    CFDictionaryRef pSource = NULL;
    const void *psValue;

    int numOfSources = CFArrayGetCount(sources);
    if (numOfSources == 0) {
        NSLog(@"Error in CFArrayGetCount");
        return -1.0f;
    }

    for (int i = 0 ; i < numOfSources ; i++)
    {
        pSource = IOPSGetPowerSourceDescription(blob,     CFArrayGetValueAtIndex(sources, i));
        if (!pSource) {
            NSLog(@"Error in IOPSGetPowerSourceDescription");
            return -1.0f;
        }
        psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));

        int curCapacity = 0;
        int maxCapacity = 0;
        double curCapacityVal;

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);

        curCapacityVal = (double)curCapacity;

        return curCapacityVal;
    }
    return -1.0f;
#endif
}




#include "IOPowerSources.h"
#include "IOPSKeys.h"

- (double) maxCapacity // Find maximum capacity
{
#if TARGET_IPHONE_SIMULATOR
    return 780;
#else
    CFTypeRef blob = IOPSCopyPowerSourcesInfo();
    CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

    CFDictionaryRef pSource = NULL;
    const void *psValue;

    int numOfSources = CFArrayGetCount(sources);
    if (numOfSources == 0) {
        NSLog(@"Error in CFArrayGetCount");
        return -1.0f;
    }

    for (int i = 0 ; i < numOfSources ; i++)
    {
        pSource = IOPSGetPowerSourceDescription(blob,     CFArrayGetValueAtIndex(sources, i));
        if (!pSource) {
            NSLog(@"Error in IOPSGetPowerSourceDescription");
            return -1.0f;
        }
        psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));

        int curCapacity = 0;
        int maxCapacity = 0;
        double maxCapacityVal;

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);

        psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
        CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);

        maxCapacityVal = (double)maxCapacity;

        return maxCapacityVal;

    }
    return -1.0f;
#endif
}

Many thanks,

Stu

A: 

Judging from the constants in IOPSKeys.h, it might not be possible to get a raw mAh value for the battery.

The power source's software may specify the units for this key. The units must be consisent for all capacities reported by this power source. Clients may derive a percentage of power source battery remaining by dividing "Current Capacity" by "Max Capacity"

Daniel Yankowsky
A: 

Well, the function as written returns only a single double representing either an error or the percentage. To return the current and max capcities you would need to either rewrite the function to return multiple values like so:

-(double) batteryLevel(**currentCapacity,**maxCapacity){...}

Or break it up into three separate functions each of which calls a separate IOPower function for each capacity sought.

TechZen
Thanks for your response. I tried to split it up into three separate functions (see above) but I think I went about it wrongly as it still does not work. Can you show me how it should be done?
Stumf
+1  A: 

I've changed the method to return a dict:

#include "IOPowerSources.h"
#include "IOPSKeys.h"

- (NSDictionary *) batteryData
{
  CFTypeRef blob = IOPSCopyPowerSourcesInfo();
  CFArrayRef sources = IOPSCopyPowerSourcesList(blob);

  CFDictionaryRef pSource = NULL;
  const void *psValue;

  int numOfSources = CFArrayGetCount(sources);
  if (numOfSources == 0) {
    NSLog(@"Error in CFArrayGetCount");
      return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:-1], @"curCapacity", [NSNumber numberWithInt:-1], @"maxCapacity", nil];;
  }

  for (int i = 0 ; i < numOfSources ; i++)
  {
    pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
    if (!pSource) {
      NSLog(@"Error in IOPSGetPowerSourceDescription");
          return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:-1], @"curCapacity", [NSNumber numberWithInt:-1], @"maxCapacity", nil];;
    }
    psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));

    int curCapacity = 0;
    int maxCapacity = 0;
    double percent;

    psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
    CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);

    psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
    CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);

    percent = ((double)curCapacity/(double)maxCapacity * 100.0f);

    return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:curCapacity], @"curCapacity", [NSNumber numberWithInt:maxCapacity], @"maxCapacity", nil];
  }
  return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:-1], @"curCapacity", [NSNumber numberWithInt:-1], @"maxCapacity", nil];;
}
Stephan Burlot