react-native-background-geolocation
react-native-background-geolocation copied to clipboard
BackgroundGeolocation.ready call not setting state.enabled to true and intermittent geolocation failures on real devices
Your Environment
- Plugin version: 4.15.2
- Platform: Android
- OS version: android 12
- Device manufacturer / model: realme
- React Native version (
react-native -v): 0.72.4 - Plugin config
import React from 'react';
import {Switch, Text, View} from 'react-native';
import BackgroundGeolocation, {
Location,
Subscription,
} from 'react-native-background-geolocation';
// import React, {useEffect, useState} from 'react';
import {sendLocations} from '../../../services/user';
import {useSelector} from 'react-redux';
import {useToast} from '../CustomToast/ToastContext';
const LocationUpdater = () => {
const {showToast} = useToast();
const userId = useSelector(state => state.profileSetup.userId);
const token = useSelector(state => state.auth.token);
const [enabled, setEnabled] = React.useState(true);
const sendLocationToServer = async location => {
try {
if (!userId) throw new Error('User id not available');
const data = {
onboardingDataId: userId,
location,
};
const res = await sendLocations({token, data});
showToast('success', 'Location sent', res.message);
} catch (error) {
// console.error('Error sending location to server:', error);
showToast('error', 'Issue sending location', error.message);
}
};
React.useEffect(() => {
/// 1. Subscribe to events.
const onLocation = BackgroundGeolocation.onLocation(async location => {
console.warn('[onLocation]', location);
await sendLocationToServer(location);
});
const onMotionChange = BackgroundGeolocation.onMotionChange(event => {
console.log('[onMotionChange]', event);
});
const onActivityChange = BackgroundGeolocation.onActivityChange(event => {
console.log('[onActivityChange]', event);
});
const onProviderChange = BackgroundGeolocation.onProviderChange(event => {
console.log('[onProviderChange]', event);
});
/// 2. ready the plugin.
BackgroundGeolocation.ready({
// Geolocation Config
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: 1,
// Activity Recognition
stopTimeout: 5,
// Application config
debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
startOnBoot: true, // <-- Auto start tracking when device is powered-up.
}).then(state => {
// setEnabled(state.enabled);
console.warn(
'- BackgroundGeolocation is configured and ready: ',
state.enabled,
);
});
return () => {
// Remove BackgroundGeolocation event-subscribers when the View is removed or refreshed
// during development live-reload. Without this, event-listeners will accumulate with
// each refresh during live-reload.
onLocation.remove();
onMotionChange.remove();
onActivityChange.remove();
onProviderChange.remove();
};
}, [userId]);
/// 3. start / stop BackgroundGeolocation
React.useEffect(() => {
if (enabled) {
BackgroundGeolocation.start();
} else {
BackgroundGeolocation.stop();
}
}, [enabled, userId]);
return null;
};
export default LocationUpdater;
Expected Behavior
It should request permission on real devices, and it should also acquire background location and call the API to retrieve the location.
Actual Behavior
It's not asking for permission when the mobile app is on, and even when we manually enable all permissions, it's still not retrieving the location from both the background and foreground.
Steps to Reproduce
Context
We have attempted to retrieve the location and send it to the backend, essentially.
Debug logs
Logs
05-06 17:52:01.294 2280 2280 D TSLocationManager: 🎾 start [ActivityRecognitionService startId: 1, eventCount: 1]
05-06 17:52:01.302 2280 2480 D TSLocationManager: [c.t.l.s.ActivityRecognitionService a]
05-06 17:52:01.302 2280 2480 D TSLocationManager: 🚘 ️DetectedActivity [type=STILL, confidence=100]
05-06 17:52:01.339 2280 2480 D TSLocationManager: [c.t.l.service.AbstractService a]
05-06 17:52:01.339 2280 2480 D TSLocationManager: ⚙️︎ FINISH [ActivityRecognitionService startId: 1, eventCount: 0, sticky: false]
05-06 17:52:01.588 2280 2280 D TSLocationManager: [c.t.l.service.AbstractService f]
05-06 17:52:01.588 2280 2280 D TSLocationManager: ⚙️︎ ActivityRecognitionService.stopSelfResult(1): true
05-06 17:52:01.593 2280 2280 D TSLocationManager: [c.t.l.service.AbstractService onDestroy]
05-06 17:52:01.593 2280 2280 D TSLocationManager: 🔴 ActivityRecognitionService stopped
Calling .start() -> State.enabled: true Calling .stop() -> State.enabled: false
We are experiencing issues with real devices when testing the release version. While the simulator works as expected in the foreground and provides the location accurately, on real devices, even after moving 2-3 miles, we are unable to obtain the location. Additionally, upon launching the app on a real device, it does not prompt for background/location permission, which it does on the simulator.
Thanks.
Calling .start() -> State.enabled: true Calling .stop() -> State.enabled: false
See wiki “Debugging”. Learn how to obtain the plug-in log database using method .emailLog.
also see https://dontkillmyapp.com
@christocracy Can you please help me solve the version issue with googlePlayServicesVersion? I changed it from 17.0.0 to 21.0.1 according to the reference, but now the android app crashes when i open it. Please help me resolve this version issue and here is the my android/build.gradle file code.
android/build.gradle
buildscript {
ext {
//new imp
// googlePlayServicesVersion = "+"
// firebaseMessagingVersion = "21.1.0"
buildToolsVersion = "33.0.0"
minSdkVersion = 26
compileSdkVersion = 33
targetSdkVersion = 33
// We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
ndkVersion = "23.1.7779620"
appCompatVersion = "1.4.2"
googlePlayServicesVersion = "21.0.1"
// googlePlayServicesVersion = "20.0.0"
// googlePlayServicesIidVersion = "17.0.0"
// googlePlayServicesVersion = "17.0.0"
}
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" } // Add this line
}
dependencies {
classpath("com.android.tools.build:gradle")
classpath("com.facebook.react:react-native-gradle-plugin")
// new imp
classpath 'com.google.gms:google-services:4.3.15'
}
allprojects {
repositories {
google()
mavenCentral()
// FaceTecSDK: Add directory to find native .aar file
flatDir {
dirs 'libs'
}
// Required for react-native-background-geolocation
maven { url("${project(':react-native-background-geolocation').projectDir}/libs") }
maven { url 'https://developer.huawei.com/repo/' }
// Required for react-native-background-fetch
maven { url("${project(':react-native-background-fetch').projectDir}/libs") }
}
}
}
When you have a crash, the first thing to do is fetch the stacktrace from $ adb logcat. See wiki “Debugging”.
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.