【问题标题】:Return Capacity Values, Not Percent返回容量值,而不是百分比
【发布时间】:2010-01-05 14:52:03
【问题描述】:

在此处使用此方法如何返回 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
}

【问题讨论】:

  • 请注意,此处使用的所有 API 在 iPhone 上均未记录,如果您将此应用程序提交到 App Store,可能会导致拒绝。我建议放弃对 2.x 的支持并使用新的 UIDevice 电池监控方法。
  • 我知道这一点,但不打算提交。只是练习!还是谢谢。

标签: objective-c iphone battery


【解决方案1】:

我改变了返回字典的方法:

#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];;
}

【讨论】:

    【解决方案2】:

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

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

    或者将其分解为三个独立的函数,每个函数都为所寻求的每个容量调用一个单独的 IOPower 函数。

    【讨论】:

    • 感谢您的回复。我试图将它分成三个独立的功能(见上文),但我认为我做错了,因为它仍然不起作用。你能告诉我应该怎么做吗?
    【解决方案3】:

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

    电源的软件可以指定这个键的单位。对于此电源报告的所有容量,单位必须一致。 客户可以通过将“当前容量”除以“最大容量”得出电源电池剩余百分比

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 2016-08-25
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 2011-12-27
      • 2013-01-03
      相关资源
      最近更新 更多