transistorsoft / cordova-background-geolocation-lt

The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.
http://www.transistorsoft.com/shop/products/cordova-background-geolocation
Other
655 stars 277 forks source link

Enabling background location #104

Closed paultanner closed 8 years ago

paultanner commented 8 years ago

I just loaded up my previously-tested cordova app into a new iPhone and got no background responses. I discovered a setting that had to be set as "always" for the plugin to do its work. I am wondering if there is a way to determine this setting before trying to use it, better still a way to force it to "always" (or "never"). Thx.

christocracy commented 8 years ago

After first deleting the app from my Phone, I just did a fresh install of Sample App

2016-04-20 09:25:23.938 BG Geo[6562:2824338] ╔═════════════════════════════════════════════════
2016-04-20 09:25:23.938 BG Geo[6562:2824338] ║TSLocationManager ► Configure
2016-04-20 09:25:23.938 BG Geo[6562:2824338] ╠═════════════════════════════════════════════════
2016-04-20 09:25:23.939 BG Geo[6562:2824338] ╟─ {
    activityRecognitionInterval = 10000;
    activityType = OtherNavigation;
    autoSync = true;
    batchSync = false;
    debug = true;
    deferTime = 0;
    desiredAccuracy = 0;
    disableElasticity = false;
    distanceFilter = 20;
    fastestLocationUpdateInterval = 1000;
    forceReloadOnBoot = false;
    forceReloadOnGeofence = false;
    forceReloadOnHeartbeat = false;
    forceReloadOnLocationChange = false;
    forceReloadOnMotionChange = false;
    foregroundService = true;
    heartbeatInterval = 60;
    locationUpdateInterval = 5000;
    method = POST;
    params =     {
        device =         {
            available = 1;
            cordova = "4.1.1";
            isVirtual = 0;
            manufacturer = Apple;
            model = "iPhone8,1";
            platform = iOS;
            serial = unknown;
            uuid = "07F0A017-8269-4816-855F-2A99F1064700";
            version = "9.3.1";
        };
    };
    preventSuspend = false;
    startOnBoot = false;
    stationaryRadius = 20;
    stopDetectionDelay = 0;
    stopOnTerminate = true;
    stopTimeout = 1;
    triggerActivities = "in_vehicle, on_bicycle, running, walking, on_foot";
    url = "http://posttestserver.com/post.php?dir=ionic-cordova-background-geolocation";
    useSignificantChangesOnly = false;
}
2016-04-20 09:25:23.939 BG Geo[6562:2824338] ✓ Request location authorization: requestAlwaysAuthorization

I see no issue here.

christocracy commented 8 years ago

The default for locationAuthorizationRequest is Always. It always has been. If you want Always authorization, you shouldn't even worry about setting it.

