Android rotation vector sensor error

I do not ask a question. I am going to post this in case anyone else comes across this issue. If you follow the Android API manual for the rotation sensor, you will encounter an error. In particular: java.lang.IllegalArgumentException, because some devices return an array with five values. You can probably fix it now that you know it, but still here's how to do it:

    private int rotateVectLength;
    private float[] jRotateVectValues = null;
    public void onSensorChanged(SensorEvent event) {
        // we received a sensor event. it is a good practice to check
        // that we received the proper event
        if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
            // convert the rotation-vector to a 4x4 matrix. the matrix
            // is interpreted by Open GL as the inverse of the
            // rotation-vector, which is what we want.
            if(jRotateVectValues == null) {
                rotateVectLength = event.values.length;
                jRotateVectValues = new float[rotateVectLength];
            }
            for(int i = 0; i < rotateVectLength; i++) {
                jRotateVectValues[i] = event.values[i];
            }
            SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
        }
    }

Hope this helps someone. Hurrah!

The documentation indicates:

values[0]: x*sin(θ/2) 
values[1]: y*sin(θ/2) 
values[2]: z*sin(θ/2) 
values[3]: cos(θ/2) 
values[4]: estimated heading Accuracy (in radians) (-1 if unavailable)

values ​​[3], initially optional, will always be present at SDK Level 18 onwards. Values ​​[4] is the new value added to SDK Level 18.

+4
source share

Source: https://habr.com/ru/post/1523738/


All Articles