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

Ankit
  • 1,684
  • 14
  • 14
  • 3
    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. – Alex Brown Sep 10 '12 at 07:57
  • 2
    @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! – tipycalFlow Sep 10 '12 at 09:20
  • @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%. – tipycalFlow Sep 10 '12 at 16:57
  • @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! – tipycalFlow Sep 10 '12 at 17:01
  • 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. – Hot Licks Jun 25 '13 at 22:27

5 Answers5

9

Starting with iOS 8,

[UIDevice currentDevice].batteryLevel

provides 1% accuracy :)

Undo
  • 25,519
  • 37
  • 106
  • 129
Nandan Sawant
  • 136
  • 1
  • 2
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.

ilarele
  • 128
  • 2
  • 6
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.

Matt Clarke
  • 93
  • 1
  • 10
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.

Community
  • 1
  • 1
tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
  • 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. – Alex Brown Sep 10 '12 at 07:55
  • 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 "http://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.. – Ankit Sep 10 '12 at 08:32
  • @AlexBrown The *appearance* of accuracy is for the **user**, not the programmer(of course it's not accurate for the programmers)! – tipycalFlow Sep 10 '12 at 09:04
  • @tipycalFlow : 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.. – Ankit Sep 10 '12 at 09:13
  • @Ankit 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. – tipycalFlow Sep 10 '12 at 09:18
  • 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. – Joachim Prinsloo Sep 25 '12 at 13:06
  • @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... – Ankit Sep 26 '12 at 12:47
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.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186