📜 ⬆️ ⬇️

We collect sensor readings from an Android smartphone

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


Android OS sensors fall into three categories: movement, position, and environment. These sensors can be very different:


Naturally, their set depends on the “configuration” of the device, but there are sensors that are present in most Android smartphones - the accelerometer and gyroscope.
')
Through these sensors we can find out the position of the phone in space, namely the angles of inclination of the device in all three planes (XY, YZ, ZX). This we will do!

To begin with, we will create a new project and distribute a simple display with three inscriptions to display the sensor readings and corresponding signatures to them. I did something like this:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text=" XY" /> <TextView android:id="@+id/xyValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text="0" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView3" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text=" XZ" /> /> <TextView android:id="@+id/xzValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text="0" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView5" android:layout_width="60dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text=" ZY" /> <TextView android:id="@+id/zyValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="25dp" android:text="0" /> </LinearLayout> </LinearLayout> 


In the main activation class declaration we bring to the form:

 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 may have guessed, we will soon be interested in the latter method. For now, let's 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's sensor manager. She will give us access to the sensor we are interested in. For this, the onCreate event will 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 finished! It remains to get the values ​​and display them in the text fields. Recall the onSensorChanged event. If you remember, the parameter SensorEvent event is passed to it. It also contains the values ​​of the angles of inclination in degrees. Therefore, we make the final stroke 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 is clear that you can’t turn a virtual smartphone. Therefore, for testing you will need a real machine. We launch, turn our smartphone and watch the numbers.

If someone needs it, then lay out apk made by example.

I hope the post was useful and understandable to you!

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

Source: https://habr.com/ru/post/137678/


All Articles