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

Could not get the location when first load/start the app

Open RuFang-21 opened this issue 2 years ago • 5 comments

Your Environment

  • Plugin version: ^4.13.3
  • Platform: Android
  • OS version: 12
  • Device manufacturer / model: realme 6
  • React Native version (react-native -v): 0.70.9
  • Plugin config
export const initBackgroundLocation = async () => {
  try {
    const res = await BackgroundGeolocation.ready({
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
      distanceFilter: 30, // Meters
      stopTimeout: 10, // For motion events
      debug: false,
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
      stopOnTerminate: true, // <-- Allow the background-service to continue tracking when user closes the app.
      startOnBoot: true, // <-- Auto start tracking when device is powered-up.
      autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives.
    });
    return Promise.resolve(res.enabled);
  } catch (e) {
    console.log(e);
    return Promise.reject(e);
  }
};

export const startBackgroundService = async () => {
  try {
    console.log('start background');
    await BackgroundGeolocation.start();
    locationSubscription = BackgroundGeolocation.onLocation(
      async location => {
        try {
          console.log('location', location);
          await apolloClient
            .mutate<Gql.UpdateRiderLocationMutation, Gql.UpdateRiderLocationMutationVariables>({
              mutation: gql(Gql.UpdateRiderLocationDocument),
              variables: {
                latitude: location.coords.latitude,
                longitude: location.coords.longitude,
                heading: location.coords.heading ?? 0,
              },
            })
            .finally(() => {
              console.log('saved');
            });
          store.dispatch(
            locationSlice.actions.setLocation({
              longitude: location.coords.longitude,
              latitude: location.coords.latitude,
            }),
          );
        } catch (e) {
          console.log(e);
        }
      },

      error => {
        parseErrorMessage(error);
      },
    );
  } catch (e) {
    return Promise.reject(e);
  }
};

Expected Behavior

Should captured the location after logged in to the app

Actual Behavior

Need to kill app and open the app just can get the location

Steps to Reproduce

Context

Trigger BackgroundGeolocation.onLocation after initiation authentication

Debug logs

Logs
09-07 10:04:08.082  4635  4740 I TSLocationManager: - Enable: false → true, trackingMode: 1
09-07 10:04:18.749  4635  4742 I TSLocationManager: - Enable: true → true, trackingMode: 1
09-07 10:04:28.304  4635  4742 I TSLocationManager: - Enable: false → true, trackingMode: 1
09-07 10:05:00.593  4635  4742 I TSLocationManager: - Enable: true → true, trackingMode: 1

RuFang-21 avatar Sep 07 '23 02:09 RuFang-21

There's got to be more logs than that.

christocracy avatar Sep 07 '23 03:09 christocracy

❯ adb logcat -s TSLocationManager
--------- beginning of system
--------- beginning of main
09-08 14:06:19.531 19363 19950 I TSLocationManager: - Enable: false → true, trackingMode: 1

above is the logs when i first load my app

export const initBackgroundLocation = async () => {
  try {
    const res = await BackgroundGeolocation.ready({
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
      distanceFilter: 30, // Meters
      stopTimeout: 10, // For motion events
      debug: false,
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
      stopOnTerminate: true, // <-- Allow the background-service to continue tracking when user closes the app.
      startOnBoot: true, // <-- Auto start tracking when device is powered-up.
      autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives.
    });
    startBackgroundService();
    return Promise.resolve(res.enabled);
  } catch (e) {
    console.log(e);
    return Promise.reject(e);
  }
};

export const startBackgroundService = async () => {
  try {
    console.log('start background');
    await BackgroundGeolocation.start();
    locationSubscription = BackgroundGeolocation.onLocation(
      async location => {
        try {
          console.log('location', location);
          await apolloClient
            .mutate<Gql.UpdateRiderLocationMutation, Gql.UpdateRiderLocationMutationVariables>({
              mutation: gql(Gql.UpdateRiderLocationDocument),
              variables: {
                latitude: location.coords.latitude,
                longitude: location.coords.longitude,
                heading: location.coords.heading ?? 0,
              },
            })
            .finally(() => {
              console.log('saved');
            });
          store.dispatch(
            locationSlice.actions.setLocation({
              longitude: location.coords.longitude,
              latitude: location.coords.latitude,
            }),
          );
        } catch (e) {
          console.log(e);
        }
      },

      error => {
        parseErrorMessage(error);
      },
    );
  } catch (e) {
    console.log(e);
    return Promise.reject(e);
  }
};

RuFang-21 avatar Sep 08 '23 06:09 RuFang-21

❯ adb logcat -s TSLocationManager
--------- beginning of system
--------- beginning of main
09-08 14:06:19.531 19363 19950 I TSLocationManager: - Enable: false → true, trackingMode: 1
09-08 14:09:15.164 20445 20445 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity a] locationsettings
09-08 14:09:15.166 20445 20445 D TSLocationManager: [c.t.l.adapter.TSConfig translateDesiredAccuracy] translateDesiredAccuracy (true): -1
09-08 14:09:15.248 20445 20445 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity c] eventCount: 0
09-08 14:09:15.339 20445 20445 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity onDestroy] 
09-08 14:09:15.484 20445 20539 D TSLocationManager: [c.t.l.u.LocationAuthorization withBackgroundPermission] 
09-08 14:09:15.484 20445 20539 D TSLocationManager:   ℹ️  LocationAuthorization: Permission granted
09-08 14:09:15.489 20445 20531 I TSLocationManager: - Enable: false → true, trackingMode: 1
09-08 14:09:15.491 20445 20531 I TSLocationManager: [c.t.l.g.TSGeofenceManager start] 
09-08 14:09:15.491 20445 20531 I TSLocationManager:   🎾  Start monitoring geofences
09-08 14:09:15.496 20445 20531 D TSLocationManager: [c.t.l.http.HttpService startMonitoringConnectivityChanges] 
09-08 14:09:15.496 20445 20531 D TSLocationManager:   🎾  Start monitoring connectivity changes
09-08 14:09:15.501 20445 20531 D TSLocationManager: [c.t.l.device.DeviceSettings startMonitoringPowerSaveChanges] 
09-08 14:09:15.501 20445 20531 D TSLocationManager:   🎾  Start monitoring powersave changes
09-08 14:09:15.503 20445 20531 I TSLocationManager: [c.t.l.s.ActivityRecognitionService start] 
09-08 14:09:15.503 20445 20531 I TSLocationManager:   🎾  Start motion-activity updates
09-08 14:09:15.507 20445 20792 D TSLocationManager: [c.t.l.http.HttpService a] 
09-08 14:09:15.507 20445 20792 D TSLocationManager: ╔═════════════════════════════════════════════
09-08 14:09:15.507 20445 20792 D TSLocationManager: ║ 📶  Connectivity change: connected? true
09-08 14:09:15.507 20445 20792 D TSLocationManager: ╠═════════════════════════════════════════════
09-08 14:09:15.513 20445 20531 I TSLocationManager: [c.t.l.service.HeartbeatService stop] 
09-08 14:09:15.513 20445 20531 I TSLocationManager:   🔴  Stop heartbeat
09-08 14:09:15.521 20445 20531 I TSLocationManager: [c.t.l.service.TrackingService changePace] 
09-08 14:09:15.521 20445 20531 I TSLocationManager:   🔵  setPace: false → false
09-08 14:09:15.536 20445 20445 I TSLocationManager: [c.t.l.l.TSLocationManager a] 
09-08 14:09:15.536 20445 20445 I TSLocationManager: ╔═════════════════════════════════════════════
09-08 14:09:15.536 20445 20445 I TSLocationManager: ║ motionchange LocationResult: 1 (170748ms old)
09-08 14:09:15.536 20445 20445 I TSLocationManager: ╠═════════════════════════════════════════════
09-08 14:09:15.536 20445 20445 I TSLocationManager: ╟─ 📍  Location[fused 3.10****,101.68**** hAcc=20 et=+1d1h55m11s801ms alt=28.299999237060547 vel=0.0 vAcc=1 sAcc=??? bAcc=??? {Bundle[{battery_level=0.9, is_charging=true, odometer=0.0}]}], time: 1694153184788
09-08 14:09:15.537 20445 20445 D TSLocationManager: [c.t.l.l.TSLocationManager a] Median accuracy: 20.0
09-08 14:09:15.553 20445 20445 D TSLocationManager: [c.t.l.u.LocationAuthorization withPermission] 
09-08 14:09:15.553 20445 20445 D TSLocationManager:   ℹ️  LocationAuthorization: Permission granted
09-08 14:09:15.600 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService a] 
09-08 14:09:15.600 20445 20445 D TSLocationManager:   🎾  START [LocationRequestService  startId: 1, eventCount: 1]
09-08 14:09:15.606 20445 20531 I TSLocationManager: [c.t.l.l.SingleLocationRequest startUpdatingLocation] 
09-08 14:09:15.606 20445 20531 I TSLocationManager:   🔵  [SingleLocationRequest start, action: 1, requestId: 1]
09-08 14:09:15.616 20445 20531 D TSLocationManager: [c.t.l.service.AbstractService a] 
09-08 14:09:15.616 20445 20531 D TSLocationManager:   ⚙️︎   FINISH [LocationRequestService startId: 1, eventCount: 0, sticky: true]
09-08 14:09:15.873 20445 20445 I TSLocationManager: [c.t.l.s.TSScheduleManager oneShot] 
09-08 14:09:15.873 20445 20445 I TSLocationManager:   ⏰ Scheduled OneShot: TERMINATE_EVENT in 10000ms (jobID: -1708771588)
09-08 14:09:15.914 20445 20445 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity a] locationsettings
09-08 14:09:15.915 20445 20445 D TSLocationManager: [c.t.l.adapter.TSConfig translateDesiredAccuracy] translateDesiredAccuracy (true): -1
09-08 14:09:15.962 20445 20445 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity c] eventCount: 0
09-08 14:09:16.209 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService a] 
09-08 14:09:16.209 20445 20445 D TSLocationManager:   🎾  START [LocationRequestService  startId: 2, eventCount: 1]
09-08 14:09:16.213 20445 20531 I TSLocationManager: [c.t.l.l.TSLocationManager a] 
09-08 14:09:16.213 20445 20531 I TSLocationManager: ╔═════════════════════════════════════════════
09-08 14:09:16.213 20445 20531 I TSLocationManager: ║ motionchange LocationResult: 1 (142ms old)
09-08 14:09:16.213 20445 20531 I TSLocationManager: ╠═════════════════════════════════════════════
09-08 14:09:16.213 20445 20531 I TSLocationManager: ╟─ 📍  Location[fused 3.10****,101.68**** hAcc=20 et=+1d1h58m3s83ms alt=28.299999237060547 vAcc=2 sAcc=??? bAcc=??? {Bundle[{}]}], time: 1694153356069
09-08 14:09:16.218 20445 20531 I TSLocationManager: [c.t.l.l.TSLocationManager onSingleLocationResult] 
09-08 14:09:16.218 20445 20531 I TSLocationManager:   🔵  Acquired motionchange position, isMoving: false
09-08 14:09:16.218 20445 20531 D TSLocationManager: [c.t.l.l.TSLocationManager a] Median accuracy: 20.05
09-08 14:09:16.229 20445 20445 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity onDestroy] 
09-08 14:09:16.232 20445 20531 D TSLocationManager: [c.t.l.service.AbstractService a] 
09-08 14:09:16.232 20445 20531 D TSLocationManager:   ⚙️︎   FINISH [LocationRequestService startId: 2, eventCount: 0, sticky: false]
09-08 14:09:16.237 20445 20528 I TSLocationManager: [c.t.l.d.s.SQLiteLocationDAO persist] 
09-08 14:09:16.237 20445 20528 I TSLocationManager:   ✅  INSERT: 14ab27c7-87e6-440a-933b-c96634ad1bc2
09-08 14:09:16.265 20445 20445 D TSLocationManager: [c.t.l.g.TSGeofenceManager startMonitoringStationaryRegion] 
09-08 14:09:16.265 20445 20445 D TSLocationManager:   🎾  Start monitoring stationary region (radius: 150.0m 3.1013087,101.6773402 hAcc=20.1)
09-08 14:09:16.290 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService b] 
09-08 14:09:16.290 20445 20445 D TSLocationManager:   🎾  STOP [LocationRequestService startId: 3, eventCount: 1]
09-08 14:09:16.291 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService a] 
09-08 14:09:16.291 20445 20445 D TSLocationManager:   ⚙️︎   FINISH [LocationRequestService startId: 3, eventCount: 0, sticky: false]
09-08 14:09:16.316 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService a] 
09-08 14:09:16.316 20445 20445 D TSLocationManager:   🎾  START [TrackingService  startId: 1, eventCount: 1]
09-08 14:09:16.319 20445 20445 I TSLocationManager: [c.t.l.service.TrackingService k] 
09-08 14:09:16.319 20445 20445 I TSLocationManager: ╔═════════════════════════════════════════════
09-08 14:09:16.319 20445 20445 I TSLocationManager: ║ TrackingService motionchange: false
09-08 14:09:16.319 20445 20445 I TSLocationManager: ╠═════════════════════════════════════════════
09-08 14:09:16.324 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService a] 
09-08 14:09:16.324 20445 20445 D TSLocationManager:   ⚙️︎   FINISH [TrackingService startId: 1, eventCount: 0, sticky: false]
09-08 14:09:16.568 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService f] 
09-08 14:09:16.568 20445 20445 D TSLocationManager:   ⚙️︎  LocationRequestService.stopSelfResult(3): true
09-08 14:09:16.569 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService f] 
09-08 14:09:16.569 20445 20445 D TSLocationManager:   ⚙️︎  TrackingService.stopSelfResult(1): true
09-08 14:09:16.574 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService onDestroy] 
09-08 14:09:16.574 20445 20445 D TSLocationManager:   🔴  LocationRequestService stopped
09-08 14:09:16.574 20445 20445 D TSLocationManager: [c.t.l.service.AbstractService onDestroy] 
09-08 14:09:16.574 20445 20445 D TSLocationManager:   🔴  TrackingService stopped

and here are the logs when i kill app and open the again. My issue is when i first load and open the app, can't capture the location until i kill app.

RuFang-21 avatar Sep 08 '23 06:09 RuFang-21

I suggest you observe $ adb logcat without the filter

christocracy avatar Sep 08 '23 09:09 christocracy

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

github-actions[bot] avatar May 04 '24 01:05 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar May 18 '24 01:05 github-actions[bot]