3

I want to detect whether device is moving or device is stationary. I have a situation where i want to execute task when device is not moving.

Right now i have implemented something like this using sensor but i'm not getting device is moving when i try to move device on plain surface like table.

@Override
    public void onSensorChanged(SensorEvent sensorEvent) {

        double alpha = 0.6;

        double[] gravity = new double[3];
        gravity[0] = 0;
        gravity[1] = 0;
        gravity[2] = 0;

        gravity[0] = alpha * gravity[0] + (1 - alpha) * sensorEvent.values[0];
        gravity[1] = alpha * gravity[1] + (1 - alpha) * sensorEvent.values[1];
        gravity[2] = alpha * gravity[2] + (1 - alpha) * sensorEvent.values[2];

        double[] linear_acceleration = new double[3];
        linear_acceleration[0] = sensorEvent.values[0] - gravity[0];
        linear_acceleration[1] = sensorEvent.values[1] - gravity[1];
        linear_acceleration[2] = sensorEvent.values[2] - gravity[2];

        float magnitude = 0.0f;
        magnitude = (float)Math.sqrt(linear_acceleration[0]*linear_acceleration[0]+linear_acceleration[1]*linear_acceleration[1]+linear_acceleration[2]*linear_acceleration[2]);
        magnitude = Math.abs(magnitude);
        Log.e("Magnitude",magnitude+"");
        if(magnitude>0.2){
            Log.e("walking","Yes");
        }else{
            Log.e("walking","No");
        }
    }
RAAAAM
  • 3,378
  • 19
  • 59
  • 108
  • are you using accelerometer – krishank Tripathi Mar 14 '18 at 15:37
  • 1
    if yes than this might be useful https://stackoverflow.com/a/14574992/9287163 – krishank Tripathi Mar 14 '18 at 15:39
  • 1
    Use Google [ActivityRecognitionApi](https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionApi) to check the device state. – Surender Kumar Mar 14 '18 at 16:18
  • Just be aware that an accelerometer measures change in velocity - not movement per se. So if you are moving something on a table with your hand chances are there is acceleration initially but mostly constant velocity within a threshold until it then decelerates. –  Mar 14 '18 at 16:18

1 Answers1

2

If you are looking for an example there is one below

This code is for walking detection

to get smoother value.

// initialize
private SensorManager sensorMan;
private Sensor accelerometer;

private float[] mGravity;
private double mAccel;
private double mAccelCurrent;
private double mAccelLast;

private boolean sensorRegistered = false;
// onCreate

    sensorMan = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mAccel = 0.00f;
    mAccelCurrent = SensorManager.GRAVITY_EARTH;
    mAccelLast = SensorManager.GRAVITY_EARTH;

    sensorMan.registerListener(this, accelerometer,
            SensorManager.SENSOR_DELAY_NORMAL);
    sensorRegistered = true;
// onSensorChanged

private int hitCount = 0;
private double hitSum = 0;
private double hitResult = 0;

private final int SAMPLE_SIZE = 50; // change this sample size as you want, higher is more precise but slow measure.
private final double THRESHOLD = 0.2; // change this threshold as you want, higher is more spike movement

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        mGravity = event.values.clone();
        // Shake detection
        double x = mGravity[0];
        double y = mGravity[1];
        double z = mGravity[2];
        mAccelLast = mAccelCurrent;
        mAccelCurrent = Math.sqrt(x * x + y * y + z * z);
        double delta = mAccelCurrent - mAccelLast;
        mAccel = mAccel * 0.9f + delta;

        if (hitCount <= SAMPLE_SIZE) {
            hitCount++;
            hitSum += Math.abs(mAccel);
        } else {
            hitResult = hitSum / SAMPLE_SIZE;

            Log.d(TAG, String.valueOf(hitResult));

            if (hitResult > THRESHOLD) {
                Log.d(TAG, "Walking");
            } else {
                Log.d(TAG, "Stop Walking");
            }

            hitCount = 0;
            hitSum = 0;
            hitResult = 0;
        }
    }
}
krishank Tripathi
  • 616
  • 1
  • 7
  • 23
  • Since I only need to detect if user is walking and not count the steps, this code was helpful. Thx! – Sam Jan 17 '23 at 14:42