获得iPhone的电池电量 [英] Getting iPhone's battery level

查看:283
本文介绍了获得iPhone的电池电量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple question. How do I get iPhone's battery level?

[UIDevice currentDevice] batteryLevel]

够简单吗?但是有一个小抓 - 我不能使用UIKit 。这是我到目前为止写的,但我认为它不起作用:

Simple enough? However there is a little catch - I can't use UIKit. Here is what I wrote so far, but I don't think it works:

    // 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);

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

I'm still pretty sure you have to rely on IOKit in order to retrieve battery level. My application does not use UIKit as it is a low-level application in which UIKit cannot be used. Here are the frameworks I am using :

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

推荐答案

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

A while ago I wrote a program called iox that is similar to ioreg, except it makes it easier for me to translate back to IOKit calls. When I run that on my laptop, I see the following with a battery level.

AppleSmartBattery - IOService:/AppleACPIPlatformExpert/SMB0/AppleECSMBusController/AppleSmartBatteryManager/AppleSmartBattery
        CurrentCapacity = 11678
        FullyCharged = YES
        DesignCapacity = 13000
        MaxCapacity = 11910
        ...

在代码中,这是

IOServiceNameMatching( "AppleSmartBattery" );

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

I have no idea if the name would be the same on iOS, but I would try either finding a program like ioreg that you can run on the iPhone, or writing something simple to log the equivalent.

ioreg是 IOKitTools 它应该只能在iPhone上编译。

ioreg is part of IOKitTools and it should just compile on 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 );

添加一些安全检查。一旦你找到了你想要的特定属性,就可以直接获取它们,而不是使用IORegistryEntryCreateCFProperties来一次性获取它们。

Add some safety checks. Once you figure out the specific properties you want, you can get them directly instead of using IORegistryEntryCreateCFProperties to get them all at once.

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

IOKit represents everything as a big tree. IOPMPowerSource may not directly have the attributes you want, in which case you would need to iterate through the children. Using something like ioreg can tell you what you are looking for before you start coding.

这篇关于获得iPhone的电池电量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