获得iPhone的电池电量

Kri*_*oks 4 iphone battery kernel iokit

我有一个简单的问题.我如何获得iPhone的电池电量?

[UIDevice currentDevice] batteryLevel]
Run Code Online (Sandbox Code Playgroud)

够简单吗?然而有一点点 - 我不能使用UIKit.这是我到目前为止所写的内容,但我认为它不起作用:

    // notification port
IONotificationPortRef nport = IONotificationPortCreate(kIOMasterPortDefault);

CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(nport), kCFRunLoopDefaultMode);

CFMutableDictionaryRef matching = IOServiceMatching("IOPMPowerSource");

kern_return_t kr = IOServiceAddMatchingNotification(nport, kIOFirstMatchNotification, matching, (IOServiceMatchingCallback)power, self, &powerIterator);

NSLog(@"Kernel said %d",kr);

power((void*)nport,powerIterator);
Run Code Online (Sandbox Code Playgroud)

我仍然非常确定你必须依靠IOKit来检索电池电量.我的应用程序不使用UIKit,因为它是一个不能使用UIKit的低级应用程序.以下是我使用的框架:

alt text http://img837.imageshack.us/img837/1829/screenshot20100718at211.png

dra*_*ard 8

前段时间我写了一个名为iox的程序,类似于ioreg,除了它让我更容易转换回IOKit调用.当我在笔记本电脑上运行时,我会看到以下电池电量.

AppleSmartBattery - IOService:/AppleACPIPlatformExpert/SMB0/AppleECSMBusController/AppleSmartBatteryManager/AppleSmartBattery
        CurrentCapacity = 11678
        FullyCharged = YES
        DesignCapacity = 13000
        MaxCapacity = 11910
        ...
Run Code Online (Sandbox Code Playgroud)

在代码中,就是这样

IOServiceNameMatching( "AppleSmartBattery" );
Run Code Online (Sandbox Code Playgroud)

我不知道iOS上的名称是否相同,但我会尝试找到像ioreg这样的程序,你可以在iPhone上运行,或者写一些简单的东西来记录等价物.

ioreg是IOKitTools的一部分,它应该只能在iPhone上编译.

编辑:

CFMutableDictionaryRef matching , properties = NULL;
io_registry_entry_t entry = 0;
matching = IOServiceMatching( "IOPMPowerSource" );
//matching = IOServiceNameMatching( "AppleSmartBattery" );
entry = IOServiceGetMatchingService( kIOMasterPortDefault , matching );
IORegistryEntryCreateCFProperties( entry , &properties , NULL , 0 );
NSLog( @"%@" , properties );
CFRelease( properties );
IOObjectRelease( entry );
Run Code Online (Sandbox Code Playgroud)

添加一些安全检查.找到所需的特定属性后,您可以直接获取它们,而不是使用IORegistryEntryCreateCFProperties立即获取它们.

IOKit代表一棵大树.IOPMPowerSource可能没有直接拥有您想要的属性,在这种情况下,您需要遍历子项.在开始编码之前,使用像ioreg这样的东西可以告诉你你在寻找什么.