I need to get device orientation. As I know usually used TYPE_ACCELEROMETER
and TYPE_MAGNETIC_FIELD
sensors. My problem is that SensorManager.getDefaultSensor
returns me null
for geomagnetic sensor. It returns null
for TYPE_ORIENTATION
sensor too.
manager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor sensorAcc = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), //normal object
sensorMagn = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); //null
orientationListener = new OrientationSensorListener();
manager.registerListener(orientationListener, sensorAcc, 10);
manager.registerListener(orientationListener, sensorMagn, 10);
I need another ways to get device orientation.
Orientation can be decomposed in three Euler angle : Pitch, Roll and Azimuth.
With only accelerometer datas, you cannot compute your Azimuth, neither the sign of your pitch.
You can try something as this to know something about your pitch and roll :
You have to add some permissions to the manifest. The docs states:
I know it sounds counter intuitive, but apparently the permissions you need are:
(or a sub-set of those).
See here: manifest.xml when using sensors
i did something like :
if you want to understand about my if statements see this image
for my use i lock the screen in a portrait mode , and a use 2 images to show the angle on screen , this is my screenshot
i'm still have to make it little better , just not enough time for it.
i hope this help , if you need a full code let me know.
https://developer.android.com/guide/topics/sensors/sensors_position.html
https://developer.android.com/guide/topics/sensors/sensors_motion.html
https://developer.android.com/guide/topics/sensors/sensors_overview.html
Which sensor for rotating android phone?
You can try the GEOMAGNETIC_ROTATION_VECTOR like this:
And compute the sensor info with this:
Extracted from android docs: https://developer.android.com/guide/topics/sensors/sensors_position.html
add the proper permissions in the manifest.
Hope this helps.
For anyone who is still confused by this problem, say: you want to get the orientation of your phone (the azimuth, pitch and roll), but sometimes the magnetic field is unstable, so that the orientation you get is also unstable. The answers above may help you get the pitch and roll angle, but you still cannot get the azimuth angle. They said it is impossible. Then you become desperate. So, what should you do to solve this problem?
If you only care about the orientation and you don't care about where north is, here is my suggestion, try this sensor, it works awesome in my case: