May 11, 2011

How to use the Light sensor on Android

If your android device has a Light Sensor you can use it in your applications. It provide the value in lux units as described here. More about lux find here.

main.xml





main.java
package com.blog.lightsensor;

import java.util.Date;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class main extends Activity implements SensorEventListener {
 private  SensorManager sensorManager = null;
 private  Sensor currentSensor = null; 
 
 @Override
 public void onResume(){
  super.onResume();
  if(currentSensor != null)sensorManager.registerListener(this, currentSensor, SensorManager.SENSOR_DELAY_FASTEST);
 }
 
 @Override
 public void onPause(){
  super.onPause();
  sensorManager.unregisterListener(this);
 } 
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
  currentSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT );
  if(currentSensor != null){
   sensorManager.registerListener(this, currentSensor, SensorManager.SENSOR_DELAY_FASTEST);
  }else{
   ((TextView) findViewById(R.id.textView1)).setText("Can't initialize the LIGHT sensor.");   
  } 
    }

 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
  // TODO Auto-generated method stub
 }

 @Override
 public void onSensorChanged(SensorEvent event) {
  // TODO Auto-generated method stub
  
  if (event.sensor.getType() == Sensor.TYPE_LIGHT){   
   TextView tv = (TextView) findViewById(R.id.textView1);
   tv.setText( tv.getText()+ "value: " +event.values[0] + " lux , time: " + new Date() + "\n");   
  }
 }
}

List of available sensors on Android

You can obtain the list of available sensors on your device by calling sensonManager.getSensorList() and providing the type Sensor.TYPE_ALL as a parameter. Find below a full example.




main.xml


    

main.java
package com.blog.sensorslist;

import java.util.List;

import android.app.ListActivity;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class main extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);
        
        // create new SensorManager
        SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        // get all sensors of all types
        List sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
        // list of all sensor types
        String[] sensorTypes = new String[]{"","ACCELEROMETER", "MAGNETIC_FIELD", 
          "ORIENTATION", "GYROSCOPE", "LIGHT", "PRESSURE","TEMPERATURE", 
          "PROXIMITY", "GRAVITY", "LINEAR_ACCELERATION", "ROTATION_VECTOR"};
        
        // array for ListAdapter
        String[] infoList = new String[sensorList.size()];
        
        for (int i =0; i < sensorList.size(); i++){
         Sensor currentSensor = sensorList.get(i);
         infoList[i] = "Sensor Name: "+ currentSensor.getName() +"\n" +
             "Vendor: "+ currentSensor.getVendor() + "\n" +
             "Version: "+ currentSensor.getVersion()+ "\n"+
             "Type: "+ currentSensor.getType() + " - "+sensorTypes[currentSensor.getType()]+ "\n"+
             "Maximum Range: "+ currentSensor.getMaximumRange()+ "\n"+
             "Power (mA):"+ currentSensor.getPower()+ "\n"+
             "Resolution: "+ currentSensor.getResolution();              
        }
        // setting the ListAddapter
        setListAdapter(new ArrayAdapter(this, R.layout.main, infoList));
    }
}