user1086105
user1086105

Reputation: 247

Android: How to collect sensor values for a fixed period of time?

I want to collect for ca. 5 seconds the values of the Accelerometer. What is a proper way to do this?

I tried it by implementing an AsyncTask which registers and unregisters itself at the sensor manager for the accelerometer and then sleeps for 5 seconds. But during those 5s of sleeping the onSensorChanged method isn't invoked :D...

The motivation behind my sought is the following: In order to calibrate my accelerometer I want to collect for some seconds sensor values (while the phone is lying on the floor) and then get the bias by computing the mean values...

I appreciate any hint!

Upvotes: 0

Views: 1708

Answers (1)

Renard
Renard

Reputation: 6929

Assuming you are starting your sensors in onResume:

/**
 * start sensors
 */
public void onResume() {
    mSensorManager.registerListener(mListener, mSensorAcceleration, SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(mListener, mSensorMagnetic, SensorManager.SENSOR_DELAY_GAME);
    Handler h = new Handler();
    h.postDelayed(new Runnable() {

        @Override
        public void run() {
        //    do stuff with sensor values
            mSensorManager.unregisterListener(mListener);               
        }
    }, 5000);
...

Upvotes: 3

Related Questions