I have a little problem with the "Compass".
I want to show on the screen in which direction I am looking with the camera.
For that I have a CameraView
and an Activity
in running in landscape mode.
Now I try to get the direction with the sensors but something went wrong. When I start the app it shows some strange data but after shaking my phone it shows the correct data?!
Maybe you can help me. This is my onSensorChanged method:
public void onSensorChanged(SensorEvent event) {
int type = event.sensor.getType();
if (type == Sensor.TYPE_ACCELEROMETER) {
mGrav = event.values.clone();
} else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
mMag = event.values.clone();
}
if (SensorManager.getRotationMatrix(mRinn, null, mGrav, mMag)) {
SensorManager.remapCoordinateSystem(mRinn, SensorManager.AXIS_X, SensorManager.AXIS_Z, mRout);
SensorManager.getOrientation(mRout, mOrient);
//Logger.d("direction?: " + Math.toDegrees(mOrient[0]));
setOrientationText(Math.toDegrees(mOrient[0]));
}
}
I am getting the data of the two sensors (magnet and accelerator) and then creating the RotationMatrix (like the documentation told me). In case it is landscape I put X -> X Axis and Y -> Z Axis for the RotationMatrix.
The screenOrientation for this Activity is landscape, too.
But I really don't understand why I am getting the correct data only after shaking the phone?!