Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It works only at the first time. #749

Closed
pyeonjaesik opened this issue Aug 15, 2018 · 25 comments
Closed

It works only at the first time. #749

pyeonjaesik opened this issue Aug 15, 2018 · 25 comments
Labels

Comments

@pyeonjaesik
Copy link

pyeonjaesik commented Aug 15, 2018

Your Environment

  • Plugin version:
    "cordova-android": "^7.0.0",
    "cordova-background-geolocation-lt": "^2.12.2",
    "cordova-plugin-background-fetch": "^5.4.1",
    "cordova-plugin-camera": "^4.0.3",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-file": "^6.0.1",
    "cordova-plugin-file-transfer": "^1.7.1",
    "cordova-plugin-firebase": "^1.0.5",
    "cordova-plugin-geolocation": "^4.0.1",
    "cordova-plugin-ionic-keyboard": "^2.1.2",
    "cordova-plugin-request-location-accuracy": "^2.2.3",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "cordova.plugins.diagnostic": "^4.0.8"

  • Platform: iOS or Android: Android

  • OS version: Mac

  • Device manufacturer / model: galaxy A5

  • Cordova version (cordova -v): 8.0.0

  • Cordova platform version (cordova platform ls):
    android 7.0.0
    Available platforms:
    browser ~5.0.1
    ios ~4.5.4
    osx ~4.0.1
    windows ~5.0.0
    www ^3.12.0

  • Plugin config

Context

It works only at the first time. ( bgeo.strart();)
following is my code

  $('#bgeo_start_btn').click(function(){
  document.addEventListener("deviceready", onDeviceReady_lbb);
function onDeviceReady_lbb() {
    // Get a reference to the plugin.
    var bgGeo = window.BackgroundGeolocation;

    //This callback will be executed every time a geolocation is recorded in the background.
    var callbackFn = function(location) {
        var coords = location.coords;
        var lat    = coords.latitude;
        var lng    = coords.longitude;
        console.log('- Location: ', JSON.stringify(location));        
    };

    // This callback will be executed if a location-error occurs.  Eg: this will be called if user disables location-services.
    var failureFn = function(errorCode) {
        console.warn('- BackgroundGeoLocation error: ', errorCode);
    }

    // Listen to location events & errors.
    bgGeo.on('location', callbackFn, failureFn);
    // Fired whenever state changes from moving->stationary or vice-versa.
    // Fired whenever an HTTP response is received from your server.
    bgGeo.on('motionchange', function(isMoving) {
      console.log('- onMotionChange: ', isMoving);
    });
    // Fired whenever a geofence transition occurs.
    bgGeo.on('geofence', function(geofence) {
      console.log('- onGeofence: ', geofence.identifier, geofence.location);
    });    
    bgGeo.on('http', function(response) {
      console.log('http success: ', response.responseText);
    }, function(response) {
      console.log('http failure: ', response.status);
    });

    // BackgroundGeoLocation is highly configurable.
    bgGeo.ready({
        // Geolocation config
        desiredAccuracy: 0,
        distanceFilter: 10,
        stationaryRadius: 25,
        // Activity Recognition config
        activityRecognitionInterval: 5000,
        stopTimeout: 5,
        // Application config
        debug: true,  // <-- Debug sounds & notifications.
        stopOnTerminate: false,
        startOnBoot: true,
        // HTTP / SQLite config
        url: url+'/sendlocal',
        method: "POST",
        autoSync: true,
        maxDaysToPersist: 3,
        headers: {  // <-- Optional HTTP headers
            "X-FOO": "bar"
        },
        params: {   // <-- Optional HTTP params
            _id:window.localStorage['_id'],
            li:window.localStorage['li']
        }
    }, function(state) {
        // This callback is executed when the plugin is ready to use.
        console.log("BackgroundGeolocation ready: ", state);
    });
    bgGeo.start();
}            
        });    
});    

Debug logs

@christocracy
Copy link
Member

christocracy commented Aug 15, 2018

You must not execute any plugin method which requests the location until the callback to #ready fires. That's why the method is called "ready".

bgGeo.ready(config, function(state) {
  if (!state.enabled) {
    bgGeo.start();  // <-- YES
  }
});
bgGeo.start();  // <-- NO!  You must wait until #ready callback.

You were warned about this in the README.

@pyeonjaesik
Copy link
Author

