Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap Build: accelerometer not working android

Let me first start by saying that I have seen a lot of these questions, and many of them just says it <script src="cordova-x-x-x.js"></script> and some says not include the cordova.js file into Phonegap Build.

So I have been testing and fixing my code for some time now, and still getting the error from onError function. I have also copied and pasted the code from phonegap docs.

So here's the clean code from the url:

<!DOCTYPE html>
<html>
  <head>
    <title>Acceleration Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    }

    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!');
    }

    </script>
  </head>
  <body>
  </body>
</html>

And in my config.xml file I have added:

<gap:plugin name="org.apache.cordova.device-motion" />
<gap:plugin name="org.apache.cordova.device-orientation" />
like image 526
Marius Djerv Avatar asked Mar 13 '26 17:03

Marius Djerv


1 Answers

After testing and asking the Phonegap Build crew, they showed me a code with all the functions. And this is the best way to get the accelerometer to work with Phonegap Build

    <!DOCTYPE html>
<html>
  <head>
    <title>Device Ready Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    function onDeviceReady() {
        alert("Device is Ready");
        alert(device.available);
    }

    function getAcceleration(){
        navigator.accelerometer.getCurrentAcceleration(onAccelSuccess, onError);
    }

    function onAccelSuccess(acceleration) {
        var element = document.getElementById('accelerometer');
        element.innerHTML = 'Acceleration X: ' + acceleration.x         + '<br />' +
                            'Acceleration Y: ' + acceleration.y         + '<br />' +
                            'Acceleration Z: ' + acceleration.z         + '<br />' +
                            'Timestamp: '      + acceleration.timestamp + '<br />';
    }

    function onError() {
        alert('onError!');
    }

    function startWatch() {
        // Update acceleration every 1 seconds
        var options = { frequency: 1000 };

        watchID = navigator.accelerometer.watchAcceleration(onAccelSuccess, onError, options);
    }

    function stopWatch() {
        if (watchID) {
            navigator.accelerometer.clearWatch(watchID);
            watchID = null;
        }
    }
    </script>
  </head>
  <body onload="onLoad()">
    <p>
        <button onclick="getAcceleration()">Get Acceleration</button>
    </p>
    <p>
        <button onclick="startWatch()">Watch Acceleration</button>
    </p>
    <p>
        <button onclick="stopWatch()">Stop Watching Acceleration</button>
    </p>
    <div id="accelerometer">Waiting for accelerometer...</div>
  </body>
</html>
like image 154
Marius Djerv Avatar answered Mar 15 '26 07:03

Marius Djerv