Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sanduluca opened this issue Jan 16, 2024 · 11 comments

Comments

@sanduluca
Copy link

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();
  };
}, []);
@christocracy
Copy link
Member

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

@sanduluca
Copy link
Author

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.

@christocracy
Copy link
Member

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
Copy link
Member

currentDevice].batteryMonitoringEnabled

This is a global property of the OS.

@sanduluca
Copy link
Author

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
Copy link
Author

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

@sanduluca
Copy link
Author

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

Copy link

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

@github-actions github-actions bot added the stale label Apr 17, 2024
@sanduluca
Copy link
Author

Up

@github-actions github-actions bot removed the stale label Apr 18, 2024
Copy link

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

@github-actions github-actions bot added the stale label May 18, 2024
@sanduluca
Copy link
Author

Up

@github-actions github-actions bot removed the stale label May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants