trackscanner/src/de/neosoft/trackscanner/TrackScannerActivity.java

changeset 13
0bb6f12124fd
child 14
d115c088b508
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trackscanner/src/de/neosoft/trackscanner/TrackScannerActivity.java	Tue Nov 22 21:36:35 2011 +0100
@@ -0,0 +1,110 @@
+package de.neosoft.trackscanner;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+import android.widget.EditText;
+
+public class TrackScannerActivity extends Activity implements SensorEventListener {
+	private SensorManager sensorManager;
+	private long lastUpdate;
+	private EditText mEdit;
+	private EditText mEdit2;
+	
+	/** Called when the activity is first created. */
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.main);
+        
+		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
+		sensorManager.registerListener(this,
+				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
+				SensorManager.SENSOR_DELAY_NORMAL);
+
+		sensorManager.registerListener(this,
+				sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
+				SensorManager.SENSOR_DELAY_NORMAL);
+	
+		
+		lastUpdate = System.currentTimeMillis();
+
+		mEdit = (EditText)findViewById(R.id.editText1);
+		mEdit2 = (EditText)findViewById(R.id.editText2);
+
+    }
+    
+	@Override
+	public void onSensorChanged(SensorEvent event) {
+		long actualTime = System.currentTimeMillis();
+		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
+			float[] values = event.values;
+			// Movement
+			float x = values[0];
+			float y = values[1];
+			float z = values[2];
+
+			float accelerationSquareRoot = (x * x + y * y + z * z)
+					/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
+
+			mEdit.setText("Accelerometer action: "+accelerationSquareRoot+
+					"\nDelta: "+(actualTime-lastUpdate)+"ms"+
+					"\naX: "+x+
+					"\naY: "+y+
+					"\naZ: "+z);
+			
+			/*
+	         final float  alpha = 0.8;
+	          float[] linear_acceleration;
+
+	          float[] gravity;
+			gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
+	          gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
+	          gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
+
+			linear_acceleration[0] = event.values[0] - gravity[0];
+	          linear_acceleration[1] = event.values[1] - gravity[1];
+	          linear_acceleration[2] = event.values[2] - gravity[2];
+*/
+		}
+
+		if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
+			float[] values = event.values;
+			// Orientation (compass)
+			float x = values[0];
+			float y = values[1];
+			float z = values[2];
+			mEdit2.setText("Magnetic Field:\n"+
+					"\nX = "+x+
+					"\nY = "+y+
+					"\nZ = "+z	);
+		}
+		lastUpdate = actualTime;
+	}
+
+	@Override
+	public void onAccuracyChanged(Sensor sensor, int accuracy) {
+		// TODO Auto-generated method stub
+
+	}
+
+	@Override
+	protected void onResume() {
+		super.onResume();
+		// register this class as a listener for the orientation and
+		// accelerometer sensors
+		sensorManager.registerListener(this,
+				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
+				SensorManager.SENSOR_DELAY_NORMAL);
+	}
+
+	@Override
+	protected void onPause() {
+		// unregister listener
+		sensorManager.unregisterListener(this);
+		super.onStop();
+	}
+}
\ No newline at end of file

mercurial