How to get battery level with 1 % accuracy in ios
Asked Answered
Q

5

12

How to get battery level with 1 % accuracy .

       [UIDevice currentDevice].batteryLevel

Will give battery level with accuracy of 5%. But i recently used Battery Doctor App on my iphone 4S running ios 5.1, which is giving battery level with 1% accuracy . Anyone has any idea , how can we get that accuracy ... I have tried and searched a lot but not getting how they are calculating it .. Thanks in advance...

Quadrireme answered 10/9, 2012 at 7:32 Comment(5)
I think you are talking about precision, rather than accuracy. Both 19/20 and 99/100 are equally accurate, even for the exact same memory charge. Moving to 1 digit increments is more precise, although possibly les accurate (e.g. @tipicalflow's humorous answer below.Kioto
@AlexBrown Humorous is when people laugh at answers to questions they themselves don't have an answer to :D. All you're doing right now is demotivating the OP!Midweek
@AlexBrown He is talking about accuracy, not precision! I don't think you fully understand the concept of iOS battery levels. In a tightly closed iOS environment, the only data available to a developer is the battery power left as a percentage. We do not have the denominator to evaluate the accuracy in question - only a percentage value triggered after an interval of 5%.Midweek
@AlexBrown I see you are applying the idea of accuracy and precision in general. In our special case, we know for sure that the battery level will drop by 5% some time in the future and that time will generally not drop/rise suddenly compared to the previous value. Imagine your laptop battery dying suddenly!Midweek
There is no way to even define "battery level" to that degree of accuracy. Batteries simply are not that predictable. Anything that claims to be yielding 1% accuracy is lying.Ruthie
C
9

Starting with iOS 8,

[UIDevice currentDevice].batteryLevel

provides 1% accuracy :)

Corpora answered 11/12, 2014 at 19:50 Comment(0)
D
7

If you turn off Setting->General->Usage->Battery Percentage, Battery Doctor shows your battery level with %5 precision...So I guess that they are running some image recognition algorithm for the upper right corner of the device, where the battery percent is shown.

Dalhousie answered 11/12, 2012 at 13:53 Comment(0)
J
4

If you're jailbroken, you can simply use

int percent = [(SBUIController*)[objc_getClass("SBUIController") sharedInstance] displayBatteryCapacityAsPercentage];

This method is present on iOS 4.3+, and is probably what Battery Doctor is using.

Jaynes answered 25/6, 2013 at 21:21 Comment(0)
M
1

Answer comes from this question:

You can use an NSTimer to manipulate the values you show giving the appearance of accuracy. For each session the app is active, make a sample set of battery data(like the time required for battery level to fall from 100% to 95%, then 95% to 90%, and so on) and save it on the device itself. Then, use the timer to set off updated(and estimated) battery value according to the time required for the battery level to fall 5% divided by 5(assuming each percentage falls after the same interval, which will vary, but it won't be a noticeable deviation). It's true that battery life falls with time but since we adjust the timer value according to the values saved in the last session, it should continue being pretty much accurate over the life of the device.

Edit- For the initial state, you can provide a default value depending on the iPhone model(3,3G,4,4S). Different values can be calculated for these different devices.

Midweek answered 10/9, 2012 at 7:48 Comment(7)
You can't have the appearance of accuracy, that doesn't mean anything-it's either accurate or it's not. You can make it more precise, although this might either decrease or increase the accuracy, depending on how good your source data and timing model is.Kioto
Yes, we can make our calculation more accurate by this method but for that we have to wait when the app launches for the first time to calculate data and give precise battery level..But have a look on this app "itunes.apple.com/in/app/battery-doctor/id438159515?mt=8" giving battery level accuracy of upto 1 % ...Hope the link help.. so i think there must be a way to calculate level accurately than storing data and interpolating..Quadrireme
@AlexBrown The appearance of accuracy is for the user, not the programmer(of course it's not accurate for the programmers)!Midweek
@Midweek : have you go through the app , i added the link...When app is launched it says 82 % charge. no matter how much data i saved , i can not calculate that much accurate value when app launched.. i hope you are getting what i meant by that.. looking at the working of the app , it make me think there is a better way to find battery level than predicting using stored data..Quadrireme
@Quadrireme Yup, went through it. You'll have to collect some data to observe patterns and then generalize the behavior of batteries. The default value for the initial state should come from this data.Midweek
I agree @Ankit. There must be a better way to calculate this. The NS timer feels like a dirty solution that might become unstable. I am struggeling with exactly the same problem only we are using monotouch. As far as I know Battery Doctor is not the only app that has this. there are a few others as well. Have you found a solution for this? If so, please share and point us in the right direction.Piegari
@JoachimPrinsloo - not a good solution yet , we can get exact percentage using IOKit but unfortunately that is a private framework and app may get rejected...Quadrireme
C
1

In iOS 8.1.x I have noticed that battery level is now reporting the exact battery level for apps now. Previously, UIDevice would report every 5%. There is nothing more for you to do in iOS 8.1.x as [UIDevice currentDevice].batteryLevel should report this automatically now.

Crossbred answered 17/1, 2015 at 4:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.