because type_orientation
sensor is deprecated for the android 4.0.3 and above versions.
you have to use Sensor.TYPE_ACCELEROMETER
and Sensor.TYPE_MAGNETIC_FIELD
for version greater than 4.0.3 :
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
and get you values onSensorChanged:
float[] mGravity;
float[] mGeomagnetic;
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
/*Computes the inclination matrix I as well as the rotation matrix R transforming a vector from the device coordinate
* system to the world's coordinate system which is defined as a direct orthonormal basis*/
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
/*Computes the device's orientation based on the rotation matrix*/
SensorManager.getOrientation(R, orientation);
azimut = orientation[0]; // orientation contains: azimut, pitch and roll
}
}
mCustomDrawableView.invalidate();
}
see here for basic code snippet on compass in version greater that 4.0.3.