The only reason this is even configurable today is because someone actually requested the ability (issue #70) to be able to request WhenInUse authorization so he could display the "Blue Bar".

You do not have to specify this. The plugin strongly desires Always authorization.

christocracy commented 8 years ago

If I were you, I would analyze my other 3rd party plugins and ask "Might any of these other plugins be requesting WhenInUse authorization?"

paultanner commented 8 years ago

Sorry, I'm not sure if I framed my question right. The new iPhone had the setting WhenInUse. I did not get the popup that you show above. Changed it manually via settings then it worked. My question was about how to detect that (whatever the cause) and inform the user that locations will not be recorded.

christocracy commented 8 years ago

Show me the plugins in your app:

$ cordova plugins
paultanner commented 8 years ago

com.transistorsoft.cordova.background-geolocation 1.4.1 "BackgroundGeolocation" cordova-plugin-bluetoothle 3.0.1 "Bluetooth LE" cordova-plugin-device 1.1.0 "Device" cordova-plugin-dialogs 1.2.0 "Notification" cordova-plugin-geolocation 2.1.0 "Geolocation" cordova-plugin-network-information 1.1.0 "Network Information" cordova-plugin-whitelist 1.2.1 "Whitelist" org.apache.cordova.console 0.2.3 "Console"

Thx.

christocracy commented 8 years ago

As I suspected, you're using cordova-plugin-geolocation. This plugin is requesting WhileInUse authorization before my plugin gets a chance to request Always. Once one plugin has requested location permission, no other plugin gets to change it or request a different permission.

I suggest you get rid of cordova-plugin-geolocation and just use mine.

paultanner commented 8 years ago

Thx for suggestion. I was using it to find out whether location was enabled before the app got properly started. It's a dependency. I guess I need to find a different way to do that.

christocracy commented 8 years ago

My plugin has a #getCurrentPosition method

paultanner commented 7 years ago

Thx for that and sorry for delay in replying. Had to fix iPhone. Anyway, I removed the other plugin then tried waiting for the callback from configure() and then calling getCurrentPosition(). For some reason it is not doing the success callback from configure(). the log says:

╔═════════════════════════════════════════════════
2016-05-31 12:09:42.388 AirSome![551:179149] ║TSLocationManager ► Configure
2016-05-31 12:09:42.389 AirSome![551:179149] ╠═════════════════════════════════════════════════
2016-05-31 12:09:42.391 AirSome![551:179149] ╟─ {
    activityRecognitionInterval = 5000;
    activityType = Fitness;
    debug = 1;
    desiredAccuracy = 10;
    disableElasticity = 0;
    distanceFilter = 5;
    minimumActivityRecognitionConfidence = 80;
    startOnBoot = 0;
    stationaryRadius = 5;
    stopDetectionDelay = 2;
    stopOnTerminate = 0;
    stopTimeout = 10;
    useSignificantChangesOnly = 0;
}
2016-05-31 12:09:42.394 AirSome![551:179149] ✓ Request location authorization: requestAlwaysAuthorization
2016-05-31 12:09:42.420 AirSome![551:179101] ► didChangeAuthorizationStatus 3
2016-05-31 12:09:42.430 AirSome![551:179101] ► LocationManager didChangeAuthorizationStatus: 3
2016-05-31 12:09:42.461 AirSome![551:179101] ► LocationManager didChangeAuthorizationStatus: 3

After that the plugin is silent. Did not get the authorisation popup as above. I tried this with different location settings - same result.

Update: I also tried calling configure() and not waiting for it. In this case I got callbacks from getCurrentPosition(). However, after calling start() I did not get any callbacks even when moving 1/2 mile, although I did see the odd message mentioning "stationary".

Any further suggestion pls? Thx.

christocracy commented 7 years ago
bgGeo.configure(config, function(state) {
  console.log('- current state: ', state.enabled);
  if (!state.enabled) {
    bgGeo.start();
  }
});
paultanner commented 7 years ago

Thx @christocracy

I had been calling it wrongly and am now using the above. At first it crashed horribly so after a while I realised this was a build issue and nuked everything to start again. Now seems OK, subject to testing on the move.

In this use case I do not want to start tracking immediately so after getting the location (to make sure user has enabled it) I call stop() and then start() again when ready to track.

Update: it is not recording location in the background. Here's my code:

    options = {
      desiredAccuracy: 10, // high power high accuracy
      distanceFilter: 5,
      stationaryRadius: 5,
      locationUpdateInterval: 1000,
      fastestLocationUpdateInterval: 5000,
      autoSync: false, // prevent automatic http requests
      activityType: 'Fitness', //'OtherNavigation', //CLActivityType        
      activityRecognitionInterval: 5000,
      stopTimeout: 5, // Wait x minutes to turn off location system after stop-detection
      stopDetectionDelay: 2, // Wait x minutes to engage stop-detection system
      debug: true, // hear sounds
      stopOnTerminate: false, // for headless operation
      startOnBoot: false // ditto 
    };
    // use the background locn plugin
    bgGeo = window.BackgroundGeolocation;
    bgGeo.on('location', onLocSuccess, onLocError);
    console.log('calling bgGeo.configure');
    bgGeo.configure(
      options,
      function(state) {
        console.log('success callback from bgGeo.configure', state.enabled);
        if (!state.enabled) {
          _bleLog('not calling bgGeo.start');
          //bgGeo.start();   // this done later in a callback from getState()
        }
        console.log('calling bgGeo.getCurrentPosition');
        bgGeo.getCurrentPosition(
          function(position, taskId) { // success
            console.log('callback from bgGeo.getCurrentPosition');
            onLocSuccess(position, taskId);   // log location (to be sent with sensor data)
            _bleLog('calling bgGeo.stop');
            bgGeo.stop();
            func();
          },
          onLocError,
          options
        );
      }
    );

You will see that I have switched off autoSync. My onLocSuccess() is supposed to be called by the 'location' event and that logs the location.

In the log I see it calling me back just once after I call start(). I wonder if it is correctly sensing movement?

Versions: cordova 6.2; iOS 9.3.2

Note: there may be a documentation issue on https://github.com/transistorsoft/cordova-background-geolocation-lt/wiki/Background-Tasks as this shows a different calling pattern for configure(). Nevertheless, I am following your example above.

christocracy commented 7 years ago

When you say "it's not recording location in background", are you moving? You're aware that when initially #started, the plugin enters "stationary mode" and your app goes to sleep until the device exits the stationary geofence? You need to move typically 3 blocks away from current position to exit the stationary geofence, when your app will awaken and plugin begin aggressively tracking.

paultanner commented 7 years ago

Yes. I was aware of that which is why I did moving tests. Since this is a walking app 3 blocks is a lot. I have added a call to bgGeo.changePace() which causes tracking. At the moment this is producing some readings, many of which look wildly out (by as much as 1/2 mile). As above, stationaryRadius is set to 5. I will do some more moving tests.

I did test the iPhone using endomondo. No wild readings in that case but the app may be handling them somehow.

christocracy commented 7 years ago

You're not going to get the most accurate results possible because you're using desiredAccuracy: 10, which using only Wifi & Cell triangulation (NO GPS).

GPS is used only with desiredAccuracy: 0. Endomondo is certainly using desiredAccuracy: 0.

stationaryRadius: 5 is pointless. The plugin enforces a minimum of 25 now. iOS doesn't respond for at least 200 meters anyway.

paultanner commented 7 years ago

Thx for that. The good news is that there was only 1 wild point per session. desiredAccuracy: 0 seems to fix it. If not (after more moving tests) I can just look for changes beyond walking speed and ignore them.

Microzone commented 7 years ago

hello christocracy , i have a problem with startOnBoot. I set it to true on my options but it does not trigger the location after restarting my device

christocracy commented 7 years ago

iOS requires a significant location change to re-awaken. After closing, it won't re-awaken until you move 100-200 meters, typically. This is an iOS constraint.

On Tuesday, July 19, 2016, microzonesolutions notifications@github.com wrote:

hello christocracy , i have a problem with startOnBoot. I set it to true on my options but it does not trigger the location after restarting my device

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/transistorsoft/cordova-background-geolocation-lt/issues/104#issuecomment-233573652, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA6l4Nc8cnZeKZCrLf1xY5qv1rCJLHDks5qXJO5gaJpZM4ILlOz .

Snet form Gmail Mobile

Microzone commented 7 years ago

Hi christocracy

Okay thanks, problem fixed but i have one more problem which i believe it will fix all my problems. How can i view my SQL lite output ? i believe the plugin has build-in SQL lite, so i do get my locations and all, but i want to know where can i view my SQL lite output because i want to retrieve all the data saved on SQL lite and post it to my server .

christocracy commented 7 years ago

Have you looked at the docs and glanced through every possible method available? Do that now.

The plugin has its own native http layer. You don't need to do http with Ajax. The plugin is far better at this task than you are with Ajax. Since it owns the database, it can automatically handle http errors and retry later.

On Wednesday, July 20, 2016, microzonesolutions notifications@github.com wrote:

Hi christocracy

Okay thanks, problem fixed but i have one more problem which i believe it will fix all my problems. How can i view my SQL lite output ? i believe the plugin has build-in SQL lite, so i do get my locations and all, but i want to know where can i view my SQL lite output because i want to retrieve all the data saved on SQL lite and post it to my server .

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/transistorsoft/cordova-background-geolocation-lt/issues/104#issuecomment-233919581, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA6l-x3qNf34I8ibkaoN-KcIMp4DiHWks5qXgFPgaJpZM4ILlOz .

Snet form Gmail Mobile

Microzone commented 7 years ago

image

That's how i am doing my post to my server, is there anything wrong ? i read your README and i followed your steps

christocracy commented 7 years ago

Are you a customer?

Microzone commented 7 years ago

Yes i am

christocracy commented 7 years ago

Why then are posting issues at the Free repo and not the private Premium, customers-only repo?

This link will be 404 to you, since I see you're not a member of the private repo.
https://github.com/transistorsoft/cordova-background-geolocation

If you're a customer, whoever ordered the product can provide you access by visiting the Customer Dashboard: http://www.transistorsoft.com/shop/customers