react-native-background-geolocation
react-native-background-geolocation copied to clipboard
iOS Battery Level Issue with `react-native-device-info` (-1 Value) until `BackgroundGeolocation.start()`
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
- Install
react-native-device-infoon an real iOS device. Also installreact-native-background-geolocation. - 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();
};
}, []);
You dont need another plug-in to get the battery level. Background Geolocation provides the battery-level to each recorded location.
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.
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.
currentDevice].batteryMonitoringEnabled
This is a global property of the OS.
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
}
...
}
}
@christocracy is the above proposal a good solution for you to implement ?
@christocracy Hi, did you manage to think about this ? We still need this
This issue is stale because it has been open for 30 days with no activity.
Up
This issue is stale because it has been open for 30 days with no activity.
Up
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.