How do you rate this blog

Sunday, December 29, 2013

Light Sensor

This post is about detecting ambient lighting in which the device is present. This is done using the light sensor present in the device. The light sensor measures the light in terms of LUX. So how do you get the value of Light sensor? Given below is the logical flow to obtain the light sensor value:

1) Check if there is light sensor present on the android device.
2) Register the listener for the sensor.
3) Obtain the value from the registered sensor.


 Now lets go about building the application, we need to get the layout in place first. So we will go ahead add a linear layout and 2 text fields one as a label and the other which displays the value. The layout code will look in the image below.


Once the layout is ready, we will go ahead and apply the logic which we discussed above. Lets go step by step assuming you know how to create a project,

1) Check if there is light sensor present on the android device:
    Almost all android devices will have light sensor present, but just to make sure we handle this in our code. The code given below will help you to check if the

sensor is present or not. You need implement SensorEventListener in the class, which inturn makes you write onResume, onPause, onAccuracyChanged, onSensorChanged

methods.

        //declare Sensor Manager and Sensor       
        private SensorManager SM=null;
        private Sensor curSensor=null;

        //to check Light sensor
        SM=(SensorManager) this.getSystemService(SENSOR_SERVICE);
        curSensor=SM.getDefaultSensor(Sensor.TYPE_LIGHT);
       
        //if the curSensor returns null then this means that there is no Light Sensor present in the system.
        if(curSensor==null){
            Values.setText("unable to find the sensor in the device");
        }
        else{
            Values.setText("Light sensor is present in the device");
        }

2) Register the listener for the sensor:
     If we know that the light sensor is present in the device then we will go ahead and register the listener to that sensor. This is done using the code below:
    SM.registerListener(this, curSensor, SM.SENSOR_DELAY_FASTEST);
    where SM refers to Sensor Manager, curSensor refers to Light Sensor. There are 4 differents rates at which sensor data can be obtained
    1) SENSOR_DELAY_FASTEST :This is used when you want the data as soon as possible.
    2) SENSOR_DELAY_GAME    :This one provides at a rate which is suitable for games.
    3) SENSOR_DELAY_NORMAL    :This is the default rate.
    4) SENSOR_DELAY_UI    :This provides at a rate which is suitable for user interface.

Now that we know how to register the user we can merge step 1 and 2 and write detection and registering in one shot and the code will look like:

        //declare Sensor Manager and Sensor       
        private SensorManager SM=null;
        private Sensor curSensor=null;

        //to check Light sensor
        SM=(SensorManager) this.getSystemService(SENSOR_SERVICE);
        curSensor=SM.getDefaultSensor(Sensor.TYPE_LIGHT);
       
        //if the curSensor returns null then this means that there is no Light Sensor present in the system.
        if(curSensor!=null){
            SM.registerListener(this, curSensor, SM.SENSOR_DELAY_FASTEST);

        }
        else{
            Values.setText("unable to find the sensor in the device");
        }

3) Obtain the value from the registered sensor:
        Once the sensor listener is registered we need to go ahead and extract the value from the sensor. This is done using the code below

        if(event.sensor.getType()==Sensor.TYPE_LIGHT){
           
            Values.setText("Value= "+event.values[0]+" lux");
        }

        This code needs to be added in the onSensorChanged method.
Now your application is almost ready, but we need to add few more code for the application to function properly. In the onResume you need to check for the sensor and

add register the listener again, so go ahead and add the below code to the onResume() function.

        super.onResume();
        if(curSensor!=null){
            SM.registerListener(this, curSensor, SM.SENSOR_DELAY_FASTEST);
           
        }
        else{
            Values=(TextView) findViewById(R.id.textView1);
            Values.setText("Unable to register sensor");
        }

When application is paused then go ahead and un register the listener, this you can do by adding the below code to the onPause() function
        SM.unregisterListener(this);

Once you complete this generate an apk and run it on your device. A sample output of the application is given below.