Search code examples
androidaccelerometersampling

android accelerometer registerListener


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?


Solution

  • 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);