10 Replies Latest reply on Aug 21, 2016 9:16 AM by cnup25973865

    Accelerometer not working with Phonegap Build

    cnup25973865

      The following example works fine on smart phone Infocus m210 and  does not work on Asus Zenfone 2 (zen kl-500).

      Any ideas why?

      ------------------------------------------------------------------------

      <!DOCTYPE html>

      <html>

      <head>

      <title>Acceleration Example</title>

      <script type="text/javascript" charset="utf-8">

      var watchID = null;

      function onLoad(){

         document.addEventListener("deviceready", onDeviceReady, false);

      }

      function onDeviceReady() {

           startWatch();

      }

      function startWatch() {

         var options = { frequency: 1000 };

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

      }

      function stopWatch() {

         if (watchID) {

           navigator.accelerometer.clearWatch(watchID);

           watchID = null;

         }

      }

      function onSuccess(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!');

      }

      </script>

      </head>

      <body onload="onLoad()">

        <div id="accelerometer">Waiting for accelerometer...</div>

        <button onclick="startWatch();">Start Watching</button>

        <button onclick="stopWatch();">Stop Watching</button>

        <script type="text/javascript" src="cordova.js"></script>

      </body>

      </html>

      -------------------------------------------------------