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

BackgroundGeolocation.onActivityChange is not detecting any activity on android

Open tsachit opened this issue 2 years ago • 16 comments

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

tsachit avatar Dec 28 '22 17:12 tsachit

Are you observing the plugin logs in $ adb logcat? See wiki Debugging.

also see https://dontkillmyapp.com

christocracy avatar Dec 28 '22 17:12 christocracy

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

tsachit avatar Dec 30 '22 03:12 tsachit

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.

christocracy avatar Dec 30 '22 03:12 christocracy

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}%`);
    });

tsachit avatar Dec 30 '22 05:12 tsachit

Are you testing on a variety of devices?

christocracy avatar Dec 30 '22 11:12 christocracy

Are you testing on a variety of devices?

Yes, One plus 5T and Redmi Note 8. Will test on few more

tsachit avatar Dec 30 '22 11:12 tsachit

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).

ezwaydev avatar May 31 '23 12:05 ezwaydev

also reproduced on Android emulator with virtual sensors (device pose).

The Android Motion API does not operate in the emulator.

christocracy avatar May 31 '23 12:05 christocracy

I suggest you consult with https://dontkillmyapp.com and reboot your device.

christocracy avatar May 31 '23 12:05 christocracy

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

ezwaydev avatar May 31 '23 13:05 ezwaydev

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.

christocracy avatar May 31 '23 13:05 christocracy

Nothing of consequence has changed in the plugin with-respect-to the motion API since 4.9.x

christocracy avatar May 31 '23 13:05 christocracy

Also ensure you're using the latest version of playServicesLocationVersion. The latest version of play-services-location is 21.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.

superyarik avatar Oct 27 '23 10:10 superyarik

  • The plugin reads the playServicesLocationVersion in its build.gradle.
  • The default value (if you don't explicitly control this variable in your ext vars in your app's own build.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 avatar Oct 27 '23 12:10 christocracy

@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.

superyarik avatar Oct 27 '23 13:10 superyarik

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?

christocracy avatar Oct 27 '23 14:10 christocracy

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

github-actions[bot] avatar Jun 03 '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 Jun 17 '24 01:06 github-actions[bot]