Android Accelerometer Sensor, Anomalous Values/Glitches
<p>I'm trying to use the sensors API for the accelerometers on a test phone (Nexus S). With the phone sitting on the bench doing nothing, I get some very strange readings (image attached). Has anyone else seen this sort of behavior?<a href="http://i.stack.imgur.com/ALqSA.png" rel="nofollow">Glitches in accel reading</a>? Any idea how to deal with it?</p>

<pre><code>   package com.example.m6;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
//import android.widget.Toast;

public class HelloAndroidActivity extends Activity implements SensorEventListener {
    private SensorManager sensorManager; 
    private TextView tv;
    private float accel[] = {0,0,0};
    private float gyro[] = {0,0,0};


    InetAddress addr; 
    int port         = 1337;
    DatagramSocket s = null;
    String out       = "";
    PowerManager.WakeLock wl;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try{
            //Lock the screen 
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            //Set up the output screen
            setContentView(R.layout.main);
            tv = (TextView) findViewById(R.id.textview);
            tv.setBackgroundColor(Color.BLACK);
            tv.setTextColor(Color.GREEN);

            //Set up the telemetry feed 
            addr = InetAddress.getByName("192.168.0.12");
            s = new DatagramSocket();

            //Turning off the power management!!
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
            wl.acquire(); 

            //Set up the sensors
            sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

            out += "Done\n";
            tv.setText(out);

        } catch (Exception e) {
            out += "\nInit Error:" + e.getMessage() + "\n";
            tv.setText(out);
        }

    }

    private static final float NS2S = 1.0f / 1000000000.0f;
    private float timestamp;

    int down = 0; 
    float tstart = 0;
    @Override
    public void onSensorChanged(SensorEvent event) {            

        String type = "";
        float dT = 0;       
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            if (timestamp != 0) {
                accel = event.values;
                dT = (event.timestamp - timestamp);
                tstart += dT;
            }
            timestamp = event.timestamp;
            type = "G";
        }

        if(event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            if (timestamp != 0) {
                gyro = event.values;
                dT = (event.timestamp - timestamp);
                tstart += dT;
            }
            timestamp = event.timestamp;
            type = "A";
        }

        String out_packet = tstart / 1000 / 1000 + " "+ type + " " + dT + " " + accel[0] + " " + accel[1] + " " + accel[2] + " " + gyro[0] + " " + gyro[1] + " " + gyro[2] + "\n\0";

        try{
            DatagramPacket p = new DatagramPacket(out_packet.getBytes(),out_packet.length(), addr,port);
            s.send(p);
        }
    catch (Exception e) {
            out += "Packet send error: " + e.getMessage() + "\n";
        }

        tv.setText(timestamp + "\n" + out);
    }

    @Override
    protected void onResume() {
        super.onResume();
        out += "on resume\n";
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL);
        tv.setText(out);
    }

    @Override
    protected void onPause() {
        out += "on pause";
        sensorManager.unregisterListener(this);
        tv.setText(out);
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        out += "on destroy";
        wl.release();
        tv.setText(out);
        super.onDestroy();
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
}
</code></pre>
