How do you rate this blog

Thursday, December 15, 2011

Beautiful world of Android Sensors – Accelerometer


Well finally decided to get hands on sensors in android. In my view it’s the most exciting part of android. It will surely light up new ideas.
The sensors are used to detect changes in the phones surrounding environment like change in position, temperature, light etc. Android provides various sensors like – Accelerometer, Gyroscope, Light, Magnetic Field, Pressure, Temperature, Orientation and Proximity. I am sure looking at the list you will be excited to use these features. There is a catch however, not all sensors are available on all the phones. Only in high end android phones there is a high possibility of all the sensor hardware being included. In this post I will use the Accelerometer since it will be available in almost all android phones.
                I will use the accelerometer and try to find out if the phone has been shaked. If the phone has been shaked beyond certain threshold it should display it in the textbox with the value.
                Create a new project and choose Android 2.3.3. Go to the layout under the folder ‘res’ and open the file main.xml and remove the default contents. Add a Large text which will display a text if the phone has been shaked along with the value.
Now open the main java file, and declare a variable which is of the type SensorManager, also declare variable which will hold the acceleration across x, y and z axis as shown below.
SensorManager sensorMgr;
float xaxis,yaxis,zaxis;
float accel;

Next you need to create a SensorEventListener, if you are referring to old android ebooks it might have used SensorListener which is deprecated in android 2.3.3 onwards. When you create SensorEventListener you need to implement 2 functions one is onSensorChanged which is used to detect change in the sensors values and the other is called when accuracy of sensor has changed, this is received by the system and you cannot set anything like high accuracy etc.
Now inside the onSensorChanged function get the values of acceleration across x, y and z axis from the sensorevent as shown below.
xaxis=event.values[0];                                 
yaxis=event.values[1];                                 
zaxis=event.values[2];
Now that we have values lets calculate the acceleration as √x^2+y^2+Z^2.
accel=(float) Math.sqrt((double) ((xaxis*xaxis)+(yaxis*yaxis)+(yaxis*yaxis)));
Since we want the value to be displayed only if it is shaked beyond a point we will use if condition for this. In this case we will make 2 as the threshold value.
if(accel >=2){
TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText("The Device was shaked and the acceleration was "+accel);
                                                }

So the SensorEventListener will look like as shown below
private final SensorEventListener listner=new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
xaxis=event.values[0];
yaxis=event.values[1];
zaxis=event.values[2];
accel=(float) Math.sqrt((double) ((xaxis*xaxis)+(yaxis*yaxis)+(yaxis*yaxis)));
if(accel >=2){
TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText("The Device was shaked and the acceleration was "+accel);
}                                                                                             
                                }                                                             
                };
Now create a sensor manager and register the accelerometer sensor to it in the OnCreate function as shown below.
sensorMgr=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorMgr.registerListener(listner, sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
In registerListener function, you will have to set the SensorEventListener which you have created above. Then set the sensorManager to Accelerometer.  After this you will have to set the rate at which the sensor data are delivered here we can use for game.
Well you might think that we are done at this part, but its not. The sensors eat up lot of resource in the phone so it’s a good practice that we unregister the listener when it’s paused or stopped and started on resumption as shown below.
 @Override
    protected void onResume(){
super.onResume();
sensorMgr.registerListener(listner, sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
    }

@Override
public void onStop(){
sensorMgr.unregisterListener(listner);
super.onStop();
    }
   
@Override
protected void onPause(){
sensorMgr.unregisterListener(listner);
super.onPause();  }
The code will look the snapshot given below.



Now run the app on your phone and shake it, once you shuffle it the text box should change as shown in the screen shot given below.

The above screen shot is taken from a mobile.

Well this is just a simple application and we have just skimmed the surface. I will post more about various sensors in the future : ).

6 comments:

  1. awesome maga...you would reach places!! :)

    ReplyDelete
  2. lol dude its a simple one! you tell me that when I create an app :)

    ReplyDelete
  3. how did u take the accelerator output's screenshot.. Did u develop a custom android phone as well..!

    ReplyDelete
  4. see below the screenshot! Stop mocking!

    ReplyDelete