3

I have this code from PhoneGap documentation at http://docs.phonegap.com/en/2.9.0/cordova_accelerometer_accelerometer.md.html#Accelerometer

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

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

    // The watch id references the current `watchAcceleration`
    var watchID = null;

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        startWatch();
    }

    // Start watching the acceleration
    //
    function startWatch() {

        // Update acceleration every 3 seconds
        var options = { frequency: 3000 };

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

    // Stop watching the acceleration
    //
    function stopWatch() {
        if (watchID) {
            navigator.accelerometer.clearWatch(watchID);
            watchID = null;
        }
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    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 />';
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
  </head>
  <body>
    <div id="accelerometer">Waiting for accelerometer...</div>
  </body>
</html>

I used this code on build.phonegap.com to make an app and downloaded the .wgz file on Nokia 5800 (Symbian). The app got installed and it loads successfully. But it just show "Waiting for accelerometer...". That is, the content of the <div> tag does not change. Why is that?

Ankur
  • 233
  • 3
  • 11

3 Answers3

3

I got the answer at Phonegap forum. In the line:

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

src should be set to "cordova.js".

Ankur
  • 233
  • 3
  • 11
  • It can be set to anything you want! The important part is, that you **remove the file itself**, if you're compiling through PhoneGap Build (it will inject all necessary the files for you) and only **leave the reference**. So, in your case not the `src="cordova-x.x.x.js"` causes the problem, but the fact, that you (probably) had file `cordova-x.x.x.js` included with your project. You changed reference, so for compiler it gives the same results as removing the file. – trejder Jul 17 '13 at 11:11
  • I did not upload any `.js` file to build.phonegap.com. I only uploaded the above HTML file. It wasn't working earlier but it started to work when I set the `src` to `"cordova.js"`. An interesting observation is that also works if I set `src` to `"phonegap.js"`. – Ankur Jul 17 '13 at 15:04
  • Seems that PhoneGap Build is only able to detect `phonegap.hs` and `cordova.js` reference to inject, the code, but not when you reference a file name with version in it. Interesting observation. I believe, they're checking for precisely given file name, and not doing some search/replace, to not to cut-off more than they need. And, yeah -- PhoneGap / Cordova names works together, as two names for the same project, and so is with main Javascript file, both works. – trejder Jul 18 '13 at 06:54
2

Change this statement

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

to

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

this is for including a JS file named cordova.js which access the native feature of phone. This is the file you can download from phonegap release site here http://phonegap.com/install/

For the latest release of cordova there is no such need to do. Now version is mentioned in the file itself not in the file name. But for make your acceleration code work this time with the latest release dont forget to add the related plugin for device motion.

vineetv2821993
  • 927
  • 12
  • 25
1

Change src="cordova-x.x.x.js" to src="cordova.js" and it will work.

Louay Alakkad
  • 7,132
  • 2
  • 22
  • 45