0

So I'm trying to analyze some data from the sensors. I want the array lists to "fill up" with the data, and then after 500ms, the thread to go away and process it. It seems like the thread only executes once (I know that's what is supposed to happen), but I can't figure out a way to execute it multiple times with it sleeping for 500ms. I tried using while(true), lock, handler, timer and got nowhere.

public void onSensorChanged(SensorEvent sensorEvent) {
    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        accSensorVals = lowPass(sensorEvent.values.clone(), accSensorVals);
    }

    xAxisValues.add(accSensorVals[0]); // [0] - X axis
    yAxisValues.add(accSensorVals[1]); // [1] - Y axis
    zAxisValues.add(accSensorVals[2]); // [2] - Z axis

    new Thread() {
      public void run() {
              try {
                  Thread.sleep(DELAY); // delay for 500 ms
                  checkSpikes(xAxisValues, yAxisValues, zAxisValues);
                  xAxisValues = new ArrayList<>();
                  yAxisValues = new ArrayList<>();
                  zAxisValues = new ArrayList<>();
              } catch (Exception e) {}
      }
    }.start();
}
Terry BRETT
  • 319
  • 2
  • 11

2 Answers2

0

First of all "synchronization", If you would like to process data while fetching them from sensors it might get conflict. You should be aware of that.

Secondly, in android use service to collecting sensor data. check this link https://developer.android.com/guide/components/services.html
The current thread you shared runs only once, you should create a loop inside the thread.

public void onSensorChanged(SensorEvent sensorEvent) {
    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        accSensorVals = lowPass(sensorEvent.values.clone(), accSensorVals);
    }

    xAxisValues.add(accSensorVals[0]); // [0] - X axis
    yAxisValues.add(accSensorVals[1]); // [1] - Y axis
    zAxisValues.add(accSensorVals[2]); // [2] - Z axis

    new Thread() {
      public void run() {
              try {
                  while(true){
                   checkSpikes(xAxisValues, yAxisValues, zAxisValues);
                   xAxisValues = new ArrayList<>();
                   yAxisValues = new ArrayList<>();
                   zAxisValues = new ArrayList<>();
                   Thread.sleep(DELAY); // delay for 500 ms
                  }


              } catch (Exception e) {}
      }
    }.start();
}

Or if you would like to pause collection and just processing the data you should do something like this:

private boolean fetch = true;
public void onSensorChanged(SensorEvent sensorEvent) {
    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        accSensorVals = lowPass(sensorEvent.values.clone(), accSensorVals);
    }

    xAxisValues.add(accSensorVals[0]); // [0] - X axis
    yAxisValues.add(accSensorVals[1]); // [1] - Y axis
    zAxisValues.add(accSensorVals[2]); // [2] - Z axis

    new Thread() {
      public void run() {
              try {
                  while(true){
                   if(fetch){
                     checkSpikes(xAxisValues, yAxisValues, zAxisValues);
                     xAxisValues = new ArrayList<>();
                     yAxisValues = new ArrayList<>();
                     zAxisValues = new ArrayList<>();
                     Thread.sleep(DELAY); // delay for 500 ms
                   } 
                  }


              } catch (Exception e) {}
      }
    }.start();
}
Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26
  • I am using this piece of code in a background service already, and using a while loop consumes alot of CPU cycles so I didn't think that would be best solution. However your solution still doesn't seem to work. But thank you anyway. – Terry BRETT May 04 '17 at 10:28
  • Check this out for infinite loops then, https://developer.android.com/reference/android/app/AlarmManager.html – Abdullah Tellioglu May 04 '17 at 10:31
0

Answering my own question. I've initialized a time variable lastUpdate which gets time now, and then compares it with time now now, if that makes sense and if the difference is about 500 I do my data processing. I got the idea from this post:

How to log data from Android Motion Sensors at a fixed rate

long curTime = System.currentTimeMillis();

    if ((curTime - lastUpdate) >= 500){
        lastUpdate = curTime;
        checkSpikes(xAxisValues, yAxisValues, zAxisValues);
        xAxisValues = new ArrayList<>();
        yAxisValues = new ArrayList<>();
        zAxisValues = new ArrayList<>();
    }
Community
  • 1
  • 1
Terry BRETT
  • 319
  • 2
  • 11