← Plugins / Accelerometer
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.
cordova plugin add cordova-plugin-device-motion
The plugin lives on navigator.accelerometer. Call these after deviceready:
| Method | Description |
|---|---|
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. |
| Property | Description |
|---|---|
x / y / z | Acceleration on each axis in m/s², including gravity. |
timestamp | When the reading was taken, in milliseconds. |
Watch acceleration a few times a second and stop after use:
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);
clearWatch when leaving a screen — an open watch drains the battery.DeviceMotionEvent Web API is a modern alternative where it's supported.