flutter_background_geolocation icon indicating copy to clipboard operation
flutter_background_geolocation copied to clipboard

[Bug]: bg.BackgroundGeolocation.onMotionChange() fires and switching states between Stationary and Moving randomly even if not moving

Open justChris opened this issue 7 months ago • 1 comments

Required Reading

  • [x] Confirmed

Plugin Version

4.12.0

Flutter Doctor

[✓] Flutter (Channel stable, 3.32.4, on macOS 15.5 24F74 darwin-arm64, locale en-US) [728ms]
    • Flutter version 3.32.4 on channel stable at /Users/chris/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6fba2447e9 (3 days ago), 2025-06-12 19:03:56 -0700
    • Engine revision 8cd19e509d
    • Dart version 3.8.1
    • DevTools version 2.45.1

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [1,974ms]
    • Android SDK at /Users/chris/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.4) [1,925ms]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16F6
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web [9ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.3) [9ms]
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)

[✓] VS Code (version 1.101.0) [7ms]
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.112.0

[✓] Connected device (3 available) [6.2s]
    • Chris’s iPhone (wireless) (mobile) • 00008130-000970383EE0001C • ios            • iOS 18.5 22F76
    • macOS (desktop)                    • macos                     • darwin-arm64   • macOS 15.5 24F74 darwin-arm64
    • Chrome (web)                       • chrome                    • web-javascript • Google Chrome 137.0.7151.104

[✓] Network resources [990ms]
    • All expected network resources are available.

• No issues found!

Mobile operating-system(s)

  • [x] iOS
  • [ ] Android

Device Manufacturer(s) and Model(s)

iPhone 15 pro

Device operating-systems(s)

18.5

What happened?

bg.BackgroundGeolocation.onMotionChange() fires and switching states between Stationary and Moving randomly even if not moving at all. Phone is placed on the table.

Plugin Code and/or Config

await bg.BackgroundGeolocation.ready(
      bg.Config(
        reset: true,
        locationAuthorizationRequest: 'Always',
        backgroundPermissionRationale: bg.PermissionRationale(
          title: 'Allow App to access your location in the background?',
          message: 'Your background location data will never leave your device!',
          positiveAction: 'Allow',
          negativeAction: 'Cancel',
        ),
        desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
        distanceFilter: 50.0,
        stopOnTerminate: false,
        startOnBoot: true,
        enableHeadless: true,
        disableLocationAuthorizationAlert: true,
        disableMotionActivityUpdates: false,
      ),
    );

Relevant log output

No logs available

justChris avatar Jun 15 '25 20:06 justChris

The iOS plugin determines that a device is moving by creating a 200 meter geofence around the last known position. The OS is completely responsible for monitoring this "stationary geofence" around the device, regardless if the app terminated or device rebooted.

When the OS determines the iOS device has exited this geofence, it notifies this plugin, which takes action to transition to the moving state, turning ON the location API and firing the .onMotionChange event.

However, the OS can be susceptible to poor-accuracy locations causing it to think the device has exited the "stationary geofence", causing a false-positive .onMotionChange event. No, there's nothing the plugin can do to prevent this rare case. The plugin will quickly determine the device is not in-fact moving, initiate the stopTimeout timer and transition back to the stationary state.

christocracy avatar Jun 16 '25 15:06 christocracy

Thanks

justChris avatar Jul 02 '25 16:07 justChris