0

First I used TYPE_ORIENTATIONbut this was depreciated. Therefore I changed the code to a more recent version and used TYPE_ACCELEROMETER and TYPE_MAGNETIC_FIELD as suggested in this post.

// Initialize android device sensor capabilities
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

Then in onResume(), I used this:

// For the system's orientation sensor registered listeners
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);

and below that I used:

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // not in use
}

float[] mGravity;
float[] mGeomagnetic;

@Override
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];
        boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
        if (success) {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);
            float azimuthInRadians = orientation[0];
            float azimuthInDegrees = (float) Math.toDegrees(azimuthInRadians);
            if (azimuthInDegrees < 0.0f) {
                azimuthInDegrees += 360f;
            }
            float degree = Math.round(azimuthInDegrees);
            directionHeading.setText("Heading: " + degree + " degrees");

            // Create rotation animation
            RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);

            // How long the animation will take place
            ra.setDuration(210);

            // Set the animation after the end of the reservation status
            ra.setFillAfter(true);

            // Start the animation
            image.startAnimation(ra);
            currentDegree = -degree;
        }
    }
}

The old way was pretty precise and did not shake at all. But now, with the non-depreciated way, it shakes heavily. Why is that and how can I fix the shakiness?

Community
  • 1
  • 1
MOTIVECODEX
  • 2,624
  • 14
  • 43
  • 78
  • See my answer at http://stackoverflow.com/questions/17979238/android-getorientation-azimuth-gets-polluted-when-phone-is-tilted/17981374#17981374 – Hoan Nguyen Jan 24 '15 at 10:56
  • @HoanNguyen tanks for the reply, I'll try it out when I get home. In the comments below your answer, there are a few things, first of all, someone says: "only problem now is on quick movements it jumps alot", and you say you use "normal" and someone else is using "UI". I am using this for a compass and I want it as stable as possible. I also do not want it to jump heavily when moved fast. So what should I use, normal, UI, and does it jump if moved fast? – MOTIVECODEX Jan 24 '15 at 12:54
  • @HoanNguyen I tried it out, but mFacing is NaN. Why is this? – MOTIVECODEX Jan 24 '15 at 20:43
  • mFacing is the direction of the back camera, so if the device is laying flat there is no camera direction, thus NaN – Hoan Nguyen Jan 26 '15 at 08:07
  • My phone was 360 all around and it still did not work, but I already found a solution to the problem. I had to apply Low Pass Filter to it. – MOTIVECODEX Jan 26 '15 at 14:40

1 Answers1

0

Solution is to apply (LPF) Low Pass Filter. It's smooth and working perfectly now. I found the solution here: Low Pass Filter

MOTIVECODEX
  • 2,624
  • 14
  • 43
  • 78