iOS BatteryMonitor 给出错误的值

iOS BatteryMonitor Giving Erroneous Values

我按照 https://developer.apple.com/library/ios/samplecode/BatteryStatus/Introduction/Intro.html 中的示例为我的 VOIP 应用构建了一个电池监视器。

开始:

                    // 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];
                    });

停止:

                    // 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

我 start/stop 电池监视器在不同时间。有时我发现,如果我在启用电池监视器后立即查询电池状态,我会得到 UNKNOWN STATE 和 Battery Level 为 -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 中启用它。

问题是由于:

dispatch_async(dispatch_get_main_queue(), ^{ [[UIDevice 当前设备] setBatteryMonitoringEnabled:YES]; });

这是异步的。在我使其同步后,它的表现符合预期。