Getting iPhone Battery Level

I have a simple question. How to get iPhone battery level?

[UIDevice currentDevice] batteryLevel] 

Simple enough? However, there is a small catch - I can not use UIKit . Here is what I wrote so far, but I don't think this 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 sure you need to rely on IOKit to get the 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 that I use:

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

+4
source share
3 answers

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

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

In code, i.e.

 IOServiceNameMatching( "AppleSmartBattery" ); 

I have no idea if the name will be the same for iOS, but I would either try to find a program like ioreg that you can run on iPhone, or write something simple to register the equivalent.

ioreg is part of IOKitTools and it should just compile on the 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 security checks. When you figure out the properties you need, you can get them directly, rather than using IORegistryEntryCreateCFProperties to get them all at once.

IOKit presents everything as a big tree. IOPMPowerSource cannot directly have the required attributes, in which case you will need to go through the child elements. Using something like ioreg, you can tell you what you are looking for before you start coding.

+8
source

I have no experience developing jailbroken, but this guide may be helpful.

+2
source

I'm going to go big: Why?

As you probably know, you really can't use UIKit on iPhone, so I'm not quite sure what you mean.

0
source

Source: https://habr.com/ru/post/1316092/


All Articles