How to Read Accelerometer Values in Icenium

In this article we will have a look at reading the Accelerometer device's values in Icenium. As we know Icenium is a Cloud based IDE that works on top of Cordova or Phonegap. We will use the Accelerometer API of Cordova to read the device accelerometer values.
Let us say that on the click event of a button we want to read the current accelerometer value. The button can be created as in the following:

<div id="homeview" data-role="view" data-title="Home View">

            <a data-click="GetData" data-role="button">Get Accelorometer Data</a>

            <div id="outputdiv" ></div>

</div>

On the click event of the button we will read the Accelerometer values and display them in an output div. On the click event of the button we are calling the GetData JavaScript function. In the GetData function we are making a call to function "navigator.accelerometer.getCurrentAcceleration". In Icenium the Phonegap reference is added by default, hence we do not need to add a reference to work with Cordova.

Icenium.jpg

In onSuccess we will read the accelerometer values and in the onError function we will display an error message.
In the onSucess function we will read X, Y, Z and TimeStamp values as data.x, data.y, data.z and data.timestamp respectively.

  function onSuccess(data) {
           
var outputdiv = document.getElementById('outputdiv');
            outputdiv.innerHTML =
'X: ' + data.x + '<br />' +
                                 
'Y: ' + data.y + '<br />' +
                                 
'Z: ' + data.z + '<br />' +
                                 
'Time: ' + data.timestamp;
        }


In the onError function let us display an error message as in the following:

function onError() {

            alert("error occured");

        }

In this way we can read accelerometer values in Icenium. I hope you find this article useful. Thanks for reading.
 

Up Next
    Ebook Download
    View all
    Learn
    View all