0

I am trying to make stepcounter app in javascript with cordova, but pedometer.startPedometerUpdates(successHandler, onError) always returns error handler (never successHandler)

I am using cordova-plugin-pedometer https://www.npmjs.com/package/cordova-plugin-pedometer There is written: The success handler is executed when data is available and is called repeatedly from a background thread as new data arrives.

but as I said my successHandler never executes, so in my opinion it is not called repeatedly or there are no data to work with, but I don't know how to solve it.

I know that there are 2 questions which are close to my question, but there are not helpful answers. Cordova Plugin Pedometers How to use the pedometer plugin?

my code:

    var app = {  
        // Application Constructor
        initialize: function() {
            document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
        },

        // deviceready Event Handler
        //
        // Bind any cordova events here. Common events are:
        // 'pause', 'resume', etc.
        onDeviceReady: function() { 

            //PEDOMETER
            var successHandler = function(data) {
                // pedometerData.startDate; -> ms since 1970
                // pedometerData.endDate; -> ms since 1970
                console.log('step');
                alert("step");
                alert(data.numberOfSteps + " " + data.distance);
                // pedometerData.distance;
                // pedometerData.floorsAscended;
                // pedometerData.floorsDescended;
            };
            var onError = function(){
                //alert("pedometer failure");
            };

            var successCallback = function (){
                console.log("success");
                alert("success");
            };
            var failureCallback = function (){
                console.log("failure");
                alert("failure");
            };
            pedometer.startPedometerUpdates(successHandler, onError);
            //pedometer.stopPedometerUpdates(successCallback, failureCallback);
            pedometer.isDistanceAvailable(successCallback, failureCallback);
            pedometer.isStepCountingAvailable(successCallback, failureCallback);
        }
app.initialize();

isDistanceAvailable, isStepCountingAvailable both returns success.

I ma testing it on Xiaomi redmi 3 with Android Lolipop 5.0.

Thanks for any help.

2 Answers2

0

If you're getting any sensor errors when calling the function startPedometerUpdates(), the below GitHub issue (in link) might help. I was getting the above error until I made some changes suggested in below link.

There are two steps I followed:

  • Add this permission into AndroidManifest.xml: <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
  • Update the code in 'PedoListener.java' (follow the link below...)
ERROR: 
code: 3 
message: "Device sensor returned an error."

https://github.com/leecrossley/cordova-plugin-pedometer/issues/24

sammy_boi
  • 11
  • 3
-1

You can try something like this:

    <script>
      let app = {

      init: function() {

        pedometer.isStepCountingAvailable(function(){
            //alert(""Data available);
        }, function(){
            alert( "Step counting is NOT available on your device");
        });


        let successHandler = function (pedometerData) {

           alert(pedometerData.numberOfSteps);

        };
        pedometer.startPedometerUpdates(successHandler, onError);

        function onError(etext) {
           alert("error="+JSON.stringify(etext));
        };

      },

  }


    document.addEventListener('deviceready', app.init, false);
    </script>
ToniT
  • 1
  • 1
  • You should always provide an explanation with your answer so people can learn from it and adapt a solution for their code if they have to and not simply copy paste. – Word Rearranger Feb 14 '19 at 15:36