2

I am monitoring for battery events UIDeviceBatteryLevelDidChangeNotification and UIDeviceBatteryStateDidChangeNotification. I am looking specifically for 20% and 10%, however, I am seeing these events fire off at 23%, 13% which they should be hitting at 0.05 intervals or every 5%.

Question

Does anyone else experience this, or is there a better way to ensure the levels are more accurate?

Example

-(void)startMonitoringForBatteryChanges
{
    // Enable monitoring of battery status
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

    // Request to be notified when battery charge or state changes
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBatteryStatus) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBatteryStatus) name:UIDeviceBatteryStateDidChangeNotification object:nil];
}

- (void)checkBatteryStatus
{
    float warningLevel = [[UIDevice currentDevice] batteryLevel] * 100;

    if ( warningLevel == 20.0 ) // 20%
    {
        NSLog(@"20% Warning.");
    }

    if ( warningLevel == 10.0 ) // 10%
    {
        NSLog(@"10% Warning.");
    }
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • You could implement a timer and query the battery level manually (enabling batteryMonitoring before and disabling after reading the `[[UIDevice currentDevice] batteryLevel]`). Note that it can still (just tested) happen that the reported level is not the same as level indicated on status bar. – Rok Jarc Oct 17 '12 at 04:13
  • Not optimal as thats the point of `UIDeviceBatteryStateDidChangeNotification` and the other, to notify when there are changes. – WrightsCS Oct 17 '12 at 04:15
  • You're definetly right. But i think it's the only way to get more frequent updates thus resolution that you're after. By the way: did you check what the status bar indicator says when you get 13% and 23% ... updates? – Rok Jarc Oct 17 '12 at 04:16
  • Yes, the issue is when the Console prints the NSLogs from the example above, the statusbar says 23% / 13% respectively. – WrightsCS Oct 17 '12 at 04:34
  • I see - this discrepancy is a known issue. Similar problem in this (old) blog: http://mobiledevelopertips.com/device/display-battery-state-and-level-of-charge.html – Rok Jarc Oct 17 '12 at 04:40

0 Answers0