En utilisant cette méthode, comment puis-je retourner les valeurs mAh curCapacity et maxCapacity plutôt qu'un pourcentage?Valeurs de capacité de retour, pas pourcentage (iPhone)
Peu importe ce que je fais de mon curCapacity et les valeurs qui correspondent à mes maxcapacity pourcentage de la batterie!
Modifier // Ma 1ère tentative
#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
}
Un grand merci,
Stu
Soyez conscient que toutes les API utilisées ici sont sans papier sur l'iPhone, et conduira probablement à un rejet si vous soumettez cette demande au Magasin d'applications. Je recommande d'abandonner le support pour 2.x et d'utiliser les nouvelles méthodes de surveillance de la batterie UIDevice. –
Je suis conscient de cela, mais ne prévois pas de soumettre. juste en train de pratiquer! Merci quand même. – Stumf