0

I've got problems monitoring battery. In the foreground I can only get the level once and it seems that I don't get the change in the background either. 

If I put a new UIViewController in front of the one monitoring and then get back again the battery level is updated.

-(void)startMonitoringBattery
{
     [UIDevice currentDevice].batteryMonitoringEnabled = YES;

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

     batteryCheckTimer = [NSTimer scheduledTimerWithTimeInterval:(10.0)
                                                             target:self
                                                           selector:@selector(batteryCheck)
                                                           userInfo:nil
                                                            repeats:YES];
}

- (void)batteryStateDidChange:(NSNotification *)notification
{
     [self batteryCheck];
}

- (void)batteryLevelDidChange:(NSNotification *)notification
{
     [self batteryCheck];
}

When this method is called I want to get the current battery level and not the one that it was when I started monitor.

- (void)batteryCheck
{
     float STOP_AT_BATTERY_LEVEL = 50.0;
     float currentBatteryLevel = [UIDevice currentDevice].batteryLevel;
     
     if([UIDevice currentDevice].batteryLevel <= STOP_AT_BATTERY_LEVEL)
     {
          // STOP BATTERY CONSUMING TASK
     }
}

Questions:

  1. Why doesn't batteryStateDidChange and batteryLevelDidChange get called more than once?
  2. Why can't I get the current battery level, with a timer, from [UIDevice currentDevice].batteryLevel even if I've set batteryMonitoringEnabled to true?
  3. Is it possible to monitor battery while the app is in background or the phone is locked? The app is running in background because I record GPS data all the time.
Daniel Nord
  • 535
  • 5
  • 10
  • 1
    I copied your code, and it worked fine for me. The timer's method was called once every 10 seconds, and the delegate methods once in a while (the docs say not more than once per minute, but the interval was longer than that). Where are you calling startMonitoringBattery from? Are you sure the controller this code is in, is not being deallocated? – rdelmar Aug 23 '13 at 16:22
  • Really strange. It didn't work until I added this: 'device = [UIDevice currentDevice]; [device addObserver:self forKeyPath:@"batteryLevel" options:0x0 context:nil];' – Daniel Nord Aug 24 '13 at 11:52

0 Answers0