Search code examples
androidandroid-sensors

Are different types of sensor data got by onSensorChanged of the same quantity?


I mean, for the code below, will _aBuffer.size(), _laBuffer.size(), .... all be the same?

I've tried the same code on different devices, one is 'yes', the other is 'no', but i don't know if there is some issues with the second device's hardware, because it has some strange behavior

public void onSensorChanged(SensorEvent event) {
    int eType = event.sensor.getType();
    float[] values = event.values.clone();

    if (eType == Sensor.TYPE_ACCELEROMETER) {
        _aBuffer.offer(values);
        System.out.println("onSensorChanged values: "+values[0]+","+values[1]+","+values[2]);
    } else if (eType == Sensor.TYPE_LINEAR_ACCELERATION) {
        _laBuffer.offer(values);
    } else if (eType == Sensor.TYPE_GRAVITY) {
        _gBuffer.offer(values);
    } else if (eType == Sensor.TYPE_MAGNETIC_FIELD) {
        _mBuffer.offer(values);
    } else if (eType == Sensor.TYPE_ORIENTATION) {
        // do nothing
    } else if (eType == Sensor.TYPE_GYROSCOPE) {
        _gyroBuffer.offer(values);
    } else if (eType == Sensor.TYPE_ROTATION_VECTOR) {
        _rotBuffer.offer(values);
    }
}

Solution

  • Since the number of sample depend on the value you gave in

    public boolean registerListener (SensorEventListener listener, Sensor sensor, int rate)
    

    The size of the sample must be same theoretically.But in practise it not always true.

    http://developer.android.com/reference/android/hardware/SensorManager.html#registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int)

    says

    The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST. or, the desired delay between events in microsecond.