7

I am trying to get the phone angles by using TYPE_ACCELEROMETER sensor. My goal is to get the angle values only after the phone is tilted. It works, but the problem is when I put my phone facing up on the table, it still says isLandscape = true;

private boolean isLandscape;

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(
                                          Sensor.TYPE_ACCELEROMETER),1000000);
private final SensorEventListener mSensorListener = new SensorEventListener() { 
    @Override
    public void onSensorChanged(SensorEvent mSensorEvent) {  
        float X_Axis = mSensorEvent.values[0];
        float Y_Axis = mSensorEvent.values[1];
        double angle = Math.atan2(X_Axis, Y_Axis)/(Math.PI/180);

        if(!isLandscape) {                  
            if(angle > 80) {
                Orientation = 90;
                isLandscape = true;
            }
        }
        else 
        {

            if(Math.abs(angle) < 10) {
                Orientation = 0;  //portrait
                isLandscape = false;
            }
        }
    }

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

What's the best way to get the phone angles only after the phone is tilted? I am sorry for bad English,

Thank you.

IYM-14
  • 260
  • 2
  • 11
  • I don't clearly understand your goal. Maybe consider using the accelerometer to detect when the phone position is changing, and the gravity sensor to determine its orientation after changes have stopped. – Bob Snyder Jul 11 '15 at 21:17
  • I'm concerning about the battery when using 2 different sensors. How bad is the battery consumption for using 2 different sensors compared to 1 sensor? – IYM-14 Jul 12 '15 at 04:44
  • I don't know. I assumed that you were doing this processing in an activity and would only have the sensors registered when the activity is visible. – Bob Snyder Jul 12 '15 at 05:44

2 Answers2

7

I don't know if i understand your question ,but i think you want your app to calculate the angel of tilt only if the phone is in portrait ,first you need to take the value of mSensorEvent.values[0] and in this case if the phone in stand state in will return 0 , tilt to right will be negative values from 1 to 9 ,and the left positive .

then you have to do all this just in case of mSensorEvent.values[1] values between 9 and 7 for example (9 is perfect stand) . to ensure the the device in portrait position .

and if you need the degree angle values you can multiply the float value of mSensorEvent.values by 10.

i hope this help you

UPDATE***

you can try this :

    private boolean isLandscape;

    @Override 
    public void onSensorChanged(SensorEvent mSensorEvent) {   
        float X_Axis = mSensorEvent.values[0]; 
        float Y_Axis = mSensorEvent.values[1]; 

        if((X_Axis <= 6 && X_Axis >= -6) && Y_Axis > 5){
        isLandscape = false; 
        }
        else if(X_Axis >= 6 || X_Axis <= -6){
        isLandscape = true;
        }

    } 
Khalil Tam
  • 441
  • 1
  • 3
  • 13
  • All I want is to get the tilt angles (portrait or landscape) without having to use the screen rotation feature. I'm trying to create a camera app, so I have icons that I want them to rotate and translate when the phone is tilted to landscape/portrait. So I figured I can do this with sensors. – IYM-14 Jul 12 '15 at 04:01
  • The code I have right now is not working correctly because it says isLandscape = true; even though I put my phone flat vertically on the table without tilting it. – IYM-14 Jul 12 '15 at 04:23
  • @IYM-14 i had updated the code ,so you can try it ,and i hope this is what you are looking for. – Khalil Tam Jul 12 '15 at 11:42
  • I just tested it and this is what I wanted, Thank you! – IYM-14 Jul 13 '15 at 23:49
  • I was looking at this code and I am wondering something. This works fine with the person just tilting the phone on either sides. But this will also "work" if the person moves the phone quickly horizontal with enough force to achive a X value over 6 or under -6, seeing as accelerometer takes linear force into account. How do you fix that ? – Bojje Sep 03 '15 at 13:44
2

Instead of register for ACCELEROMETER sensor, just have a class member of type OrientationEventListener. When the device is flat you should get OrientationEventListener.ORIENTATION_UNKNOWN.

See Android landscape right to landscape left event for how create a OrientationEventListener class member.

Or you can leave your code as is but also checking the device inclination. If the device is flat then set isLandscape flag to whatever appropriate for your case. For how to calculate the device inclination see How to measure the tilt of the phone in XY plane using accelerometer in Android

Community
  • 1
  • 1
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • Thanks for the comment Hoan, but this is not what I wanted as the my activity will turn off the screen orientation feature. That means the orientation is going to be always unknown regardless my phone is on portrait or landscape angle. The reason I don't like screen orientation feature is that it pauses my app a bit every time it changes the orientation. – IYM-14 Jul 12 '15 at 21:52
  • OK, I modify my answer. – Hoan Nguyen Jul 12 '15 at 23:33
  • Thanks for the inclination method link Hoan, Thank you! – IYM-14 Jul 13 '15 at 23:50
  • heyy... i need this feature on my steer controller inside android app... @HoanNguyen... thanks! – gumuruh Jan 14 '23 at 11:15