返回容量值,而不是百分比

发布于 2024-08-17 01:07:29 字数 4542 浏览 5 评论 0原文

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

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, &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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋日私语 2024-08-24 01:07:29

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

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

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];;
}
青衫负雪 2024-08-24 01:07:29

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

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

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

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.

南城追梦 2024-08-24 01:07:29

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

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

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文