Question

I need to pull the phone orientation as an integer from 1-360 without using OrientationEventListener to check onOrientationChanged()

Can this be done? I've done a good bit of Googling and haven't gotten far.

Thanks

Was it helpful?

Solution

You might need to use the accelerometer to get a precise angle. the following was taken from Vogella:

public class SensorTestActivity extends Activity implements SensorEventListener {
  private SensorManager sensorManager;
  private boolean color = false;
  private View view;
  private long lastUpdate;

  
/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    view = findViewById(R.id.textView);
    view.setBackgroundColor(Color.GREEN);

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    lastUpdate = System.currentTimeMillis();
  }

  @Override
  public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
      getAccelerometer(event);
    }

  }

  private void getAccelerometer(SensorEvent event) {
    float[] values = event.values;
    // Movement
    float x = values[0];
    float y = values[1];
    float z = values[2];

    float accelationSquareRoot = (x * x + y * y + z * z)
        / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
    long actualTime = System.currentTimeMillis();
    if (accelationSquareRoot >= 2) //
    {
      if (actualTime - lastUpdate < 200) {
        return;
      }
      lastUpdate = actualTime;
      Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT)
          .show();
      if (color) {
        view.setBackgroundColor(Color.GREEN);

      } else {
        view.setBackgroundColor(Color.RED);
      }
      color = !color;
    }
  }

  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {

  }

  @Override
  protected void onResume() {
    super.onResume();
    // register this class as a listener for the orientation and
    // accelerometer sensors
    sensorManager.registerListener(this,
        sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
        SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override
  protected void onPause() {
    // unregister listener
    super.onPause();
    sensorManager.unregisterListener(this);
  }
} 

As for what angle to take, this might help:

Azimuth (degrees of rotation around the z axis). This is the angle between magnetic north and the device's y axis. For example, if the device's y axis is aligned with magnetic north this value is 0, and if the device's y axis is pointing south this value is 180. Likewise, when the y axis is pointing east this value is 90 and when it is pointing west this value is 270.

Pitch (degrees of rotation around the x axis). This value is positive when the positive z axis rotates toward the positive y axis, and it is negative when the positive z axis rotates toward the negative y axis. The range of values is 180 degrees to -180 degrees.

Roll (degrees of rotation around the y axis). This value is positive when the positive z axis rotates toward the positive x axis, and it is negative when the positive z axis rotates toward the negative x axis. The range of values is 90 degrees to -90 degrees.

Reference:
http://www.vogella.com/tutorials/AndroidSensor/article.html
http://developer.android.com/guide/topics/sensors/sensors_position.html

OTHER TIPS

I got this from the SensorManager class. If it helps, here it is.

public static float[] getOrientation (float[] R, float[] values)

Added in API level 3 Computes the device's orientation based on the rotation matrix.

When it returns, the array values is filled with the result:

values[0]: azimuth, rotation around the Z axis. values1: pitch, rotation around the X axis. values[2]: roll, rotation around the Y axis. The reference coordinate-system used is different from the world coordinate-system defined for the rotation matrix:

X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points West). Y is tangential to the ground at the device's current location and points towards the magnetic North Pole. Z points towards the center of the Earth and is perpendicular to the ground. Inverted world coordinate-system diagram. All three angles above are in radians and positive in the counter-clockwise direction.

Parameters R rotation matrix see getRotationMatrix(float[], float[], float[], float[]). values an array of 3 floats to hold the result. Returns The array values passed as argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top