Monday, August 9, 2010

Detect rotation around X, Y & Z axis, using SensorManager and SensorEventListener

SensorManager is a class that lets you access the device's sensors. SensorEventListener is used for receiving notifications from the SensorManager when sensor values have changed.



The method onSensorChanged(SensorEvent event) of SensorEventListener will be called when sensor values have changed. The rotation in degree can be retrieved from event.values[0], event.values[1] and event.values[2].

event.values[0]: azimuth, rotation around the Z axis.
event.values[1]: pitch, rotation around the X axis.
event.values[2]: roll, rotation around the Y axis.

Modify main.xml to have three TextView to display azimuth, pitch and roll.
<?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:id="@+id/textazimuth"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Azimuth: "
/>
<TextView
android:id="@+id/textpitch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pitch: "
/>
<TextView
android:id="@+id/textroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Roll: "
/>
</LinearLayout>


AndroidSensorEventListener.java
package com.exercise.AndroidSensorEventListener;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidSensorEventListener extends Activity {

TextView textviewAzimuth, textviewPitch, textviewRoll;
private static SensorManager mySensorManager;
private boolean sersorrunning;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textviewAzimuth = (TextView)findViewById(R.id.textazimuth);
textviewPitch = (TextView)findViewById(R.id.textpitch);
textviewRoll = (TextView)findViewById(R.id.textroll);

mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);

if(mySensors.size() > 0){
mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
sersorrunning = true;
Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
sersorrunning = false;
finish();
}

}

private SensorEventListener mySensorEventListener = new SensorEventListener() {

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub

textviewAzimuth.setText("Azimuth: " + String.valueOf(event.values[0]));
textviewPitch.setText("Pitch: " + String.valueOf(event.values[1]));
textviewRoll.setText("Roll: " + String.valueOf(event.values[2]));

}

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

}
};

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

if(sersorrunning){
mySensorManager.unregisterListener(mySensorEventListener);
Toast.makeText(AndroidSensorEventListener.this, "unregisterListener", Toast.LENGTH_SHORT).show();
}
}
}


Download the files.

Related Article:
OrientationEventListener, detect orientation change from the SensorManager.
Implement a Simple Compass using SensorManager and SensorEventListener
Implement a Simple Horizontal Indicator using SensorManager and SensorEventListener



2 comments:

Haris said...

Hai thanks for your post......
It's really helpful.......

Ivan said...

Excellent tutorial, works perfectly