user1367713
user1367713

Reputation: 286

How to detect phone tilt?

How to determine the angle of the phone orientation in the one plane?

Now I make it through SensorManager:

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

...

public void onSensorChanged(SensorEvent event) {
    xy_angle = event.values[0]; 
    xz_angle = event.values[1]; 
    zy_angle = event.values[2]; 

Here I get different angles, but I need only one angle, which varies with the rotation of the phone, while driving along the blue line.

How calc or how get this angle?

https://i.sstatic.net/kCElj.jpg

How can I use SensorManager.getOrientation for tilt controls like "My Paper Plane"? - similar question here, but I don't understand how author solve his problem.

Upvotes: 3

Views: 11880

Answers (3)

Loisaida Sam Sandberg
Loisaida Sam Sandberg

Reputation: 533

I see that this post is a bit old, so maybe it's been answered by now, but Sensor.TYPE_ORIENTATION is now deprecated in favor of SensorManager.getOrientation().

I've done a full writeup of essentially what you're looking for here: http://blog.samsandberg.com/2015/05/10/tilt-for-android/

Hope it helps someone looking for the same thing!

Upvotes: 2

Yusuf X
Yusuf X

Reputation: 14633

The author of that question solved his problem by switching his parameter order:

 if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
                                    m_lastMagFields, m_lastAccels)) {

to:

if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
                                m_lastAccels, m_lastMagFields)) {

Upvotes: 0

Krishnakant Dalal
Krishnakant Dalal

Reputation: 3578

What you need is SensorManager.getOrientation().

Upvotes: 2

Related Questions