Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 247
cat Avatar asked Aug 04 '12 09:08

cat


People also ask

Do you need 50% Battery to update iOS?

Connect to WiFi and charge your battery Sometimes it's the little things that will block an iOS update. You should have a strong, reliable WiFi connection and your iPhone needs to have at least 50 percent of battery life remaining.

How do I make my iPhone Battery level?

See the iPhone battery percentage in the status bar On an iPhone with Face ID: Swipe down from the top-right corner. On an iPhone with a Home button: Go to Settings > Battery, then turn on Battery Percentage.

How do I get Battery percentage on iOS Swift?

Xcode 11 • Swift 5.1 Then you can create a computed property to return the battery level: Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled. If battery monitoring is not enabled, battery state is UIDevice.


4 Answers

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

like image 177
nst Avatar answered Oct 05 '22 02:10

nst


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.

like image 25
bitmapdata.com Avatar answered Oct 05 '22 00:10

bitmapdata.com


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;
like image 21
user1617027 Avatar answered Oct 05 '22 00:10

user1617027


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.

like image 27
maxwell Avatar answered Oct 05 '22 01:10

maxwell