commit | 8f1ad44eec7ca768257faae92b1b447bf9b02dbb | [log] [tgz] |
---|---|---|
author | Tim Barham <tim.barham@microsoft.com> | Wed Jul 29 10:19:43 2015 +1000 |
committer | Tim Barham <tim.barham@microsoft.com> | Wed Jul 29 11:58:40 2015 +1000 |
tree | 70481b563ad824c4c604e137c460bbf20ca51f19 | |
parent | b0f8e436efd7d0db586bc2165733ebe5467b8498 [diff] |
CB-9426 Fix exception when using device motion plugin on browser platform. The plugin's plugin.xml defines a general <js-module> with the name 'accelerometer'. It defines another <js-module> for the browser platform (that merges with the general module) with the same name. Modules with the same name is not allowed, so Cordova throws an exception at runtime when trying to define the browser version of the module. Since the only difference in the browser version of the module is to fake up devicemotion events, I've deleted the browser version and added that logic to the general module, with the following change: rather than starting a new event timer everytime watchAcceleration() is called (which could happen multiple times resulting in multiple superfluous timers), we have a single event timer going as long as there are active watchers.
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.
Access is via a global navigator.accelerometer
object.
Although the object is attached to the global scoped navigator
, it is not available until after the deviceready
event.
document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { console.log(navigator.accelerometer); }
cordova plugin add cordova-plugin-device-motion
Get the current acceleration along the x, y, and z axes.
These acceleration values are returned to the accelerometerSuccess
callback function.
navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);
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);
Values for X, Y, Z motion are all randomly generated in order to simulate the accelerometer.
iOS doesn't recognize the concept of getting the current acceleration at any given point.
You must watch the acceleration and capture the data at given time intervals.
Thus, the getCurrentAcceleration
function yields the last value reported from a watchAccelerometer
call.
Retrieves the device‘s current Acceleration
at a regular interval, executing the accelerometerSuccess
callback function each time. Specify the interval in milliseconds via the acceleratorOptions
object’s frequency
parameter.
The returned watch ID references the accelerometer's watch interval, and can be used with navigator.accelerometer.clearWatch
to stop watching the accelerometer.
var watchID = navigator.accelerometer.watchAcceleration(accelerometerSuccess, accelerometerError, accelerometerOptions);
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!'); }; var options = { frequency: 3000 }; // Update every 3 seconds var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
The API calls the success callback function at the interval requested, but restricts the range of requests to the device between 40ms and 1000ms. For example, if you request an interval of 3 seconds, (3000ms), the API requests data from the device every 1 second, but only executes the success callback every 3 seconds.
Stop watching the Acceleration
referenced by the watchID
parameter.
navigator.accelerometer.clearWatch(watchID);
navigator.accelerometer.watchAcceleration
.var watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options); // ... later on ... navigator.accelerometer.clearWatch(watchID);
Contains Accelerometer
data captured at a specific point in time. Acceleration values include the effect of gravity (9.81 m/s^2), so that when a device lies flat and facing up, x, y, and z values returned should be 0
, 0
, and 9.81
.