DedrisFrameworkDedrisFramework

← Plugins  /  Accelerometer

📐

Accelerometer

cordova-plugin-device-motion
iOS Android

This Apache Cordova plugin reads the device's accelerometer — the motion sensor that detects how the device is moving along the X, Y and Z axes. Use it for shake-to-refresh, tilt controls, step counting and motion-driven UI.

Installation

terminal
cordova plugin add cordova-plugin-device-motion

API

The plugin lives on navigator.accelerometer. Call these after deviceready:

MethodDescription
getCurrentAcceleration(success, error)Read the acceleration once.
watchAcceleration(success, error, options)Read the acceleration repeatedly; returns a watch ID. options.frequency sets the interval in ms.
clearWatch(watchID)Stop a watch started with watchAcceleration.

The Acceleration object

PropertyDescription
x / y / zAcceleration on each axis in m/s², including gravity.
timestampWhen the reading was taken, in milliseconds.

Usage example

Watch acceleration a few times a second and stop after use:

www/js/app.js
var watchID = navigator.accelerometer.watchAcceleration(
  function (acc) {
    console.log(acc.x + ", " + acc.y + ", " + acc.z);
  },
  function () {
    console.error("No accelerometer");
  },
  { frequency: 200 }
);

// Later, when you no longer need it:
navigator.accelerometer.clearWatch(watchID);

Tips

📚 Source: the technical reference on this page (the methods and the Acceleration object) is based on the official Apache Cordova device-motion documentation. Descriptions and examples have been rewritten in DedrisFramework's own words. Apache Cordova documentation is published by the Apache Software Foundation under the Apache License 2.0.
← Back to plugins Start a project