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

Having issues starting location tracking using WhileInUse permission

Open felipeperdomo2 opened this issue 1 year ago β€’ 13 comments

Your Environment

  • Plugin version: 4.14.6
  • Platform: Android
  • OS version: 11
  • Device manufacturer / model: Google Pixel 2XL
  • React Native version (react-native -v): 0.72.10
  • Plugin config
BackgroundGeolocation.ready({
    locationAuthorizationRequest: 'WhenInUse',
    desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
    distanceFilter: 10,
    stopTimeout: 5,
    debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
    logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
    stopOnTerminate: true, // <-- Allow the background-service to continue tracking when user closes the app.
    startOnBoot: false, // <-- Auto start tracking when device is powered-up.
    autoSync: false, // <-- [Default: true] Set true to sync each location to server as it arrives.
    extras: {}, // <-- Optional meta-data
  })

Expected Behavior

Module should start tracking location and app should be able to be backgrounded and still get location tracking with WhileInUse permission

Actual Behavior

The module used to work correctly using the Always permission but users were ranting about it. So I started looking into using WhileInUse but whenever I try to start tracking the location, using the jogging app recommendation as an example where changePace(true) is called to create a tracking task that can be backgrounded, I keep getting Cannot changePace(true) while in the background with WhenInUse authorization error even though the app is in foreground.

Steps to Reproduce

Context

Start walk tracking method

const startWalkTracking = async (pets: string[]): Promise<void> => {
    try {
      const state = await BackgroundGeolocation.getState();
      const { enabled, odometer: initialOdometer } = state;

      const storage = await getDataFromDeviceStorage(extrasStorageItem);
      const locationExtras = JSON.parse(storage ?? '{}');

      if (locationExtras && !(opk in locationExtras)) {
        const walk = {
          start: moment().toISOString(),
          initialOdometer,
          pets,
          clientId: uuid(),
        };

        await saveDataOnDeviceStorage(extrasStorageItem, { ...locationExtras, [opk]: walk });

        if (!enabled) {
          console.log('stateeeeee', AppState.currentState);
          await BackgroundGeolocation.start();
          await BackgroundGeolocation.changePace(true);
        }

        setActiveWalk(walk);
        setStartWalkTrackingError(false);
      }
    } catch (err) {
      setStartWalkTrackingError(true);
    }
  };

Debug logs

