In my Codename One app I resort to the following iOS native code to know if the battery is charging or full:
-(BOOL)isCharging{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
if ( ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging)
|| ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateFull) ) {
return YES;
}
else {
return NO;
}
}
I the Codename One part I poll every 1000 ms if the battery is charging. It works perfectly on Android. However on iOS the initial state (ie when the app is launched) is kept and it does not get updated even when the battery state changes (plugged / unplugged and vice versa).
So if I start the app with the cable plugged isCharging
returns YES
(true in java) but if I unpluggef the cable isCharging
keeps returning YES
. If I close the app and launch it with the unplugged cable, isCharging
returns NO
and never goes to YES
when I plug the cable in although the iOS toolbar on the the upper left corner shows a charging battery.
Please note : the tests are conducted on an iPhone 4
What can I do to make the method update its value when the battery state changes ?
Any help appreciated,