Search code examples
javaandroidandroid-sensors

How to make an Android class in java that returns device angle


I am trying to make an app that uses the devices angle. I am trying to figure out how to make a class with a function that I can call in another file that will return the devices Y angle. I have never used sensor before and I have had trouble trying to set them up. The file I will be calling the function from is running in the foreground. Does anyone know how to do this and can share some code? Thank You!


Solution

  • You can try to check Android google CodeLabs. Also I found on the internet one solution, I changed it a little bit but I am not sure if the displayed data is always correct. This sample app tracks device rotation all the time so If You want to have a function that returns rotation You have to refactor code a little.

    MainActivity.java:

    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.widget.TextView;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    public class MainActivity extends AppCompatActivity
    {
        private TextView textViewToDisplayRotation;
        private float[] acc = new float[3];
        private float[] mags = new float[3];
        private float[] values = new float[3];
        SensorManager sManager;
        private SensorEventListener mySensorEventListener = new SensorEventListener()
        {
            public void onAccuracyChanged(Sensor sensor, int accuracy)
            {
            }
    
            public void onSensorChanged(SensorEvent event)
            {
                switch (event.sensor.getType())
                {
                    case Sensor.TYPE_MAGNETIC_FIELD:
                        mags = event.values.clone();
                        break;
                    case Sensor.TYPE_ACCELEROMETER:
                        acc = event.values.clone();
                        break;
                }
    
                if (mags != null && acc != null)
                {
                    float[] gravity = new float[9];
                    float[] magnetic = new float[9];
                    SensorManager.getRotationMatrix(gravity, magnetic, acc, mags);
                    float[] outGravity = new float[9];
                    SensorManager.remapCoordinateSystem(gravity,
                                                        SensorManager.AXIS_X,
                                                        SensorManager.AXIS_Z,
                                                        outGravity);
                    SensorManager.getOrientation(outGravity, values);
    
                    float azimuth = values[0] * 57.2957795f;
                    float pitch = values[1] * 57.2957795f;
                    float roll = values[2] * 57.2957795f;
                    textViewToDisplayRotation.setText("X = " + azimuth + "\nY = " + pitch + "\nZ = " + roll);
                    mags = null;
                    acc = null;
                }
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            textViewToDisplayRotation = findViewById(R.id.textViewToDisplayRotation);
        }
    
        @Override
        protected void onResume()
        {
            super.onResume();
            sManager.registerListener(mySensorEventListener,
                                      sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                                      SensorManager.SENSOR_DELAY_NORMAL);
            sManager.registerListener(mySensorEventListener,
                                      sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                                      SensorManager.SENSOR_DELAY_NORMAL);
        }
    }
    

    main_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/textViewToDisplayRotation"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="40sp" />