Finalmente ci sono riuscito...per qualcuno sembrerā banale (sono alle prime armi con android) ma questa semplicissimo script mostra sul display i valori dell'accelerometro e le posizioni in gradi degli assi X Y Z del telefono... Posto i file java e main.xml da dare in pasto ad eclipse:
AccloggerActivity.java:
codice:
package com.example.android.acclogger;
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.util.Log;
import android.widget.TextView;
public class AccloggerActivity extends Activity implements SensorEventListener {
final String tag = "AccLogger";
SensorManager sm = null;
TextView xAccView = null;
TextView yAccView = null;
TextView zAccView = null;
TextView xOrView = null;
TextView yOrView = null;
TextView zOrView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get reference to SensorManager
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
xAccView = (TextView) findViewById(R.id.xbox);
yAccView = (TextView) findViewById(R.id.ybox);
zAccView = (TextView) findViewById(R.id.zbox);
xOrView = (TextView) findViewById(R.id.xboxo);
yOrView = (TextView) findViewById(R.id.yboxo);
zOrView = (TextView) findViewById(R.id.zboxo);
}
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
float [] values = event.values;
synchronized (this) {
Log.d(tag, "onSensorChanged: " + sensor + ", x: " +
values[0] + ", y: " + values[1] + ", z: " + values[2]);
if (sensor.getType() == Sensor.TYPE_ORIENTATION) {
xOrView.setText("Yaw: " + values[0]);
yOrView.setText("Pitch: " + values[1]);
zOrView.setText("Roll: " + values[2]);
}
else if (sensor.getType() == Sensor.TYPE_ACCELEROMETER ) {
xAccView.setText("Sway: " + values[0]);
yAccView.setText("Surge: " + values[1]);
zAccView.setText("Heave: " + values[2]);
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}
@Override
protected void onResume() {
super.onResume();
Sensor Accel = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor Orient = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
// register this class as a listener for the orientation and accelerometer sensors
sm.registerListener((SensorEventListener) this, Accel, SensorManager.SENSOR_DELAY_FASTEST);
sm.registerListener((SensorEventListener) this, Orient, SensorManager.SENSOR_DELAY_FASTEST);
}
@Override
protected void onStop() {
// unregister listener
sm.unregisterListener(this);
super.onStop();
}
}
main.xml :
codice:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Accelerometer"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X Value"
android:id="@+id/xbox"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y Value"
android:id="@+id/ybox"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z Value"
android:id="@+id/zbox"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Orientation"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X Value"
android:id="@+id/xboxo"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y Value"
android:id="@+id/yboxo"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z Value"
android:id="@+id/zboxo"
/>
</LinearLayout>