I changed my code,but It is not still working.
following is my code
<script>
$(function(){
$('#pwc_s1').click(function(){
document.addEventListener("deviceready", onDeviceReady_lbb);
function onDeviceReady_lbb() {
// Get a reference to the plugin.
var bgGeo = window.BackgroundGeolocation;

//This callback will be executed every time a geolocation is recorded in the background.
var callbackFn = function(location) {
    var coords = location.coords;
    var lat    = coords.latitude;
    var lng    = coords.longitude;
    console.log('- Location: ', JSON.stringify(location));        
};

// This callback will be executed if a location-error occurs.  Eg: this will be called if user disables location-services.
var failureFn = function(errorCode) {
    alert(JSON.stringify(errorCode));
    console.warn('- BackgroundGeoLocation error: ', errorCode);
}

// Listen to location events & errors.
bgGeo.on('location', callbackFn, failureFn);
// Fired whenever state changes from moving->stationary or vice-versa.
// Fired whenever an HTTP response is received from your server.
bgGeo.on('motionchange', function(isMoving) {
  console.log('- onMotionChange: ', isMoving);
});
// Fired whenever a geofence transition occurs.
bgGeo.on('geofence', function(geofence) {
  console.log('- onGeofence: ', geofence.identifier, geofence.location);
});    
bgGeo.on('http', function(response) {
  console.log('http success: ', response.responseText);
}, function(response) {
  console.log('http failure: ', response.status);
});

// BackgroundGeoLocation is highly configurable.
bgGeo.ready({
    // Geolocation config
    desiredAccuracy: 0,
    distanceFilter: 10,
    stationaryRadius: 25,
    // Activity Recognition config
    activityRecognitionInterval: 5000,
    stopTimeout: 5,
    // Application config
    debug: true,  // <-- Debug sounds & notifications.
    stopOnTerminate: false,
    startOnBoot: true,
    // HTTP / SQLite config
    url: url+'/sendlocal',
    method: "POST",
    autoSync: true,
    maxDaysToPersist: 3,
    headers: {  // <-- Optional HTTP headers
        "X-FOO": "bar"
    },
    params: {   // <-- Optional HTTP params
        _id:window.localStorage['_id'],
        li:window.localStorage['li']
    }
}, function(state) {
    // This callback is executed when the plugin is ready to use.
    console.log("BackgroundGeolocation ready: ", state);
    if (!state.enabled) {
        bgGeo.start();
    }        
});

}
});
});
</script>

@pyeonjaesik
Copy link
Author

still working only at the first time.

@christocracy
Copy link
Member

still working only at the first time.

Define "working".

See Debugging wiki.

@pyeonjaesik
Copy link
Author

When my device call the function -bgeo.start(), my device detects location and send the data to my server.
but, after that my device can not send location to my server any more.

@christocracy
Copy link
Member

Are you watching the logs? The plugin is telling you everything it's doing.

bgGeo.ready({
  reset: true,
  logLevel: bgGeo.LOG_LEVEL_VERBOSE,
  .
  .  your existing config
  .
});
$ adb logcat -s TSLocationManager

Read the Debugging wiki.

@pyeonjaesik
Copy link
Author

After I call the function bgeo.start(); and shaking for a while at foreground and teriminate my app, my device can detect location and send the data to my server.
BUT After I call bgeo.start() and terminate my app right away, my device can not detect location.
follwing is my debug
08-16 00:36:03.952 7132 7132 D TSLocationManager: [c.t.l.a.BackgroundGeolocation c]
08-16 00:36:03.952 7132 7132 D TSLocationManager: 🔴 Cleared callbacks
08-16 00:36:03.971 7132 7132 D TSLocationManager: [c.t.l.a.BackgroundGeolocation isMainActivityActive] NO

No log is written any more...

@christocracy
Copy link
Member

This is correct. See docs for stopOnTerminate

