Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Light Sensor Value

So I'm looking for a way to get the current value of the Light Sensor (in Lux obviously) on a button press. Here is the code I'm using to implement control of the light sensor

public class SensorActivity extends Activity implements SensorEventListener 
{
 private final SensorManager mSensorManager;
 private final Sensor mLight;
 int minLux = 0;
 int currentLux = 0;
 int maxLux;

 public SensorActivity() 
 {
     mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
     mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
 }

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

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

 public void onAccuracyChanged(Sensor sensor, int accuracy) 
 {
     if(sensor.getType() == Sensor.TYPE_LIGHT)
     {

     }
 }

 public void onSensorChanged(SensorEvent event) 
 {
     if( event.sensor.getType() == Sensor.TYPE_LIGHT)
     {

     }
 }
 }

I also have this code making the button to call a function

<Button android:layout_width="122px" android:id="@+id/widget35" android:text="Start" android:layout_height="wrap_content" android:layout_x="112dp" android:layout_y="249dp"  android:onClick="onButtonDown"></Button>

How would I get the current lux value while the app is running in the background, and how would I get and store the current lux value on button press into int maxLux;?

like image 985
Cistoran Avatar asked Jun 22 '11 01:06

Cistoran


People also ask

What is light sensor value?

The Light Sensor is an analog sensor, and it returns values in the range of 0 to 4095. Higher values indicate that the sensor is in a darker area, and lower values indicate lighter areas. Note: Remember that it is important to calibrate your Light Sensor by calculating a threshold value.

What is the maximum value of the light sensor?

Light sensor value range: 0~1000, exposed under sunshine (> 500), evening (0 ~ 100), lighting (100 to 500).

How do you measure a light sensor?

To properly measure the luminous flux as perceived by a surface, called illuminance, we use a unit called lux, which is equal to one lumen per square meter. At the same distance from a light source, a 1 square meter sheet is subjected to the same illuminance as a 10 square meter sheet.

What is the output of a light sensor?

A Light Sensor generates an output signal indicating the intensity of light by measuring the radiant energy that exists in a very narrow range of frequencies basically called “light”, and which ranges in frequency from “Infra-red” to “Visible” up to “Ultraviolet” light spectrum.


1 Answers

As noted here, it isn't possible to directly get the current sensor value and, while some more recent versions of Android will generate an onSensorChanged event as soon as the listener is registered (allowing you to get a baseline), earlier versions don't. So the only thing you can really do is hope that the user is either running a more recent version of Android or that the value changes and then retrieve the current lux value from event.values[0] inside onSensorChanged (e.g.) :

 public void onSensorChanged(SensorEvent event) 
 {
     if( event.sensor.getType() == Sensor.TYPE_LIGHT)
     {
        currentLux = event.values[0];
        if (currentLux > maxLux)
          maxLux = currentLux;
     }
 }

(After changing maxLux and currentLux to floats). This then leaves the issue of retrieving the current value based on a button press, which can be done by looking at the value of currentLux at the time the button is pressed.

like image 86
Mark Avatar answered Oct 03 '22 08:10

Mark