Search code examples
androidnvidiaandroid-sensors

onSensorChanged() not fired on Android (nVidia Shield Tablet)


I'm trying to create an augmented reality app for the nVidia shield. I tried my app on another Android device and it works. Unfortunately, on the shield, the onSensorChanged event is not fired.

Here's my code:

        _sensorEventListener = new SensorEventListener()
        {
            public void onSensorChanged(SensorEvent event) {
                AndroidAttitude.this.processSensorEvent(event);
            }

            public void onAccuracyChanged(Sensor sensor, int accuracy)
            {
            }
        };

        Thread sensorThread = new Thread(new Runnable()
        {
            public void run() {
                Looper.prepare();

                _sensorLooper = Looper.myLooper();
                Handler handler = new Handler();

                _sensorManager = (SensorManager)_context.getSystemService("sensor");

                Sensor sensor = _sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
                _sensorManager.registerListener(_sensorEventListener, sensor, 0, _sensorManager.SENSOR_DELAY_GAME);

                Looper.loop();
            }
        });

        sensorThread.start();

And this is where I transform my values into a rotation matrix:

private synchronized void processSensorEvent(SensorEvent event)
{
    float[] rotationVector = { -event.values[1], event.values[0], event.values[2] };

    float[] quaternion = new float[4];
    float[] rotationMatrix = new float[16];

    _sensorManager.getQuaternionFromVector(quaternion, rotationVector);
    _sensorManager.getRotationMatrixFromVector(rotationMatrix, rotationVector);

}

Any idea why it wouldn't work?


Solution

  • So.. I just found out where the problem was coming from. The magnetic cover of my tablet was messing with the magnetometer, resulting in the Game Rotation Vector sensor and the magnetic field sensor not sending data. I can't believe I spent hours scratching my head to fix this problem...