Feeniix_
Posts: 0
Joined: Tue Dec 20, 2011 5:45 am

How can I use the accelerometer?

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

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How can I use the accelerometer?

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.

Feeniix_
Posts: 0
Joined: Tue Dec 20, 2011 5:45 am

How can I use the accelerometer?

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

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How can I use the accelerometer?

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.

Feeniix_
Posts: 0
Joined: Tue Dec 20, 2011 5:45 am

How can I use the accelerometer?

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?

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

How can I use the accelerometer?

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...

Return to “Issues”