This plugin provides access to the device’s accelerometer. The accelerometer is a motion sensor that detects the change (delta) in movement relative to the current device orientation, in three dimensions along the x, y, and z axis.

Methods

navigator.accelerometer.getCurrentAcceleration
navigator.accelerometer.watchAcceleration
navigator.accelerometer.clearWatch

Example:

function onSuccess(acceleration) {
  alert(‘Acceleration X: ‘ + acceleration.x + ‘\n’ +
    ‘Acceleration Y: ‘ + acceleration.y + ‘\n’ +
    ‘Acceleration Z: ‘ + acceleration.z + ‘\n’ +
    ‘Timestamp: ‘ + acceleration.timestamp + ‘\n’
  );
};

function onError() {
  alert(‘onError!’);
};

navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);

watchAcceleration and clearWatch Example

var watchId;

accelerometerWatch = function() {
  var options = {frequency:200};

  watchId = navigator.accelerometer.watchAcceleration(accelerationSuccess,accelerationFail,options);
};

clearAccelerometer =function() {
  navigator.accelerometer.clearWatch(watchId);
};

accelerationSuccess = function(acc) {
  document.getElementById(“id-of-element-x”).innerHTML = “Dimension X acceleration: “+acc.x;
  document.getElementById(“id-of-element-y”).innerHTML = “Dimension Y acceleration: “+acc.y;
  document.getElementById(“id-of-element-z”).innerHTML = “Dimension Z acceleration: “+acc.z;
};

accelerationFail = function() {
  alert( “Accelerometer error, device is not supporting acceleration or an error occurred”);
};

Source Documentation:
http://plugins.cordova.io/#/package/org.apache.cordova.device-motion

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *