Return Capacity Values, Not Percent
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!
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, &maxCapacit开发者_开发问答y);
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
}
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];;
}
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.
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"
精彩评论