0

I am a beginner at android programming and I have come up with some progress on getting the sensor data (Source code below). Can someone help me with detecting how fast my Android device is moving using these information? For example if I am moving the device fast, it will give me like 10, and if I am moving it slowly, I will get like 1. Aside from accelerometer, all other sensors are also OK for me.

package course.examples.Sensors.ShowValues;

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 SensorRawAccelerometerActivity extends Activity implements
        SensorEventListener {

    private static final int UPDATE_THRESHOLD = 500;
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;

    private TextView mXValueView, mYValueView, mZValueView;
    private long mLastUpdate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mXValueView = (TextView) findViewById(R.id.x_value_view);
        mYValueView = (TextView) findViewById(R.id.y_value_view);
        mZValueView = (TextView) findViewById(R.id.z_value_view);

        // Get reference to SensorManager
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        // Get reference to Accelerometer
        if (null == (mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER)))
            finish();

    }

    // Register listener
    @Override
    protected void onResume() {
        super.onResume();

        mSensorManager.registerListener(this, mAccelerometer,
                SensorManager.SENSOR_DELAY_UI);

        mLastUpdate = System.currentTimeMillis();

    }

    // Unregister listener
    @Override
    protected void onPause() {
        mSensorManager.unregisterListener(this);
        super.onPause();
    }

    // Process new reading
    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            long actualTime = System.currentTimeMillis();

            if (actualTime - mLastUpdate > UPDATE_THRESHOLD) {

                mLastUpdate = actualTime;

                float x = event.values[0], y = event.values[1], z = event.values[2];

                mXValueView.setText(String.valueOf(x));
                mYValueView.setText(String.valueOf(y));
                mZValueView.setText(String.valueOf(z));

            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // N/A
    }
}
Mohd Imran
  • 11
  • 1
  • I would try to use the GPS. It is hard (near impossible) to do this with the accelerometers reliably. – Ali Jun 19 '14 at 10:29

1 Answers1

0

Accelerometers measure change in speed (acceleration), not speed itself. If you are looking for short distances you are out of luck. Long distances you can poll this data from the GPS which gives you a heading and average velocity.

cujo
  • 368
  • 3
  • 16
  • Thanks. How about just using the accelerometer itself? Assume that I'm moving my hand back and forth; can the accelerometer itself give me the required information, how fast I'm moving my hand? – Mohd Imran Jun 19 '14 at 03:48
  • Unfortunately no, but you can guestimate based on the acceleration the device undergoes. But you can never assume the numbers are correct. – cujo Jun 19 '14 at 11:27