react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

iOS Battery Level Issue with `react-native-device-info` (-1 Value) until `BackgroundGeolocation.start()`

Open sanduluca opened this issue 1 year ago • 12 comments

Your Environment

  • Plugin version: 4.12.1
  • Platform: iOS
  • OS version: 17.2.1
  • Device manufacturer / model: iPhone XR
  • React Native version (react-native -v): 0.72.4
  • Plugin config

Expected Behavior

Actual Behavior

We use react-native-device-info plugin, version 10.8.0. We need to get battery level so we use https://github.com/react-native-device-info/react-native-device-info?tab=readme-ov-file#getbatterylevel api, but it returns -1. In their documentation its says to add this [UIDevice currentDevice].batteryMonitoringEnabled = true; to AppDelegate, but i still get -1 level on a real device. But i found that as soon as i call BackgroundGeolocation.start(), the getBatteryLevel returns the real device battery level and I dont get the Battery monitoring is not enabled. You need to enable monitoring with [UIDevice currentDevice].batteryMonitoringEnabled = TRUE warning

Steps to Reproduce

  1. Install react-native-device-info on an real iOS device. Also install react-native-background-geolocation.
  2. Try get battery level or power state using react-native-device-info

Context

useEffect(() => {
  BackgroundGeolocation.start()
    .then(() => {
      console.log("Started geolocation");
      isBatteryCharging().then((isCharging) =>
        console.log("isBatteryCharging()", isCharging)
      );
      getPowerState().then((state) => console.log("getPowerState()", state));
      getBatteryLevel()
        .then((level) => {
          console.log("getBatteryLevel1", level);
        })
        .catch((e) => {
          console.log("error on getBatteryLevel", e);
        });
    })
    .catch((e) => console.log("Error starting", e));

  return () => {
    BackgroundGeolocation.stop();
  };
}, []);

sanduluca avatar Jan 16 '24 08:01 sanduluca

You dont need another plug-in to get the battery level. Background Geolocation provides the battery-level to each recorded location.

christocracy avatar Jan 16 '24 13:01 christocracy

You dont need another plug-in to get the battery level. Background Geolocation provides the battery-level to each recorded location.

We use this library not only for battery api, but in our case we need user battery level outside of user location context.

sanduluca avatar Jan 16 '24 13:01 sanduluca

When BackgroundGeolocation.start() is executed, it runs currentDevice].batteryMonitoringEnabled = true.

When BackgroundGeolocation.stop() is executed, (or the plug-in boots in State.enabled == false) it runs currentDevice].batteryMonitoringEnabled = false.

christocracy avatar Jan 16 '24 13:01 christocracy

currentDevice].batteryMonitoringEnabled

This is a global property of the OS.

christocracy avatar Jan 16 '24 13:01 christocracy

Will this be a good solution for you to implement so you do not reset this global property ?

class Geolocation {
  
  private bool canChangeBatteryMonitoringProperty = false

  public function start(){
    if (! batteryMonitoringEnabled){
      batteryMonitoringEnabled = true
      canChangeBatteryMonitoringProperty = true
    }
  }

  public function stop(){
    ...
    if (canChangeBatteryMonitoringProperty){
      batteryMonitoringEnabled = false
      canChangeBatteryMonitoringProperty = false
    }
    ...
  }

}

sanduluca avatar Jan 17 '24 14:01 sanduluca

@christocracy is the above proposal a good solution for you to implement ?

sanduluca avatar Jan 25 '24 16:01 sanduluca

@christocracy Hi, did you manage to think about this ? We still need this

sanduluca avatar Mar 12 '24 08:03 sanduluca

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Apr 17 '24 01:04 github-actions[bot]

Up

sanduluca avatar Apr 17 '24 09:04 sanduluca

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar May 18 '24 01:05 github-actions[bot]

Up

sanduluca avatar May 18 '24 11:05 sanduluca

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Jun 18 '24 01:06 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jul 02 '24 01:07 github-actions[bot]