Page 1 of 1

How can I use the accelerometer?

Posted: Fri Jan 06, 2012 1:17 am
by Feeniix_

Hi, I'd like my page to refresh and change a tiggr component whenever the mobile device is shaken, i believe I can do this with the phonegap API? Could you please explain how this is done?
Regards,
Jaimee


How can I use the accelerometer?

Posted: Fri Jan 06, 2012 6:14 am
by maxkatz

Yes, you can do it with PhoneGap API: http://docs.phonegap.com/en/1.3.0/pho.... Here is how to use PhoneGap API in Tiggr: http://help.gotiggr.com/documentation.... Let us know if you have any other questions.


How can I use the accelerometer?

Posted: Mon Jan 09, 2012 11:17 pm
by Feeniix_

Hi Max,

I understand how I can use code on an event on a component, but what I would like is to have the app react when the phone is shaken. Where would I start to put the code for this?

Would it be upon page load?

Regards,
Jaimee


How can I use the accelerometer?

Posted: Tue Jan 10, 2012 12:09 am
by maxkatz

Once you get x, y, and z coordinates from: http://docs.phonegap.com/en/1.2.0/pho..., you can determine (based on app logic) whether the app should react to this change.


How can I use the accelerometer?

Posted: Tue Jan 10, 2012 12:51 am
by Feeniix_

Where do I place the code to have it react?

Also, I'd really like to have the user 'swipe' pages left or right and have the app recat by changing the page. Where would I put code for that type of input?


How can I use the accelerometer?

Posted: Tue Jan 10, 2012 12:59 am
by maxkatz

Here is an example (from PhoneGap API):

Create a new JavaScript file, any name will do:
code
// The watch id references the current 'watchAcceleration'
var watchID = null;

Code: Select all

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

 // PhoneGap is ready 
 // 
 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 = Tiggr('mobilelabel1'); 
     element&#46;html('Acceleration X: ' + acceleration&#46;x + '<br &#47;>' + 
                         'Acceleration Y: ' + acceleration&#46;y + '<br &#47;>' + 
                         'Acceleration Z: ' + acceleration&#46;z + '<br &#47;>' + 
                         'Timestamp: '      + acceleration&#46;timestamp + '<br &#47;>'); 
 } 

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

/code

Place Label component on a screen (default text: Waiting...), the component name should be 'mobilelabel1' (if you change the name, update the reference in onSuccess function.

Run the app on the device or via Tiggr Mobile Tester.

As for swipe events, here is how to do it: http://getsatisfaction.com/gotiggr/to...