Detecting device movement using the accelerometer
The accelerometer captures device motion in the x, y, and z axis direction. The accelerometer is a motion sensor that detects change (delta) in movement relative to the orientation of the current device.
How to do it…
We will use the accelerometer functionality from the plugins to monitor the feedback from the device:
- First, create a new PhoneGap project named
accelerometer
. Open Terminal or Command Prompt and run the following command:phonegap create accelerometer com.myapp.accelerometer accelerometer
- Add the device's platform. You can choose to use Android, iOS, or both:
cordova platform add ios cordova platform add android
- Add the
device-motion
plugin by running the following command:phonegap plugin add org.apache.cordova.device-motion
- Open
www/index.html
and clean up unnecessary elements; so you have the following:<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <...