1

I am getting battery state as UIDeviceBatteryStateCharging even though my iPhone battery is 100% charged.

It should give me state as UIDeviceBatteryStateFull.

My code is as follows.

[[UIDevice currentDevice]setBatteryMonitoringEnabled:YES];
    int i=[[UIDevice currentDevice] batteryState];
    switch (i)
    {
        case UIDeviceBatteryStateUnplugged:
        {
            NSLog(@"UnpluggedKey");

            break;
        }
        case UIDeviceBatteryStateFull:
        {
            NSLog(@"FullKey");

            break;
        }
        case UIDeviceBatteryStateCharging:
        {
            NSLog(@"ChargingKey");
            break;
        }
        default:
        {
            NSLog(@"UnknownKey");
            break;
        }
    }

    NSLog(@"Battery Status is :%d",i);
Larme
  • 24,190
  • 6
  • 51
  • 81
Rakesh
  • 1,177
  • 1
  • 15
  • 31

2 Answers2

2

it may be help you try this.....

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

    // Print current status
    [self batteryStatus];

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

}
 - (void)batteryStatus
{
 NSArray *batteryStatus = [NSArray arrayWithObjects: 
    @"Battery status is unknown.", 
    @"Battery is in use (discharging).", 
    @"Battery is charging.", 
    @"Battery is fully charged.", nil];

    if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateUnknown)
  {
        NSLog(@"%@", [batteryStatus objectAtIndex:0]);
  }
    else
  {     
    NSString *msg = [NSString stringWithFormat:
                                        @"Battery charge level: %0.2f%%\n%@", [[UIDevice currentDevice] batteryLevel] * 100,
                      [batteryStatus objectAtIndex:[[UIDevice currentDevice] batteryState]] ];
        NSLog(@"%@", msg);
    }
}

and your answer is...

you are checking with different enum key value

typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
    UIDeviceBatteryStateUnknown,
    UIDeviceBatteryStateUnplugged,   // on battery, discharging
    UIDeviceBatteryStateCharging,    // plugged in, less than 100%
    UIDeviceBatteryStateFull,        // plugged in, at 100%
};              // available in iPhone 3.0

change your code like this it will work

[[UIDevice currentDevice]setBatteryMonitoringEnabled:YES];
    int i=[[UIDevice currentDevice] batteryState];
    switch (i)
    {
        case UIDeviceBatteryStateUnplugged:
        {
            NSLog(@"UnpluggedKey");

            break;
        }
        case UIDeviceBatteryStateCharging:
        {
            NSLog(@"ChargingKey");
            break;
        }
        case UIDeviceBatteryStateFull:
        {
            NSLog(@"FullKey");

            break;
        }

        default:
        {
            NSLog(@"UnknownKey");
            break;
        }
    }

    NSLog(@"Battery Status is :%d",i);

i hope it will help you

karthikeyan
  • 3,821
  • 3
  • 22
  • 45
0

the ADC app behaves thew same... never show FULL.

https://developer.apple.com/reference/uikit/uidevicebatteryleveldidchangenotification

so Larme seems right.

ingconti
  • 10,876
  • 3
  • 61
  • 48