2

Wondering if there are references beyond the Apple tech stats for calculating battery life. I've tried comparing some existing battery apps (battery % left * Apple's figures) and I dont come up with the same answers sometimes. Also there are stats for using 2G cell (as opposed to 3G) and I dont see anything on Apple for 2G battery life.

Of course, some of the apps claim they are 'the most accurate'... but I dont see that happening unless someone has a source for very accurate stats.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
kindaran
  • 491
  • 1
  • 6
  • 15
  • This isn't really a development question and overall battery life is subject to an incredibly large range of variables. The only real measure you've got is to run it down while it's performing a representative workload. – Benno Sep 24 '09 at 03:06
  • If the question is how to calculate it programmatically, it certainly is a development question. But you do have a point about calculating the rate of burndown... more precisely, the OP should be asking (say) how many mAh is available. A rate could be calculated by sampling this value over a period of time. – Shaggy Frog Sep 24 '09 at 03:30
  • Well, yes I am here because I am interested in building myself a battery life app. So wanted to ask the community about calculations. – kindaran Sep 24 '09 at 03:35
  • Why do so many people keep asking this question, or is it the same person asking the same question over and over again? – Jay Sep 24 '09 at 08:42
  • I tried searching stackoverflow, as well as google. If you can point me to previous questions/answers, that would be great. – kindaran Sep 24 '09 at 11:39

1 Answers1

18

The API allows you to register to receive notifications for changes to the battery level. It only reports a change at 5% increments up or down, but you can use a timer and measure the time between two changes (or initial battery level and first change). Here's how you register for the notifications:

// Use this call to get the current battery level as a float
// [[UIDevice currentDevice] batteryLevel]

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(batteryStateDidChange:)
                                             name:UIDeviceBatteryStateDidChangeNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(batteryLevelDidChange:)
                                             name:UIDeviceBatteryLevelDidChangeNotification
                                           object:nil];

The first notification tells you the current state, e.g. unplugged, charging, or full. The second will get triggered whenever a 5% increment is reached.

Seems to me that if all you're given is change notifications at 5% changes up or down, accuracy is not something you can calculate very well or quickly. A 5% change could take a very long time if the device isn't doing anything.

Maybe you can monitor [[UIDevice currentDevice] batteryLevel] with a timer, however, while I haven't tried it I think it only gets updated at this same 5% increment.

Matt Long
  • 24,438
  • 4
  • 73
  • 99
  • I appreciate the post. I was aware of the SDK code. I'm more interested in info such as where do these battery life apps get the 2G cell talk time from (Apple provides 3G talk time, but I dont see 2G). Obviously I can reverse engineer the equation the apps use, but was hoping for something better. – kindaran Sep 24 '09 at 11:48