I have a class that implements SensorEventListener
and I would like to get
the tilt Angle
of my device using the Accelerometer
.
I looked for examples on the internet but they use
Sensor.TYPE_MAGNETIC_FIELD
.
I believe my device doesn't have this sensor because when I do the
following checkmanager.getSensorList(Sensor.TYPE_ACCELEROMETER).size()
, I get zero.
Is there a way to get the tilt Angle
by just using
Sensor.TYPE_ACCELEROMETER
values?
Measuring Tilt Angle using One Axis If you want to measure tilt in both x and y axis with a 2-axis accelerometer then you can simply use sin-1(a) where a is the output from one axis of the accelerometer.
In some applications, where the net acceleration or force on a system over time is gravity, an accelerometer can be used to measure the static angle of tilt or inclination.
To use the accelerometer (or any sensor in general) your class should implement the SensorEventListener interface, or you could do anonymous inner classes for them. To access the accelerometer you will need to get the SystemManager from the system and get a sensors list from that. sensors = myManager.
The accelerometer is an in-built comment of a smartphone to measure its acceleration. It tracks the different motion like shaking, tilting, swinging, and rotating and accordingly change the orientation of your app.
As people suggested you can use the accelerometer and magnetic sensor to do this.
Here is a blog post with a complete solution.
http://www.ahotbrew.com/how-to-detect-forward-and-backward-tilt/
Try this,
SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
final SensorEventListener mEventListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
break;
}
};
};
setListners(sensorManager, mEventListener);
SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
final CharSequence test;
test = ","+mValuesOrientation[0] +","+mValuesOrientation[1]+ ","+ mValuesOrientation[2];
You can use the accelerometer to get a tilt reading. If you set up an accelerometer you will notice it includes the force of gravity. So if you phone is face-up on a table the z-axis will register somewhere close to 9.81 (the force of gravity) and the x and y axes will be at 0. As you tilt the phone the force of gravity will be projected onto the x and/or y axis. Thus you the x and y values will tell you the tilt of the phone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With