Soheil
Soheil

Reputation: 1706

android-why I can't get device orientation?

I want to make an app which shows device orientation over 3 axises while the user clicks on a button, but the result always is 0.0|0.0|0.0 (no rotation) , why?

 package com.example.newp;

 import android.app.Activity;
 import android.hardware.SensorManager;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Toast;

 public class FullscreenActivity extends Activity {

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_fullscreen);
}

public void onButtonClick(View view) {
    float[] mGravs = new float[3];
    float[] mGeoMags = new float[3];
    float[] mRotationM = new float[9];
    float[] mInclinationM = new float[9];
    float[] mOrientation = new float[3];
    SensorManager.getRotationMatrix(mRotationM, mInclinationM, mGravs,
            mGeoMags);
    SensorManager.getOrientation(mRotationM, mOrientation);
    Toast.makeText(
            this,
            String.valueOf(mOrientation[0]) + "|"
                    + String.valueOf(mOrientation[1]) + "|"
                    + String.valueOf(mOrientation[2]), Toast.LENGTH_LONG)
            .show();
}
 }

Upvotes: 0

Views: 102

Answers (1)

Vikram
Vikram

Reputation: 51581

You are feeding in the wrong values. mGravs is [0.0, 0.0, 0.0] and so is mGeoMags. These values should come from sensor TYPE_ACCELEROMETER and TYPE_MAGNETIC_FIELD respectively. You can use the values returned by SensorEvent of these sensors.

Upvotes: 1

Related Questions