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

How to Determine if Sensor Light Changed

Added in API Level 3
Sensor.TYPE_LIGHT:
values[0]: Ambient light level in SI lux units

1. Register a SensorEventListener named mySensorEventListener.

2. In the method named onSensorChanged in the SensorEventListener, add the following code. This will test to determine if the sensor type that changed is a Light Sensor (TYPE_LIGHT), then if it is, it declares and sets a new float variable named myEventValue to the saves the event values of the Light Sensor. The next line will display a toast with the current reading of the Light Sensor, which converts the float to a string of float myEventValue.

if(event.sensor.getType()==Sensor.TYPE_LIGHT){
    float myEventValue = event.values[0];
    Toast.makeText(getBaseContext(), "Current Reading: " + String.valueOf(myEventValue), Toast.LENGTH_LONG).show();
}

Resources:


How to Register a SensorEventListener

public boolean registerListener (SensorEventListener listener, Sensor sensor, int rateUs) was added in API level 3
Registers a SensorEventListener for the given sensor.
Note: Don't use this method with a one shot trigger sensor such as TYPE_SIGNIFICANT_MOTION. Use requestTriggerSensor(TriggerEventListener, Sensor) instead.
Parameters:
listener - A SensorEventListener object.
sensor - The Sensor to register to.
rateUs - The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST or, the desired delay between events in microseconds. Specifying the delay in microseconds only works from Android 2.3 (API level 9) onwards. For earlier releases, you must use one of theSENSOR_DELAY_* constants.
Returns: true if the sensor is supported and successfully enabled.

1. Determine if Android Device has Light Sensor, using SensorManager with SensorManager named mySensorManager, a Sensor named mySensor. 

2. Add a SensorEventListener named mySensorEventListener

3. Add the line below to the onCreate method. This will register the SensorEventListener named mySensorEventListener to the Sensor named mySensor, at a rate of Normal Delay.

mySensorManager.registerListener(mySensorEventListener, mySensor,  SensorManager.SENSOR_DELAY_NORMAL);

4. Compile and run!

5. You can use the below options to change the sensor delay.

SENSOR_DEALY_NORMAL - rate (default) suitable for screen orientation changes
SENSOR_DELAY_UI - rate suitable for the user interface
SENSOR_DELAY_GAMES - rate suitable for games
SENSOR_DELAY_FASTEST - get sensor data as fast as possible

Resources:http://developer.android.com/reference/android/hardware/SensorManager.html#registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int)

How to Add a SensorEventListener

SensorEventListener was added in API Level 3
Used for receiving notifications from the SensorManager when sensor values have changed.

1. In the MainActivity.java file, add the lines below to the import section.

import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;

2. Add the below code to the onCreate method. This will add a SensorEventListener named mySensorEventListenerToasts were added for testing.

SensorEventListener mySensorEventListener = new SensorEventListener(){

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
Toast.makeText(getBaseContext(), "onAccuracyChanged", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
Toast.makeText(getBaseContext(), "onSensorChanged", Toast.LENGTH_LONG).show();
    }
};

3. Compile and run!

Resources:
http://developer.android.com/reference/android/hardware/SensorEventListener.html
http://developer.android.com/reference/android/hardware/SensorEvent.html