Call cordova methods outside of startup

I have the following piece of code in my meteor app

if (Meteor.isClient) {
  Meteor.startup(function(){

    var onSuccess = function(acceleration){
      alert( acceleration);
    };

    var onError = function(acceleration){
      return "error";
    }

    var options = { frequency: 3000 };  // Update every 3 seconds

    var getAcc = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

  });
}

What that does is retrieve the android phones accelerometer data every 3 seconds, and on a successful poll it will show the object in an alert. This works fine.

However, I don’t want this polling code to be in the startup function. I want to have more control over when this is executed

I have a template where I want to display the accelerometer values. I changed the onSuccess method in the code above to return the object instead of alerting (the rest of the startup code is the same):

var onSuccess = function(acceleration){
      return acceleration;
    };

My template looks like this:

Template.rawData.helpers({
    acc: function(){
      alert(getAcc);
      return getAcc;
    }
  });

What I’m expecting to happen is for the accelerometer data to be stored in getAcc in the startup function, but then to return it through acc to my webpage. This does not seem to happen. The alert in the template doesn’t occur either

Is there a way to access these cordova plugins from outside of the startup function? Am I just incorrectly returning the objects between the startup and template sections?

I guess my other overarching question is this: I’m not sure how to display those accelerometer values through a template if theyre gathered in the startup function, and not from a template helper

This is not really an issue specific to Cordova plugins, but more about JavaScript semantics. Looking at the code, I can guess what you’re trying to accomplish, but this is not how things work.

As you can see from the documentation for navigator.accelerometer.watchAcceleration, the result value is an ID that can be used to stop watching. It doesn’t return the current value, that is what the callbacks are for.

Functions like navigator.accelerometer.watchAcceleration are asynchronous and take callback arguments. If you want to store the provided value, you’ll have to do this within the onSuccess handler (making sure the variable is in a shared scope if you want to retrieve it from a helper). Returning the value from the callback doesn’t do anything (the return value is simply ignored).

So in onSuccess I can do something like Session.set("accData", acceleration), and I think this session variable will then be useable in a template helper?

But is there a way to do that without the use of session variables?

Yes, that should work. If you want the value to reactively update (rerendering the template), using a regular variable won’t be enough. An alternative to session variables would be to use a ReactiveVar. See this article or this one for more details.