voltmx.accelerometer Namespace
The voltmx.accelerometer namespace provides the following API elements for managing and retrieving data from the device’s accelerometer.
Accelerometer Functions
The voltmx.accelerometer namespace provides the following functions.
voltmx.accelerometer.registerAccelerationEvents
Registers event handlers for acceleration events, such as ‘shake’.
Syntax
Input Parameters
Parameter | Description |
---|---|
events | An object that specifies a hash table containing the events. This table has the key-value pairs that specify the event and the event handler. |
Example
Return Values
None.
Remarks
Event handlers that you set with this function are called asynchronously whenever an accelerometer event occurs. The event handler is triggered only at the end of the event. For example, a shake
event handler is called after the shaking has stopped, indicating that device motion has occurred. The precision with which a shake
event can be recognized may vary per platform and depends on the device capability.
The table contained in the event parameter has the following format: {<event>:<event-handler-function>}
where <event>
is the name of the event and <event-handler-function>
is the name of the event handler function for that specific event. For example, to set an event handler for the shake
event, the table would look similar to the following.
{shake:onshake}
Note: Currently, shake is the only event that is supported.
Platform Availability
Available on all platforms except SPA, Desktop Web and Mobile Web.
voltmx.accelerometer.retrieveCurrentAcceleration
Sets callback functions for retrieving the current device acceleration.
Syntax
Input Parameters
Parameter | Description |
---|---|
onSuccessCallback | The callback function that is executed when retrieving the current device acceleration is successful. For details, see the Remarks section below. |
onFailureCallback | The callback that is executed when an error occurs while retrieving the current device acceleration. For details, see the Remarks section below. |
Example
Example 1:
Example 2:
Return Values
None.
Remarks
This function sets twp callback that your app uses for retrieving the current acceleration information from the device. One is called if the acceleration was successfully retrieved, while the other is called if it was not.
The onSuccessCallback parameter contains a callback function that is invoked upon success has the following syntax:
onSuccessCallback(accelerometerdata);
The accelerometerdata parameter to the onSuccessCallback
function is a table containing key-value pairs, as explained in the following.
Key | Type | Description |
---|---|---|
x | Floating Point Number | The acceleration in the X direction. |
y | Floating Point Number | The acceleration in the Y direction. |
z | Floating Point Number | The acceleration in the Z direction. |
timestamp | Floating Point Number | The number of milli seconds elapsed since the start of the Unix Epoch. The standard Unix Epoch is 00:00:00 UTC on 1 January 1970. The timestamp does not reflect the frequency at which the device can retrieve the accelerometer data, because the device capability (in terms of frequency) can vary from one platform to the other. |
The onFailureCallback parameter contains callback function that is invoked if an error occurs has the following syntax:
onFailureCallback();
The onFailureCallback
function has no parameters. It enables your app to handle the error however you want it to.
The two callback functions are invoked asynchronously and this function returns the value immediately without waiting for actual retrieval of the device acceleration data.
Platform Availability
Available on all platforms except SPA, Desktop Web and Mobile Web.
voltmx.accelerometer.startMonitoringAcceleration
Starts monitoring the device’s acceleration on a continuous basis.
Syntax
Input Parameters
Parameter | Description |
---|---|
onSuccessCallback | The callback function that is executed when retrieving the current device acceleration is successful. For details, see the Remarks section below. |
onFailureCallback | The callback that is executed when an error occurs while retrieving the current device acceleration. For details, see the Remarks section below. |
configData | A JavaScript object that specifies the configuration parameters for the monitoring operation. For details, see the Remarks section below. |
Example
Example 1:
Example 2:
Return Values
None.
Remarks
By calling this function, your app can start monitoring the device acceleration or motion continuously. When there is a change in the device acceleration values because the device moves, the callback functions passed in through this function’s parameters are invoked asynchronously. This function returns immediately without waiting for the device initialization for accelerometer.
The onSuccessCallback parameter contains a callback function that is invoked upon success has the following syntax:
onSuccessCallback(accelerometerdata);
The accelerometerdata parameter to the onSuccessCallback
function is a table containing key-value pairs, as explained in the following.
Key | Type | Description |
---|---|---|
x | Floating Point Number | The acceleration in the X direction. |
y | Floating Point Number | The acceleration in the Y direction. |
z | Floating Point Number | The acceleration in the Z direction. |
timestamp | Floating Point Number | The number of milli seconds elapsed since the start of the Unix Epoch. The standard Unix Epoch is 00:00:00 UTC on 1 January 1970. The timestamp does not reflect the frequency at which the device can retrieve the accelerometer data, because the device capability (in terms of frequency) can vary from one platform to the other. |
The callback function that is invoked if an error occurs has the following syntax:
onFailureCallback();
The onFailureCallback
function has no parameters. It enables your app to handle the error however you want it to.
The two callback functions are invoked asynchronously and this function returns the value immediately without waiting for actual retrieval of the device acceleration data.
When your app invokes the voltmx.accelerometer.startMonitoringAcceleration
function, the third parameter that your app must pass is configData. The configData parameter contains a JavaScript object that holds a set of key-value pairs that must be in the following format.
Key | Type | Description |
---|---|---|
frequency | Floating Point Number | The time interval, in milliseconds, in which accelerometer data needs to be retrieved. The default value of frequency must be “200” milliseconds minimum. Any negative value specified in the frequency reverts to the default value i.e., 200ms. |
onchange | Boolean | A value that determines whether or not to trigger an event whenever the device is moving regardless of the value specified in frequency . If onchange is set to true , the number set in frequency is not respected and the onSuccessCallback event is invoked whenever the device is in motion. If this value is set to false, the onSuccessCallback event is invoked in the time interval specified in the frequency parameter is used. The default value for onchange is false. |
Note: If you set
onchange
tofalse
, it is necessary to specify afrequency
value or the onSuccessCallback function is never invoked.
Platform Availability
Available on all platforms except SPA, Desktop Web, and Mobile Web.
voltmx.accelerometer.stopMonitoringAcceleration
Stops the device monitoring activity if it is active.
Syntax
Example
Input Parameters
None.
Return Values
None.
Remarks
If your app has been continuously monitoring the device’s motion, it calls the voltmx.accelerometer.stopMonitoringAcceleration
to stop. Apps can start monitoring the device motion using the accelerometer.startmonitoringacceleration function.
Platform Availability
Available on all platforms except SPA, Desktop Web, and Mobile Web.
voltmx.accelerometer.unregisterAccelerationEvents
Unregisters event handlers for the specified acceleration event types.
Syntax
Input Parameters
Parameter | Description |
---|---|
eventTypes | An array of events. |
Example
Return Values
None.
Remarks
After this function returns, the event specified in the eventTypes parameter no longer have event handlers registered for them. As a result, your app no longer receives notifications of those events.
Platform Availability
Available on all platforms except SPA, Desktop Web, and Mobile Web.