Android
Unlike iOS, the Android plugin's tracking will not pause at all when user terminates the app. However, only the plugin's native background service continues to operate, "headless" (in this case, you should configure an #url in order for the background-service to continue uploading locations to your server).

When you terminate an Android app, your ENTIRE Javascript is terminated. Obviously if you're using Javascript event-listeners upon the plugin, those will no longer fire.

This is why the plugin has its own persistence and HTTP services -- so it can continue to post locations to your server after the Android app has been terminated.

@pyeonjaesik
Copy link
Author

Sorry, I can't understand why you make me see the docs which is about stopOnTerminate.
The thing I want to know is "why my device CAN NOT detect location after terminating app without shaking my device " AND "why my device CAN detect location after shaking for a while and termination"

@christocracy
Copy link
Member

why you make me see the docs which is about stopOnTerminate.

Because you terminated your app.

BUT After I call bgeo.start() and terminate my app right away

why my device CAN NOT detect location after terminating app without shaking my device

Go for a walk

@pyeonjaesik
Copy link
Author

It seems like my device can not detect location without "EnableHeadlessMode :true". But After shaking for a while at the first time, without "EnableHeadlessMode:true" my device can detect loction even in the background

@christocracy
Copy link
Member

Forget about shaking and forget about enableHeadless. Go for a walk. Listen to the plugin beep.

@pyeonjaesik
Copy link
Author

What do you mean? -- "Listen to the plugin beep"

@christocracy
Copy link
Member

debug: true. The plugin will beep.

@christocracy
Copy link
Member

Because the “beep” means a location was recorded.

If a location was recorded, a location was posted to your url.

If a location is posted to your url, you have no problem.

@pyeonjaesik
Copy link
Author

Sorry, It was 3 AM in my country, I felt sleepy and did fall a sleep.
I already set debug true and checkecd whether my device can detect location and send the data to server or not.
Sometimes (after
1.Removing my app
2.cordova platform rm android
3.Cordova platform add android
4.Cordova run android ->launch)
my device can detect location but sometimes
(after
1.Removing my app
2.cordova platform rm android
3.Cordova platform add android
4.Cordova run android ->launch)
My device can not detect location.
When my device can not detect location, I can get the following debug
TSLocationManager: [c.t.l.a.BackgroundGeolocation c]
08-16 00:36:03.952 7132 7132 D TSLocationManager: 🔴 Cleared callbacks
08-16 00:36:03.971 7132 7132 D TSLocationManager: [c.t.l.a.BackgroundGeolocation isMainActivityActive] NO

@christocracy
Copy link
Member

There is nothing unusual about those log messages.

Removing / re-adding platform has no effect on the behaviour of the plugin.

I doubt you have any issue at all until you show me a log file after traveling 1km.

@pyeonjaesik
Copy link
Author

pyeonjaesik commented Aug 15, 2018

I did that ( removing and add platform) to change my configure such as changing activityRecognitionInterval from10000 to 5000.
It is strange that when rauching the app and shaking for a while and terminating, my device can detect location.
But without that behavior my device detect location only one time.

--- I doubt you have any issue at all until you show me a log file after traveling 1km.
I set the distancefilter 10. Then It should detect every 10m not 1km,right?

@christocracy
Copy link
Member

Stop asking questions and get out of the office. Go for a long fieldtest.

Fetch your logfile after.

@christocracy
Copy link
Member

This is my SampleApp today after terminating the app.

@pyeonjaesik
Copy link
Author

pyeonjaesik commented Aug 16, 2018

Sorry for asking too much questions. You are my only the one teacher. I tried several times walking around my house but I will try more times. After that I will send the log file to you.

@christocracy
Copy link
Member

You don’t test by walking around the office. You test by walking hundreds of meters.

@pyeonjaesik
Copy link
Author

pyeonjaesik commented Aug 16, 2018 via email

@pyeonjaesik
Copy link
Author

pyeonjaesik commented Aug 16, 2018 via email

@pyeonjaesik
Copy link
Author

pyeonjaesik commented Aug 17, 2018

I walked around hundreds of meter.
When I set config like this
{distancefilter : 10 activityRecognitionInterval :5000}
It works well.
I have some question. Would you give me advice?

  1. My device detects location and send the data to server right after I call the bgeo.start(). But it can not detect location any more for a while. After I worked hundreds of meter and turn on and off my app repeatedly, my app can detect location every 10m. Why this problem occur?

  2. When my device battery is lower than 15%, background location detecting does not work at all. How to enable my device to detect location even though lower than 15% battery?

  3. In Andorid, when I turn off the location service(gps) my device can not detect location. I want to notice user to turn on the location with kind message when user turn off the location service. How to alarm users when they turn off location service?

  4. When I set the config like this
    {distancefilter:100
    stationaryRadius:50
    activityRecognitionInterval:3000}
    My device should detect location every 100m, right?
    I walked hundreds of meters But My device can not detect location at all except first time when I call the bgeo.start().
    ( my device does not ring at all --beep)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants