Here’s the code. Basically when I tap on the app nothing happens (yet it is supposed to stop the accelerometer). It’s a JS/DOM problem I reckon.
<!DOCTYPE html>
UNH BSApp
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// The watch id references the current `watchAcceleration`
var watchID = null;
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
startWatch();
}
function onMouseClick() {
a stopWatch();
}
// Start watching the acceleration
//
function startWatch() {
// Update acceleration every 0.1 seconds
var options = { frequency: 10 };
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>
<style>
#start {
display:block;
border:solid;
}
</style>
</head>
<body>
<div id="accelerometer">Waiting for accelerometer...</div>
<div id="start">Start</div>
</body>
</html>
onMouseClick() isn’t bound to an event. Since you’re using PhoneGap I’m assuming you’ll want to bind to either touchstart or touchend.
You’ll probably want to add the event listeners once the document is fully loaded as well.