android10

  • Increase font size
  • Default font size
  • Decrease font size

Using an Android phone's sensors

E-mail Print
( 1 Vote )
Share

Introduction

SDK Version: M3

Accessing the sensor data of a phone, is not too complicated, but since API level 3, a lot of things got deprecated. Using only the emulator, you have only a few (here is one) options, to simulate sensor data. I would recommend the use of a physical phone. Keep in mind, that not all phones have the same sensors integrated! Cheaper phones might not have a temperature sensor, or a gyroscope, but I'm pretty sure, that all Android phones have at least an accelerometer, and an orientation sensor.

Using Sensors

Here is an example, how to access acceleration and orientation sensor data (in API level 3 and above), without using any deprecated methods.

public class SensorTest extends Activity implements SensorEventListener {

SensorManager sensorManager = null;

//for accelerometer values
TextView outputX;
TextView outputY;
TextView outputZ;

//for orientation values
TextView outputX2;
TextView outputY2;
TextView outputZ2;

@Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

Start with sensorManager setup

     sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
     setContentView(R.layout.main);

//just some textviews, for data output
     outputX = (TextView) findViewById(R.id.TextView01);
     outputY = (TextView) findViewById(R.id.TextView02);
     outputZ = (TextView) findViewById(R.id.TextView03);

outputX2 = (TextView) findViewById(R.id.TextView04);
     outputY2 = (TextView) findViewById(R.id.TextView05);
     outputZ2 = (TextView) findViewById(R.id.TextView06);
 }

Now register the required listeners, it is recommended, to put this part in onResume.
What's interesting here, is the refresh speed. Just like using a toast(short,long...), there is no easy way, to set up a proper time limit. There are 4 sensor delay settings: fastest, game, normal, and ui.
Try them all out, and check which one works the best for your application. Aim for the lowest required refresh speed!

 @Override
 protected void onResume() {
     super.onResume();
     sensorManager.registerListener(this, 
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
sensorManager.SENSOR_DELAY_GAME);
     sensorManager.registerListener(this, 
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
sensorManager.SENSOR_DELAY_GAME);
 }

It is recommended, to stop using the sensor event listener, as soon as you don't need it, and it should not be left running in the background. It can drain the battery pretty fast.

 @Override
 protected void onStop() {
     super.onStop();
     sensorManager.unregisterListener(this, 
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
     sensorManager.unregisterListener(this, 
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION));
 }

The event.sensor.getType() will tell us, what kind of event just happened.

 public void onSensorChanged(SensorEvent event) {
     synchronized (this) {
         switch (event.sensor.getType()){
             case Sensor.TYPE_ACCELEROMETER:
                 outputX.setText("x:"+Float.toString(event.values[0]));
                 outputY.setText("y:"+Float.toString(event.values[1]));
                 outputZ.setText("z:"+Float.toString(event.values[2]));
                 break;
             case Sensor.TYPE_ORIENTATION:
                 outputX2.setText("x:"+Float.toString(event.values[0]));
                 outputY2.setText("y:"+Float.toString(event.values[1]));
                 outputZ2.setText("z:"+Float.toString(event.values[2]));
                 break;

}
     }
 }

There is also an onAccuracyChanged part, which we don't care about right now.

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

To sum things up, to access sensor data you have to do the following things:
    1. Check sensor availability.
    2. Register a listener to a sensorManager.
    3. Catch the needed data , from onSensorChanged.
    4. Unregister the sensorManager's listener.

using_sensor



You can read more, about those seemingly random numbers on the above screenshot, in the official google documentation of Android here.


Comments (2)
  • stalker
    avatar

    Yeah.. this is required to build some applications like games. nice one.

  • shivathebravo  - How to measure distance using accelerometer featur

    Thanks for sharing. I need to build similar app using accelerometer features that will measure the distance from one point to another and display the result.Any idea how to tackle this problem.Thank you.

Only registered users can write comments!
 

ANDROID10 --- TIP!!!

android10 tipYou can edit your position easily: just click on your picture when you're logged in, then on the top you will find a menu, go there, edit your profile, go to your position tab and drag the icon in the map. That's all. Just easy!!!
contact android10!!!