Get where is facing the cellphone in android
-
29-04-2021 - |
Question
I'm trying to understand how can I know if the device's screen is facing the user or, for example, flat on a table with the screen looking up, or facing down, etc.
I'm not using the orientation sensor as it is deprecated I'm using the rotation matrix, I know pitch and roll values should be good the get that information, but before I get those values I need to remapcoordinatesystem and to know which Axle is the X and which one is the Y I need to know what is the screen facing.
For example: If the screen is facing the user I can use this mapping table
ROTATION_0 X Y
ROTATION_90 -Y X
ROTATION_180 -X -Y
ROTATION_270 Y -X
If the screen is facing the sky maybe I'll need to swap Y axle with Z axle, but I don't know how I can find out where is the screen facing.
Any ideas?
I'm also using this method I've find in the web (http://code.google.com/p/the-schwartz-unsheathed/source/browse/trunk/src/com/android/app/schwarz/PhoneOrientation.java?r=12)
public static int getOrientation(float roll, float pitch) {
int orientation = ORIENTATION_INVALID;
if (Math.abs(roll) == 0.0 && Math.abs(pitch) == 0.0){
return ORIENTATION_INVALID;
}
if(roll >= -90 && roll <= -(90-mTolerance)){
orientation = ORIENTATION_FACE_LEFT;
}
else if(roll <= 90 && roll >= (90-mTolerance)){
orientation = ORIENTATION_FACE_RIGHT;
}
if(pitch >= (90-mTolerance) && pitch <= (90+mTolerance)) {
if(orientation != ORIENTATION_INVALID){
orientation = ORIENTATION_INVALID;
}
else {
orientation = ORIENTATION_FACE_FORWARD;
}
} else if(pitch <= -(90-mTolerance) && pitch >= -(90+mTolerance)) {
if(orientation != ORIENTATION_INVALID) {
orientation = ORIENTATION_INVALID;
}
else {
orientation = ORIENTATION_FACE_BACKWARD;
}
}
if((roll >= -mTolerance && roll <= mTolerance)) {
if((pitch >= -mTolerance && pitch <= mTolerance)) {
if(orientation != ORIENTATION_INVALID) {
orientation = ORIENTATION_INVALID;
}
else {
orientation = ORIENTATION_FACE_UP;
}
} else if((pitch <= -(180-mTolerance) && pitch >= -180) || (pitch >= (180-mTolerance) && pitch <= 180)) {
if(orientation != ORIENTATION_INVALID) {
orientation = ORIENTATION_INVALID;
}
else {
orientation = ORIENTATION_FACE_DOWN;
}
}
}
return orientation;
}
Anyone has one better?
No correct solution
OTHER TIPS
You can use Accelerometer, because it gives the x-y axis of the device, so you can get where the device is facing.