react-native-background-geolocation
react-native-background-geolocation copied to clipboard
BackgroundGeolocation.onActivityChange is not detecting any activity on android
Your Environment
- Plugin version: ^4.9.4
- Platform: Android
- OS version: 10.0.1
- Device manufacturer / model: One Plus 5T
- React Native version (
react-native -v): 0.68.5 - Plugin config
import React, { useEffect, useState } from 'react';
import BackgroundGeolocation, { Subscription } from 'react-native-background-geolocation';
import { Text } from 'components/commons';
import constStyles from 'constants/Styles';
import colors from 'constants/Colors';
const App = () => {
const [message, setMessage] = useState('Please wait...');
useEffect(() => {
const onActivityChange: Subscription = BackgroundGeolocation.onActivityChange(event => {
console.log('[onMotionChange]', event);
setMessage(`${event.activity}: ${event.confidence}%`);
});
// eslint-disable-next-line @typescript-eslint/no-floating-promises
BackgroundGeolocation.ready({
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: 10,
activityRecognitionInterval: 1000,
debug: true, // <-- enable debug sounds/notifications
}).then(state => {
console.log('BackgroundGeolocation is configured and ready: ', state.enabled, state);
if (!state.enabled) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
BackgroundGeolocation.start(() => console.log('BackgroundGeolocation Start success', state.enabled));
}
});
return () => {
onActivityChange.remove();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
BackgroundGeolocation.stop(() => console.log('BackgroundGeolocation Stop success'));
};
}, []);
return (
<Text style={constStyles.font32} color={colors.skyblue}>
{message}
</Text>
);
};
export default App;
Expected Behavior
I followed all the instructions mentioned here BackgroundGeolocation.onActivityChange didn't detect anything on android, but it works on ios.
Actual Behavior
It doesn't log anything inside the onActivityChange method
Steps to Reproduce
Context
Trying to display if it detect if i'm on a vehicle on android phone
Debug logs
Logs
PASTE_YOUR_LOGS_HERE
Are you observing the plugin logs in $ adb logcat? See wiki Debugging.
also see https://dontkillmyapp.com
Are you observing the plugin logs in
$ adb logcat? See wiki Debugging.also see https://dontkillmyapp.com
Only logs from ready. Could it be because activity recognition was only introduced from api level 29?
https://developer.android.com/reference/android/Manifest.permission#ACTIVITY_RECOGNITION
Could it be because activity recognition was only introduced from api level 29?
No. I develop and test every day with api 33.
this is not a plugin issue.
see wiki Debugging and observe logcat.
It seems like only onLocation works on android and from that i'm able to access activity like this
BackgroundGeolocation.onLocation(location => {
const {
activity: { type, confidence },
} = location;
setMessage(`${type}: ${confidence}%`);
});
Are you testing on a variety of devices?
Are you testing on a variety of devices?
Yes, One plus 5T and Redmi Note 8. Will test on few more
Experiencing similar issue, on versions 4.8.x it works as expected, however on versions 4.9.x, 4.10.x, 4.11.x onActivityChange is not called. Problem reported from multiple android phones, also reproduced on Android emulator with virtual sensors (device pose).
also reproduced on Android emulator with virtual sensors (device pose).
The Android Motion API does not operate in the emulator.
I suggest you consult with https://dontkillmyapp.com and reboot your device.
With version 4.8.x I can even simulate different kind of activity (walking/running/in_vehicle) just by playing with virtual accelerometer in Android emulator. Not the case for higher versions, but that is most probably as you mentioned due "Motion API does not operate in the emulator". Our app explicitly requests to be removed from battery saver/optimizer, might that be not enough. I'll try to collect more info on the issue. Thanks
See wiki "Debugging". See API docs Logger. Learn to fetch logs from plugin using .emailLog method.
I do not have any issues with my test devices. Also ensure you're using the latest version of playServicesLocationVersion. The latest version of play-services-location is 21.0.1.
Nothing of consequence has changed in the plugin with-respect-to the motion API since 4.9.x
Also ensure you're using the latest version of
playServicesLocationVersion. The latest version ofplay-services-locationis21.0.1.
@christocracy how i can check verison of play-services-location? in logs i can only see "Google Play Services: connected (version code:12451000)"
I'm asking because I don't fully understand how the selection of a play-services-location version works in build.gradle.
- The plugin reads the
playServicesLocationVersionin itsbuild.gradle. - The default value (if you don't explicitly control this variable in your
extvars in your app's ownbuild.gradle) is 20.0.0 - To see all available versions of
play-services-location, consult with Google's maven repo at maven.google.com
@christocracy are there any restrictions in the library depending on how we perform the installation process? I see 4 files related to Android installation(INSTALL-ANDROID-AUTO INSTALL-ANDROID-RNPM INSTALL-ANDROID INSTALL-EXPO), and they have different playServicesLocationVersion versions. Specifically, I'm interested in whether I can use version 21.0.1 in conjunction with Expo, making the necessary change in the expo-gradle-ext-vars section.
The only acceptable Setup Guides are linked in the README. Anything not linked are deprecated and due to be removed.
There is nothing mysterious about playServicesLocationVersion 21+. Google simply made a breaking change in v21, modifying a particular java Class to an Interface. Importing v21+ requires that EVERY OTHER PLUGIN importing play-services-location must also import v21+. Not every plugin takes care to offer a convenient way to control the imported version as done here in my plugin with ext.playServicesLocationVersion -- some other plugins naïvely hard-code the imported version.
When one plugin imports v20 and another imports v21, your app will have a runtime error.
This plugin is perfectly willing and able to operate with either v20 or v21. The question is, are ALL your OTHER plugins also able to do this?
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.