How to get real time battery level on iOS with a 1% granularity
Asked Answered
R

7

18

I'm using this function to get current battery level of device:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
UIDevice *myDevice = [UIDevice currentDevice];

[myDevice setBatteryMonitoringEnabled:YES];
double batLeft = (float)[myDevice batteryLevel]; 
NSLog(@"%f",batLeft);

but the result has a 5% granularity. Example: when the phone battery is at 88%, it only logs a value of 0.85. batteryLevel only returns values in increments of 0.05. For example: 0.85, 0.9, 0.95 and never returns values like 0.82 or 0.83.

Is there any solution to get a percentage with a higher precision?

Refutative answered 4/8, 2012 at 9:0 Comment(1)
For the record: I just check your code with the iPhone 6 Plus and it does show all scalar levels, like 0.49 etcCorrincorrina
W
10

check out this site : Reading the battery level programmatically

but, carefully use. all of the APIs used here are undocumented on the iPhone, and will probably lead to a rejection if you submit this application to the App Store. Although battery charge status is not exactly, I'd recommend using the UIDevice battery monitoring methods.

Writhen answered 4/8, 2012 at 9:17 Comment(1)
Thank you for your link. I've checked out it. I saw this line: "Don't forget to remove the headers and libIOKit.A.dylib from your code before shipping!", did it mean after done, I can remove libIOKit.A.dylib and remove the headers from my code to upload to Apple Store?Refutative
S
33

There are at least four different ways to read the battery level, and all four ways may return different values.

Here is a chart of these values through time.

The values were recorded with this iOS project: https://github.com/nst/BatteryChart

Please check out the code for reference.

iPhone 5 Battery

Sumptuous answered 22/5, 2013 at 15:4 Comment(1)
Does this app remain running and reporting percentages in the background? Or do you simply leave the app running?Tillo
W
10

check out this site : Reading the battery level programmatically

but, carefully use. all of the APIs used here are undocumented on the iPhone, and will probably lead to a rejection if you submit this application to the App Store. Although battery charge status is not exactly, I'd recommend using the UIDevice battery monitoring methods.

Writhen answered 4/8, 2012 at 9:17 Comment(1)
Thank you for your link. I've checked out it. I saw this line: "Don't forget to remove the headers and libIOKit.A.dylib from your code before shipping!", did it mean after done, I can remove libIOKit.A.dylib and remove the headers from my code to upload to Apple Store?Refutative
H
4
UIDevice *myDevice = [UIDevice currentDevice];    
[myDevice setBatteryMonitoringEnabled:YES];

double batLeft = (float)[myDevice batteryLevel] * 100;
NSLog(@"%.f", batLeft);    

NSString * levelLabel = [NSString stringWithFormat:@"%.f%%", batLeft];    
lblLevel.text = levelLabel;
Hsu answered 22/8, 2012 at 12:55 Comment(2)
Your first line is superfluous since you do exactly the same thing on the third line of code.Bushtit
@VictorEngel I just edited it to remove the duplicate line of code. Thanks for pointing that out.Heilungkiang
Z
3

Swift version to get the battery level:

UIDevice.current.isBatteryMonitoringEnabled = true
let batteryLevel = UIDevice.current.batteryLevel 

batteryLevel return 0,39; 0,40 values for me.

Zorn answered 21/2, 2019 at 14:28 Comment(4)
It is not working for me? Do I need to perform any other configuration? I am getting -1 as an answer.Findlay
Did you write this code? UIDevice.current.isBatteryMonitoringEnabled = true If you didn't write this you will get -1.Zorn
Yes, I wrote this line "UIDevice.current.isBatteryMonitoringEnabled = true". Also, I am new to developing iOS Apps. I am calling this code form viewDIdLoad() method under ViewController. Also, I am connected to a simulator, not an actual phone. Will that make a difference?Findlay
Of course, the simulator will return "-1". Use this code only for real devices.Zorn
E
2

The answers above are very good, but they are all in Obj-C, I have used these with other examples to do the same task on MonoTouch, so I am putting my code here in case anybody needs it:

try
{
    UIDevice.CurrentDevice.BatteryMonitoringEnabled = true;
    _Battery.Level = (int)(UIDevice.CurrentDevice.BatteryLevel * IOSBatteryLevelScalingFactor);
    _Battery.State = UIDevice.CurrentDevice.BatteryState;
}
catch (Exception e)
{
    ExceptionHandler.HandleException(e, "BatteryState.Update");
    throw new BatteryUpdateException();
}
finally
{
    UIDevice.CurrentDevice.BatteryMonitoringEnabled = false;
}

I also have a full post on my blog to give all the details in here

Eclipse answered 20/1, 2014 at 5:51 Comment(0)
C
1

You can find a perfect answer here

Xcode: 11.4 Swift: 5.2

Click Here

Calling answered 21/4, 2020 at 4:46 Comment(0)
A
0

If you are using swift 5.1 or greater this code will definitely work for you.

Step 1:- To get started, first enable the isBatteryMonitoringEnabled property of the current device, like this:-

UIDevice.current.isBatteryMonitoringEnabled = true

Step 2:- You can now read the current battery level

let level = UIDevice.current.batteryLevel
print(level)
Argentiferous answered 5/3, 2020 at 6:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.