We collect sensor readings from an Android smartphone

In my first post on Habr, I would like to talk about how to get sensor data in the Android OS, and specifically, the angle of inclination of your device in all three planes. Interested please under cat.


Android OS sensors fall into three categories: motion, position, and environment. These sensors can be very different:
  • Accelerometer
  • Gyroscope
  • Light sensor
  • Magnetic field sensor
  • Accelerometer
  • Barometer
  • Sensor holding the phone to the head
  • Device temperature sensor
  • Ambient temperature sensor environment
  • Relative humidity meter
  • Etc.


Naturally, their set depends on the “configuration” of the device, but there are sensors that are present in most Android smartphones - an accelerometer and a gyroscope.

Using these sensors, we can find out the position of the phone in space, namely, the tilt angles of the device in all three planes (XY, YZ, ZX). This is what we will do!

First, create a new project and put on a simple display with three inscriptions for displaying sensor readings and corresponding signatures for them. I got something like this:

 />
        


In the main activity, the class declaration is reduced to:

public class Main extends Activity implements SensorEventListener {


The SensorEventListener class will help us track events on sensors.

You should have four required methods:

@Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) { //Изменение точности показаний датчика
  }
@Override
  protected void onResume() {
  }
@Override
  protected void onPause() {
  }
@Override
  public void onSensorChanged(SensorEvent event) { //Изменение показаний датчиков
  }


As you probably already guessed, we will soon be interested in the latter method. In the meantime, declare the variables we need:

  private SensorManager mSensorManager; 
  private Sensor mOrientation;
  private float xy_angle;
  private float xz_angle;
  private float zy_angle;
  private TextView xyView;
  private TextView xzView;
  private TextView zyView;


The first variable is the device sensor manager. It is she who will give us access to the sensor we are interested in. To do this, make the onCreate event look like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); // Получаем менеджер сенсоров
    mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); // Получаем датчик положения
    xyView = (TextView) findViewById(R.id.xyValue);  //
    xzView = (TextView) findViewById(R.id.xzValue);  // Наши текстовые поля для вывода показаний
    zyView = (TextView) findViewById(R.id.zyValue);  //
  }


Well, almost done! It remains only to get the values ​​and display in the text fields. Recall the onSensorChanged event. If you remember, the SensorEvent event parameter is passed to it. It contains the values ​​of the angle of inclination in degrees. Therefore, we make the final touch and bring the event to the form:

public void onSensorChanged(SensorEvent event) {
    xy_angle = event.values[0]; //Плоскость XY
    xz_angle = event.values[1]; //Плоскость XZ
    zy_angle = event.values[2]; //Плоскость ZY
    xyView.setText(String.valueOf(xy_angle));
    xzView.setText(String.valueOf(xz_angle));
    zyView.setText(String.valueOf(zy_angle));
}


All is ready! I think it’s clear that you cannot turn a virtual smartphone. Therefore, for testing, you will need a real device. We start, turn our smartphone and follow the numbers.

If someone needs it, then I post the apk made by example.

I hope the post was useful and understandable for you!

UPD: As it turned out, you can test in the emulator . Thanks to BlackStream for the link!

Also popular now: