Android sensor data is lost

I am collecting data in my Android application as shown below (this is a stripped down version):

public class Main extends Activity {

    ...

    public void log5secs(){
        try {
            CollectData collectData = new CollectData(this);
            // collect data for five seconds in the background
            Thread.sleep(5000);
            Log.d("unregister", "gyro from Main: "+collectData.gyroscopeResults.size());
            fingerprint.finish();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }        

}

public class CollectData implements SensorEventListener  {

    ArrayList<SensorEvent> gyroscopeResults = new ArrayList<SensorEvent>();

    public CollectData(Context context){
        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onSensorChanged(SensorEvent event) {
        gyroscopeResults.add(event); 
        Log.d("SensorChanged", "gyro results size: "+gyroscopeResults.size());
    }

    public void finish(){
        unregister();
    }

    private void unregister() {
        Log.d("Unregister", "gyro results size before unregister: "+gyroscopeResults.size());
        sensorManager.unregisterListener(this);
    }
}

      

Thus, the log messages show that every time the onSensorChanged event is raised, the size of the ArrayList grows (reaching 26 in five seconds).

However, when the size is requested from Main or because of a call to unregister (), the size of the ArrayList is zero. In the debugger, I can't see where it changes.

What happened to my data ???

I am running it on HTC Sensation XL.

UPDATE : Get more information! Out of curiosity, I made the ArrayListResults gyroscope a static field in the Main class. The effect was interesting - the second time the recording was made, the results of the first were visible (but not the first time). I'm guessing this means the primary stream ends before the sensor data is written - some kind of buffering problem ???

+3


source to share


1 answer


Your data is not lost, the problem is Thread.sleep()

that stops the program for 5 seconds (including sensor collection), then executes the line fingerprint.finish();

after that starts the sensor collection.

So when you call Log.d("Unregister", "gyro results size before unregister: "+gyroscopeResults.size());

, you get 0

because no sensor data was recorded during sleep.

Here are some tips to get your program to work as you expect.

To collect data in 5 seconds, you can use Timer

(here I am canceling the timer after 5 seconds, but you can continue collecting after clearing ArrayList

to run out of memory)



final Timer timer = new Timer();
final CollectData collectData = new CollectData(MainActivity.this);

timer.schedule(new TimerTask() {
boolean started = false;
@Override
public void run() {
  if (!started) {
    Log.d("unregister", "BEFORE] gyro from Main: " + collectData.gyroscopeResults.size());
    started = true;
  } else {
    collectData.finish();
    Log.d("unregister", "AFTER] gyro from Main: " + collectData.gyroscopeResults.size());
    timer.cancel();
  }
    }
}, 0, 5000);

      

Another solution might be simpler if you want to keep Thread.sleep()

to run log5secs()

withinThread

new Thread() {
    public void run() {
        log5secs();
    }
 }.start();

      

However, the use Timer

would be more appropriate if you want a continuous sensor log to run with data every 5 seconds (e.g. log for file and / or cleanup ArrayList

)

+2


source







All Articles