Search code examples
androidionic-frameworkcordova-pluginsngcordova

Ionic : Cannot read property 'getCurrentAcceleration' of undefined


I'm unable to get the value from Accelerometer on Ionic Framework and here is the code below,

Executing the code get an error :

Cannot read property 'getCurrentAcceleration' of undefined

.controller('DashCtrl', function($cordovaDeviceMotion) {

    $cordovaDeviceMotion.getCurrentAcceleration().then(function(result) {
      var X = result.x;
      var Y = result.y;
      var Z = result.z;
      var timeStamp = result.timestamp;
      console.log(result);
    }, function(err) {
        console.log(err);
      // An error occurred. Show a message to the user
    });


  // watch Acceleration
  var options = { frequency: 20000 };

    var watch = $cordovaDeviceMotion.watchAcceleration(options);
    watch.then(
      null,
      function(error) {
      // An error occurred
      },
      function(result) {
        var X = result.x;
        var Y = result.y;
        var Z = result.z;
        var timeStamp = result.timestamp;
    });


    watch.clearWatch();
    // OR
    $cordovaDeviceMotion.clearWatch(watch)
      .then(function(result) {
          console.log(result);
        // success
        }, function (error) {
        // error
      });
})

More Info :

  1. I got the code from http://ngcordova.com/docs/plugins/deviceMotion/
  2. When I print $cordovaDeviceMotion, I am getting empty{} object
  3. Create Issue on GitHub : https://github.com/driftyco/ng-cordova/issues/1207

Solution

  • $cordovaDeviceMotion is not working on the browser but working on my android deceive. we can't able to test on browser.

    ngCordova : Right now the device motion not working on browser but working in device(App) Apache Cordova : Working on both browser and app.

    So, I raised the issue on GitHub