views:

314

answers:

3

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

[UIDevice currentDevice] batteryLevel]

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

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

A: 

I'm going to go for the big one: Why?

AS you probably know, you can't really not use UIKit on the iPhone, so I'm not quite sure what you're on about.

Williham Totland
Heh, you are a tiny bit wrong regarding not using UIKit. UIKit is intended for AppStore applications which run with SpringBoard. My application runs instead of SpringBoard. Long story short - UIKit relies on SpringBoard which is not running.
Nick Brooks
So, um, jailbreak and stuff? Good luck with that.
Williham Totland
IOKit on iPhone doesn't have a public-facing API. The public-facing API for getting the battery level is, of course, `-[UIDevice batteryLevel]`. I'm going out on a limb here and assuming you're doing some jailbroken work, which is fine, but the question of *why?* remains.
Jonathan Grynspan
Yup, do you have anything against unoffical things?
Nick Brooks
Why? I am making a SpringBoard replacement
Nick Brooks
It's not that people have anything against it, it's just the majority of developers on here know how to do stuff the official way. You might have more luck asking on a website with more 'jailbreaky' developers.
Tom Irving
Have you *tried* using UIKit in the absence of SpringBoard? Are you certain it requires that process to be running?
Jonathan Grynspan
Jonathan Grynspan - you have a point. UIKit is also huge and it is pointless if you're using it for one API.
Nick Brooks
So use it for other APIs too. No sense in finding a dozen workarounds if UIKit has all the direct routes.
Jonathan Grynspan
Huge? It's a few kilobytes, and it links dynamically…
Time Machine
+3  A: 

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
        ...

In code, that is

IOServiceNameMatching( "AppleSmartBattery" );

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 is part of IOKitTools and it should just compile on iPhone.

Edit:

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

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 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.

drawnonward
UIKit uses IOPMPowerSource. I don't know what to do with it beyond doing IOServiceMatching. :(
Nick Brooks
Thank you so much
Nick Brooks
+1  A: 

I don't have any experience with jailbroken development, but this guide might be helpful.

eman
That should be able to help
Nick Brooks