Logs
06-05 11:52:23.936 15515 15515 D TSLocationManager: [c.t.l.a.BackgroundGeolocation a]
06-05 11:52:23.936 15515 15515 D TSLocationManager:   πŸ”΄  Cleared callbacks
06-05 11:52:23.937 15515 15515 D TSLocationManager: [c.t.l.l.LifecycleManager setHeadless]
06-05 11:52:23.937 15515 15515 D TSLocationManager: ╔═════════════════════════════════════════════
06-05 11:52:23.937 15515 15515 D TSLocationManager: β•‘ ☯️  HeadlessMode? true
06-05 11:52:23.937 15515 15515 D TSLocationManager: ╠═════════════════════════════════════════════
06-05 11:52:23.938 15515 15515 D TSLocationManager: [c.t.l.a.BackgroundGeolocation a]
06-05 11:52:23.938 15515 15515 D TSLocationManager:   πŸ”΄  Cleared callbacks
06-05 11:52:23.942 15515 15801 I TSLocationManager: [c.t.l.p.TSProviderManager stopMonitoring]
06-05 11:52:23.942 15515 15801 I TSLocationManager:   πŸ”΄  Stop monitoring location-provider changes
06-05 11:52:23.943 15515 15801 I TSLocationManager: [c.t.l.s.TSScheduleManager stop]
06-05 11:52:23.943 15515 15801 I TSLocationManager:   πŸ”΄  Scheduler OFF
06-05 11:52:23.947 15515 15801 I TSLocationManager: [c.t.l.a.BackgroundGeolocation e]
06-05 11:52:23.947 15515 15801 I TSLocationManager: ╔═════════════════════════════════════════════
06-05 11:52:23.947 15515 15801 I TSLocationManager: β•‘ MainActivity was destroyed
06-05 11:52:23.947 15515 15801 I TSLocationManager: ╠═════════════════════════════════════════════
06-05 11:52:23.947 15515 15801 I TSLocationManager: β•Ÿβ”€ stopOnTerminate: true
06-05 11:52:23.947 15515 15801 I TSLocationManager: β•Ÿβ”€ enabled: false
06-05 11:52:39.037 15515 15804 D TSLocationManager: [c.t.l.u.LocationAuthorization withBackgroundPermission]
06-05 11:52:39.037 15515 15804 D TSLocationManager:   ℹ️  LocationAuthorization: Permission granted
06-05 11:52:39.038 15515 15801 I TSLocationManager: - Enable: false β†’ true, trackingMode: 1
06-05 11:52:39.040 15515 15801 I TSLocationManager: [c.t.l.g.TSGeofenceManager start]
06-05 11:52:39.040 15515 15801 I TSLocationManager:   🎾  Start monitoring geofences
06-05 11:52:39.044 15515 15801 D TSLocationManager: [c.t.l.http.HttpService startMonitoringConnectivityChanges]
06-05 11:52:39.044 15515 15801 D TSLocationManager:   🎾  Start monitoring connectivity changes
06-05 11:52:39.048 15515 15801 D TSLocationManager: [c.t.l.device.DeviceSettings startMonitoringPowerSaveChanges]
06-05 11:52:39.048 15515 15801 D TSLocationManager:   🎾  Start monitoring powersave changes
06-05 11:52:39.051 15515 15606 D TSLocationManager: [c.t.l.http.HttpService a]
06-05 11:52:39.051 15515 15606 D TSLocationManager: ╔═════════════════════════════════════════════
06-05 11:52:39.051 15515 15606 D TSLocationManager: β•‘ πŸ“Ά  Connectivity change: connected? true
06-05 11:52:39.051 15515 15606 D TSLocationManager: ╠═════════════════════════════════════════════
06-05 11:52:39.052 15515 15801 I TSLocationManager: [c.t.l.s.ActivityRecognitionService start]
06-05 11:52:39.052 15515 15801 I TSLocationManager:   🎾  Start motion-activity updates
06-05 11:52:39.058 15515 15801 I TSLocationManager: [c.t.l.service.HeartbeatService stop]
06-05 11:52:39.058 15515 15801 I TSLocationManager:   πŸ”΄  Stop heartbeat
06-05 11:52:39.062 15515 15801 I TSLocationManager: [c.t.l.service.TrackingService changePace]
06-05 11:52:39.062 15515 15801 I TSLocationManager:   πŸ”΅  setPace: false β†’ false
06-05 11:52:39.075 15515 15801 W TSLocationManager: [c.t.l.service.TrackingService changePace]
06-05 11:52:39.075 15515 15801 W TSLocationManager:   ⚠️  Cannot changePace(true) while in the background with WhenInUse authorization
06-05 11:52:39.077 15515 15515 I TSLocationManager: [c.t.l.l.TSLocationManager a]
06-05 11:52:39.077 15515 15515 I TSLocationManager: ╔═════════════════════════════════════════════
06-05 11:52:39.077 15515 15515 I TSLocationManager: β•‘ motionchange LocationResult: 1 (1665877ms old)
06-05 11:52:39.077 15515 15515 I TSLocationManager: ╠═════════════════════════════════════════════
06-05 11:52:39.077 15515 15515 I TSLocationManager: β•Ÿβ”€ πŸ“  Location[fused -34.893551,-56.104504 hAcc=17 et=+36s688ms alt=25.299999237060547 vAcc=1 sAcc=2 bAcc=45 {Bundle[{battery_level=0.63, is_charging=true, odometer=153.32591}]}], time: 1717597493199
06-05 11:52:39.294 15515 15515 D TSLocationManager: [c.t.l.l.TSLocationManager a] Median accuracy: 17.005
06-05 11:52:39.318 15515 15515 W TSLocationManager: [c.t.l.l.SingleLocationRequest g]
06-05 11:52:39.318 15515 15515 W TSLocationManager: ╔═════════════════════════════════════════════
06-05 11:52:39.318 15515 15515 W TSLocationManager: β•‘ ⚠️  Background location-updates forbidden (WhenInUse)
06-05 11:52:39.318 15515 15515 W TSLocationManager: ╠═════════════════════════════════════════════
06-05 11:52:39.318 15515 15515 W TSLocationManager: β•Ÿβ”€ Cannot initiate location requests in the background/headless with WhenInUse authorization.
06-05 11:52:39.318 15515 15515 W TSLocationManager: β•Ÿβ”€ Location updates must have previously been initiated with your app in the foreground.  Eg: .changePace(true).
06-05 11:52:39.318 15515 15515 W TSLocationManager: β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
06-05 11:52:39.321 15515 15515 W TSLocationManager: [c.t.l.l.SingleLocationRequest finish]
06-05 11:52:39.321 15515 15515 W TSLocationManager:   ⚠️  πŸ“  SingleLocaitonRequest ERROR: 3
06-05 11:52:39.322 15515 15515 I TSLocationManager: [c.t.l.l.SingleLocationRequest finish]
06-05 11:52:39.322 15515 15515 I TSLocationManager:   πŸ”΅  Found previous sample
06-05 11:52:39.324 15515 15801 I TSLocationManager: [c.t.l.l.TSLocationManager a]
06-05 11:52:39.324 15515 15801 I TSLocationManager: ╔═════════════════════════════════════════════
06-05 11:52:39.324 15515 15801 I TSLocationManager: β•‘ motionchange LocationResult: 1 (1666124ms old)
06-05 11:52:39.324 15515 15801 I TSLocationManager: ╠═════════════════════════════════════════════
06-05 11:52:39.324 15515 15801 I TSLocationManager: β•Ÿβ”€ πŸ“  Location[fused -34.893551,-56.104504 hAcc=17 et=+36s688ms alt=25.299999237060547 vAcc=1 sAcc=2 bAcc=45 {Bundle[{battery_level=0.63, persist=true, is_charging=true, odometer=153.32591}]}], time: 1717597493199
06-05 11:52:39.324 15515 15801 I TSLocationManager: [c.t.l.l.TSLocationManager onSingleLocationResult]
06-05 11:52:39.324 15515 15801 I TSLocationManager:   πŸ”΅  Acquired motionchange position, isMoving: false
06-05 11:52:39.325 15515 15801 D TSLocationManager: [c.t.l.l.TSLocationManager a] Median accuracy: 17.005
06-05 11:52:39.329 15515 15515 D TSLocationManager: [c.t.l.s.ForegroundNotification createNotificationChannel] NotificationChannel{mId='com.app.androidTSLocationManager', mName=TSLocationManager, mDescription=, mImportance=1, mBypassDnd=false, mLockscreenVisibility=-1, mSound=null, mLights=false, mLightColor=0, mVibration=null, mUserLockedFields=0, mFgServiceShown=false, mVibrationEnabled=false, mShowBadge=false, mDeleted=false, mGroup='null', mAudioAttributes=null, mBlockableSystem=false, mAllowBubbles=-1, mImportanceLockedByOEM=false, mImportanceLockedDefaultApp=false, mOriginalImp=-1000, mParent=null, mConversationId=null, mDemoted=false, mImportantConvo=false}
06-05 11:52:39.340 15515 15806 I TSLocationManager: [c.t.l.d.s.SQLiteLocationDAO persist]
06-05 11:52:39.340 15515 15806 I TSLocationManager:   βœ…  INSERT: 5e4185a7-40b8-4226-82ad-16a098307b86
06-05 11:52:39.352 15515 15515 D TSLocationManager: [c.t.l.service.AbstractService a]
06-05 11:52:39.352 15515 15515 D TSLocationManager:   🎾  start [ActivityRecognitionService  startId: 1, eventCount: 1]
06-05 11:52:39.364 15515 15806 D TSLocationManager: [c.t.l.s.ActivityRecognitionService a]
06-05 11:52:39.364 15515 15806 D TSLocationManager:   🚘 ️DetectedActivity [type=STILL, confidence=100]
06-05 11:52:39.367 15515 15515 D TSLocationManager: [c.t.l.g.TSGeofenceManager startMonitoringStationaryRegion]
06-05 11:52:39.367 15515 15515 D TSLocationManager:   ℹ️  Cannot monitor stationary-region with 'WhenInUse' authorization
06-05 11:52:39.374 15515 15806 D TSLocationManager: [c.t.l.service.AbstractService a]
06-05 11:52:39.374 15515 15806 D TSLocationManager:   βš™οΈοΈŽ   FINISH [ActivityRecognitionService startId: 1, eventCount: 0, sticky: false]
06-05 11:52:39.385 15515 15515 I TSLocationManager: [c.t.l.s.TSScheduleManager oneShot]
06-05 11:52:39.385 15515 15515 I TSLocationManager:   ⏰ Scheduled OneShot: TERMINATE_EVENT in 10000ms (jobID: -1708771588)
06-05 11:52:39.427 15515 15515 D TSLocationManager: [c.t.l.service.AbstractService a]
06-05 11:52:39.427 15515 15515 D TSLocationManager:   🎾  motionchange [TrackingService  startId: 1, eventCount: 1]
06-05 11:52:39.428 15515 15515 I TSLocationManager: [c.t.l.service.TrackingService k]
06-05 11:52:39.428 15515 15515 I TSLocationManager: ╔═════════════════════════════════════════════
06-05 11:52:39.428 15515 15515 I TSLocationManager: β•‘ TrackingService motionchange: false
06-05 11:52:39.428 15515 15515 I TSLocationManager: ╠═════════════════════════════════════════════
06-05 11:52:39.433 15515 15515 D TSLocationManager: [c.t.l.service.AbstractService a]
06-05 11:52:39.433 15515 15515 D TSLocationManager:   βš™οΈοΈŽ   FINISH [TrackingService startId: 1, eventCount: 0, sticky: false]
06-05 11:52:39.467 15515 15515 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity a] locationsettings
06-05 11:52:39.467 15515 15515 D TSLocationManager: [c.t.l.adapter.TSConfig translateDesiredAccuracy] translateDesiredAccuracy (true): -1
06-05 11:52:39.511 15515 15515 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity c] eventCount: 0
06-05 11:52:39.587 15515 15515 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity onDestroy]
06-05 11:52:39.603 15515 15515 D TSLocationManager: [c.t.l.service.AbstractService f]
06-05 11:52:39.603 15515 15515 D TSLocationManager:   βš™οΈοΈŽ  ActivityRecognitionService.stopSelfResult(1): true
06-05 11:52:39.605 15515 15515 D TSLocationManager: [c.t.l.service.AbstractService onDestroy]
06-05 11:52:39.605 15515 15515 D TSLocationManager:   πŸ”΄  ActivityRecognitionService stopped
06-05 11:52:39.636 15515 15515 D TSLocationManager: [c.t.l.service.AbstractService f]
06-05 11:52:39.636 15515 15515 D TSLocationManager:   βš™οΈοΈŽ  TrackingService.stopSelfResult(1): true
06-05 11:52:39.636 15515 15515 D TSLocationManager: [c.t.l.service.AbstractService onDestroy]
06-05 11:52:39.636 15515 15515 D TSLocationManager:   πŸ”΄  TrackingService stopped
06-05 11:52:49.475 15515 15806 I TSLocationManager: [c.t.l.scheduler.ScheduleEvent a]
06-05 11:52:49.475 15515 15806 I TSLocationManager: ╔═════════════════════════════════════════════
06-05 11:52:49.475 15515 15806 I TSLocationManager: β•‘ ⏰ OneShot event fired: TERMINATE_EVENT
06-05 11:52:49.475 15515 15806 I TSLocationManager: ╠═════════════════════════════════════════════

felipeperdomo2 avatar Jun 05 '24 15:06 felipeperdomo2