sgallego
sgallego

Reputation: 318

Android accelerometer and Android versions

since Android 3.0 the android.hardware.SensorManager.getRotationMatrix method result has to be remaped to work as on previous Android versions.

I use the next code:

android.hardware.SensorManager.getRotationMatrix(newm, null, mAccelerometerValues, mMagneticValues);
if( sdk >= 11){ //Honeycomb
            android.hardware.SensorManager.remapCoordinateSystem(newm, 
                                                            android.hardware.SensorManager.AXIS_MINUS_Y, 
                                                            android.hardware.SensorManager.AXIS_X, 
                                                            newm);
        } 

Now, on Android Ice Cream Sandwich (4.0) the SensorManager acts as on Android 2.X. My question is:

The accelerometer is only rotated on Tablets? The accelerometer is only rotated on Android 3.X? If there any example of how to read the accelerometer in any Android version?

Upvotes: 1

Views: 1753

Answers (1)

sgallego
sgallego

Reputation: 318

after some investigation I have found a solution:

Tablets have LANDSCAPE as default orientation, so screen rotation is 0 or 180 when the orientation is LANDSCAPE, and smartphones have PORTRAIT. I use the next code to difference between tablets and smartphones:

Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int orientation = getScreenOrientation(display);
    int rotation = display.getRotation();

    remapCoordinates  = (orientation == Configuration.ORIENTATION_LANDSCAPE && rotation == Surface.ROTATION_0) ||
            (orientation == Configuration.ORIENTATION_LANDSCAPE && rotation == Surface.ROTATION_180) ||
            (orientation == Configuration.ORIENTATION_PORTRAIT && rotation == Surface.ROTATION_90) ||
            (orientation == Configuration.ORIENTATION_PORTRAIT && rotation == Surface.ROTATION_270);

where getScreenOrientation method is:

public static int getScreenOrientation(Display display){


    int orientation;

    if(display.getWidth()==display.getHeight()){
                orientation = Configuration.ORIENTATION_SQUARE;
    }else{ //if widht is less than height than it is portrait
                if(display.getWidth() < display.getHeight()){
                orientation = Configuration.ORIENTATION_PORTRAIT;
                }else{ // if it is not any of the above it will defineitly be landscape
                orientation = Configuration.ORIENTATION_LANDSCAPE;
                }
    }   
    return orientation;
}

Now, I remap the coordinates as before only when the default orientation is LANDSCAPE:

if( remapCoordinates){ 
            android.hardware.SensorManager.remapCoordinateSystem(newm, 
                                                            android.hardware.SensorManager.AXIS_MINUS_Y, 
                                                            android.hardware.SensorManager.AXIS_X, 
                                                            newm);
} 

Best regards.

Upvotes: 3

Related Questions