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.
To get the angle of rotation when the device is not flat, implements
OrientationEventListener
.onOrientationChanged
will give the device orientation. See https://developer.android.com/reference/android/view/OrientationEventListener.html for detail.