What Is the Alternative to Android Orientation Sensor

What is the alternative to android orientation sensor?

There is no actual "orientation" sensor - this is (was) actually a composite sensor, generated from a combination of the accelerometer and the magnometer.

From http://developer.android.com/reference/android/hardware/SensorEvent.html

"This sensor type exists for legacy reasons, please use getRotationMatrix() in conjunction with remapCoordinateSystem() and getOrientation() to compute these values instead."

public class OrientationTestActivity extends Activity implements SensorEventListener
{
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mMagnetometer;

private float[] mLastAccelerometer = new float[3];
private float[] mLastMagnetometer = new float[3];
private boolean mLastAccelerometerSet = false;
private boolean mLastMagnetometerSet = false;

private float[] mR = new float[9];
private float[] mOrientation = new float[3];

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}

protected void onResume() {
super.onResume();
mLastAccelerometerSet = false;
mLastMagnetometerSet = false;
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
}

protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor == mAccelerometer) {
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometerSet = true;
} else if (event.sensor == mMagnetometer) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometerSet = true;
}
if (mLastAccelerometerSet && mLastMagnetometerSet) {
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mOrientation);
Log.i("OrientationTestActivity", String.format("Orientation: %f, %f, %f",
mOrientation[0], mOrientation[1], mOrientation[2]));
}
}
}

Sensor.TYPE_ROTATION_VECTOR has drift: Is there an alternative?

https://play.google.com/store/apps/details?id=org.hitlabnz.sensor_fusion_demo has some handy alternatives I just found. I still have to test which fusion leads to most steady results.
Source here: https://bitbucket.org/apacha/sensor-fusion-demo/src .

A lot of these also show drift; in particular the ones which use the gyro.

Locking ScreenOrientation

In general onConfigurationChanged() event fire only when an configuration change has occured. In your app the orientation changed event occurs only when the screen is free to rotate. If you have locked the screen orientation then the screen orientation event is not fired. onConfigurationChanged() does not listen to the sensor that is responsible to rotate the device but fires only when the appropriate event is happening.

So what you really want is to have access to a SensorManager and attach a SensorListener. This is the way for you to listen to the actual orientation of the device.

Here is a very nice demo that demonstrates the SensorManager capabilities with the orientation of the phone:

http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/

UPDATE: The orientation sensor is a composite sensor to make things easier for the developer. It does not actually exist in the phone. Is a very neat sensor combining the accelometer sensor and the magnetic field sensor. And the OrientationSensor is currently deprecetated according to the docs (http://developer.android.com/guide/topics/sensors/sensors_position.html).

See What is the alternative to android orientation sensor? for sample usage.

it may need some fixing I have not tested it much.

Alternative of openintents for simulating sensors for Android

Calling SensorManager.connectSimulator() constitutes a network operation as shown by your stack trace. Thus, you're attempting to perform network operations on your application's main thread. Android does not allow this by default and so you have two options.

  1. Use another thread or async task. The AsyncTask syntax and usage convention can be a bit confusing, so here's an example: AsyncTask Android example.
  2. Turn off strict mode (not recommended).


Related Topics



Leave a reply



Submit