0
我正在製作一個程序,可以在插入電腦時監視iPhone的充電狀態。我如何在VB,BAT或C++中執行此操作?如何在插入電腦時監控iPhone的充電狀態?
我正在製作一個程序,可以在插入電腦時監視iPhone的充電狀態。我如何在VB,BAT或C++中執行此操作?如何在插入電腦時監控iPhone的充電狀態?
您可以設置(在ObjC)的定時器如下:
-(NSString *)getBatteryPercent
{
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
CFDictionaryRef pSource = NULL;
const void *psValue;
int i;
int curCapacity = 0;
int maxCapacity = 0;
int percent = 0;
int numOfSources = CFArrayGetCount(sources);
//if (numOfSources == 0) return 1;
for (i = 0 ; i < numOfSources ; i++)
{
pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
percent = (int)((double)curCapacity/(double)maxCapacity * 100);
}
return [NSString stringWithFormat:@"%d",percent];
}
爲了讓電池的狀態:
-(NSString*)batteryStateStatus:(UIDeviceBatteryState)state
{
switch (state)
{
case UIDeviceBatteryStateUnknown:
return @"Unknown";
break;
case UIDeviceBatteryStateUnplugged:
return @"Unplugged";
break;
case UIDeviceBatteryStateCharging:
return @"Charging";
break;
case UIDeviceBatteryStateFull:
return @"Charged";
break;
}
return nil;
}
編輯:
需要IOKit.framework
#include <IOKit/IOKitLib.h>
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/ps/IOPSKeys.h>
你能提供的不僅僅是監視充電狀態 – robobooga 2011-01-26 01:46:14
我認爲你需要購買一臺Mac,非公開協議,以及開發許可做一個程序等更多細節。您還需要學習Cocoa/Objective C(http://developer.apple.com/technologies/mac/cocoa.html) – IDWMaster 2011-01-26 01:46:21