Search This Blog

Showing posts with label SensorEventListener. Show all posts
Showing posts with label SensorEventListener. Show all posts

Sunday, 22 July 2012

Sample Code - Implementing the sensor in Android


Sample Code - TYPE_LIGHT sensor to get the screen brightness for android device



Android provide the android.hardware framework to implement the sensors. you could get a quick understanding of android sensor framework for our previous post.


http://creativeandroidapps.blogspot.in/2012/07/basics-of-sensors-on-android-platform.html


This post will explain the implementation of SensorEventListener for the TYPE_LIGHT sensor. The light listener is used to control the brightness of the screen.


You need to create a Android Application Project in your eclipse editor and modify the Activity class to extend with SensorEventListener.  See the following code of the sample.

package com.example.samplesensor;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity implements SensorEventListener {

    private SensorManager mSensorManager;
    private Sensor mLight;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

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

    @Override
    public void onSensorChanged(SensorEvent arg0) {
        // TODO Auto-generated method stub
        float lux = arg0.values[0];
        Toast.makeText(getApplicationContext(), "Sensor Value " + lux, 1000).show();
    }

    @Override
      protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
      }

      @Override
      protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
      }
}


Get the instance of the system service of sensor manager and get the TYPE_LIGHT sensor from the system.
You need to implement the two callback methods provided by the SessionEventListener.


The sensor need to register on the onResume() and onPause() methods of activity class.


The TYPE_LIGHT sensor only returns the one value which is brightness of the device screen. The brightness of the screen is measured by lux unit.


One lux is equal to one lumen per square metre.


If you like the example please provide your feedback.


Thanks
Creative Android Apps


Sensors framework for Android



Android Sensor Framework


Android provides a development framework to develop the applications with various sensors described in the following post.


http://creativeandroidapps.blogspot.in/2012/07/basics-of-sensors-on-android-platform.html

Android provides a android.hardware package which includes the following four classes for sensor management.

SensorManager - Sensor Manager is a class to manage the various sensors provides the methods to list the sensors available on the device, register the sensor listeners and acquire the sensors data. You can use this class to create an instance of the sensor service.


Sensor - This class provides the methods specific to a sensor. The sensor manager returns the sensor object which can use to handle the sensor functions which depends on the type of the sensor.


SensorEvent - The framework defines this class to handle the sensor events and create a object of sensor event. A sensor event object includes the raw sensor data, the type of sensor that generated the event, the accuracy of the data, and the timestamp for the event.


SensorEventListener - The sensor event listener provides the two callback methods to implement to receive the sensor events when sensor changes its value or when sensor accuracy changes.


Now you can see the next article to create quick sample for sensors on android platform.


http://creativeandroidapps.blogspot.in/2012/07/sample-code-implementing-sensor-in.html


If you like the post please provide your feedback.


Thanks
Creative Android Apps