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

473 views Asked by At

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?

4

There are 4 answers

0
Adrien Neveu On BEST ANSWER

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...

0
siva On

Have you tried with SENSOR_DELAY_FASTEST?

1
Hoan Nguyen On

Since registerListener does not throw exception if you pass in a null sensor, you should check sensorfor null. It looks like your device does not have Sensor.TYPE_ROTATION_VECTOR

1
Jorgesys On

Ok if you have to use TYPE_ROTATION_VECTOR, the change from SENSOR_DELAY_GAME to SENSOR_DELAY_NORMAL

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