Я следовал примеру в https://developer.apple.com/library/ios/samplecode/BatteryStatus/Introduction/Intro.html и построил монитор батареи для моего приложения VOIP.iOS BatteryMonitor Предоставление ошибочных значений
Начало:
// Subscribe to battery level and battery state changes
CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), // center
this, // observer
&NotificationHandler, // callback
(CFStringRef)UIDeviceBatteryLevelDidChangeNotification, // name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately); // suspensionBehavior
CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), // center
this, // observer
&NotificationHandler, // callback
(CFStringRef)UIDeviceBatteryStateDidChangeNotification, // name
NULL, // object
CFNotificationSuspensionBehaviorDeliverImmediately); // suspensionBehavior
// Enable battery monitoring. This is required to fetch battery state and level.
dispatch_async(dispatch_get_main_queue(), ^{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
});
Stop:
// Disable battery monitoring
dispatch_async(dispatch_get_main_queue(), ^{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:NO];
});
// Unsubscribe from battery level and battery state changes
CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), // center
this, // observer
(CFStringRef)UIDeviceBatteryLevelDidChangeNotification, // name
NULL); // object
CFNotificationCenterRemoveObserver(CFNotificationCenterGetLocalCenter(), // center
this, // observer
(CFStringRef)UIDeviceBatteryStateDidChangeNotification, // name
NULL); // object
Я старт/стоп монитор батареи в разное время. Иногда я нахожу, что если я запрошу статус батареи, как только я включу монитор батареи, я получу UNKNOWN STATE и уровень батареи -1%.
BatteryState batteryState = BATTERY_STATE_UNKNOWN;
switch (nativeBatteryState)
{
case UIDeviceBatteryStateUnplugged:
batteryState = BATTERY_STATE_UNPLUGGED;
break;
case UIDeviceBatteryStateCharging:
batteryState = BATTERY_STATE_CHARGING;
break;
case UIDeviceBatteryStateFull:
batteryState = BATTERY_STATE_FULL;
break;
case UIDeviceBatteryStateUnknown:
default:
PEX_ASSERT_MSG(nativeBatteryState == UIDeviceBatteryStateUnknown, STREAM_ADHOC(nativeBatteryState));
break;
}
// Map native battery level to unsigned short
unsigned short batteryLevel = 0;
if (batteryState != BATTERY_STATE_UNKNOWN)
{
// Only read battery level if state is known ...
// The native battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged).
// If battery monitoring is not enabled, battery level is –1.0.
float nativeBatteryLevel = [[UIDevice currentDevice] batteryLevel];
PEX_ASSERT_MSG(nativeBatteryLevel >= 0.0 && nativeBatteryLevel <= 1.0, STREAM_ADHOC(nativeBatteryLevel));
batteryLevel = static_cast<unsigned short>(nativeBatteryLevel * 100);
}
Прошло какое-то время, прежде чем я могу запросить фактическое значение батареи? Кроме того, я вижу это чаще, когда мое приложение находится в фоновом режиме. У меня есть приложение VOIP, и я включил его в info.plist по мере необходимости.