1

I am new to android and working on accelerometer. I want to collect 20 samples of x,y,z per second, for this can I use the following? registerListener(SensorEventListener listener, Sensor sensor, int rate)in the int rate can I use 3000000(microseconds) so that I get one x,y,z value for every 3000000(microseconds) or 3 seconds so it would be like registerListener(this, Accelerometer , 3000000); and get 20 samples of x,y,z in 1 seconds. If I am wrong can anyone suggest me how to solve this?

  • Deprecated Use `SensorEventListener` instead. [Android: I want to shake it][1] [1]: http://stackoverflow.com/questions/2317428/android-i-want-to-shake-it – msysmilu Jan 21 '15 at 12:34

1 Answers1

6

Check out the Javadocs for SensorManager.registerListener()

You need to pass in one of the 4 constants, you cannot enter an arbitrary value like you are trying to do.

The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST or, the desired delay between events in microsecond.

so an example call might look like this:

registerListener(this, Accelerometer , SensorManager.SENSOR_DELAY_FASTEST);

EDIT: Tim.footInMouth() I didn't see the last part of the definition. You can actually pass it microseconds.

I think the math for your seconds is off a little

You want 20 samples per second (1000 milliiseconds)

So you want 1 sample per 50 milliseconds

A microsecond is 1/1000 of a millisecond

So 1000 microseconds = 1 milisecond

and 50,000 microseconds = 50 millisecond = 20 times per second.

So your call should be:

registerListener(this, Accelerometer , 50000);
FoamyGuy
  • 45,328
  • 16
  • 118
  • 151
  • Thank you very much!can you suggest me an alternative solution? – viswanath patimalla Mar 30 '12 at 02:05
  • Did you see the part after the edit? I think that is the solution you are looking for. – FoamyGuy Mar 30 '12 at 02:07
  • Thank you.You are so quick!So this means that after 50000 microseconds public void onSensorChanged(SensorEvent event) would be called?Or I am wrong? All I want is to store each x,y,z sample values with each sample having a 50000 microsec difference – viswanath patimalla Mar 30 '12 at 02:15
  • I don't know exactly, I don't have any experience with the Accelerometer. Did you try running it? – FoamyGuy Mar 30 '12 at 02:24
  • yes I have tried but need clarity on when the onSensorChanged(SensorEvent event) method is called, is it called each and every second or only after 50000sec, and from where is it called – viswanath patimalla Mar 30 '12 at 05:00