Search code examples
androidaccelerometer

how to trigger off an alert when the values of 3axis changes in the accelerometer?


i have this code below:

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;

TextView xCoor; // declare X axis object
TextView yCoor; // declare Y axis object
TextView zCoor; // declare Z axis object

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object
    yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object
    zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object

    // add listener. The listener will be HelloAndroid (this) class
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);

    /*  More sensor speeds (taken from api docs)
        SENSOR_DELAY_FASTEST get sensor data as fast as possible
        SENSOR_DELAY_GAME   rate suitable for games
        SENSOR_DELAY_NORMAL rate (default) suitable for screen orientation changes
    */
}

public void onAccuracyChanged(Sensor sensor,int accuracy){

}

public void onSensorChanged(SensorEvent event){

    // check sensor type
    if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

        // assign directions
        float x=event.values[0];
        float y=event.values[1];
        float z=event.values[2];
// to display the 
        xCoor.setText("Accelerometer X: "+ x);
        yCoor.setText("Accelerometer Y: "+ y);
        zCoor.setText("Accelerometer Z: "+ z);
    }
}

i need to trigger off an alert when one of axis changed its values.... let said when there is an accident and my x axis changed and will trigger off the video upload event.... is there anyone who know and willing to guide me?


Solution

  • You will need something like this (Note that this is more of an example than functional Android specific code):

    float foo = 100f;//Some default value
    
    public void compareX(float x) { //Call this from your onSensorChanged and pass it the X value
    float diff = x - foo;
    if(diff>threshold) //threshold is the baseline value for your sudden change
    {
    uploadVideo();
    }
    else{
    foo = x;
    }
    

    This will most likely not work in practice as the sensor gives you new values quite fast and the diff is unlikely to higher than the threshold. Instead, you'll need to edit it to store the maximum and minimum values from the sensor in a short period of time, and check their difference against the threshold. For example, record the maximum and minium in a 3 second period, and compare those. If their difference is greater than the threshold, which you should have precalculated from some accident test data, then you upload the video or whatever you want to do.