返回容量值,不是百分比

问题描述 投票:0回答:3

在这里使用此方法如何返回curCapacity和maxCapacity mAh值而不是百分比?

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

无论我尝试什么,我的curCapacity和maxCapacity值都匹配我的电池百分比!

我的第一次尝试

#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
}
objective-c iphone battery
3个回答
2
投票

我已经改变了返回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];;
}

0
投票

好吧,写入的函数只返回一个表示错误或百分比的double。要返回当前和最大容量,您需要重写函数以返回多个值,如下所示:

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

或者将其分解为三个独立的功能,每个功能为每个寻求的容量调用单独的IOPower功能。


-1
投票

IOPSKeys.h的常数来看,可能无法获得电池的原始mAh值。

电源软件可以指定该密钥的单位。对于该电源报告的所有容量,这些单元必须保持一致。客户可以通过将“当前容量”除以“最大容量”来获得剩余电源电池的百分比

© www.soinside.com 2019 - 2024. All rights reserved.