Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Light sensor code returning limited range of values

Tags:

android

I'm trying to code a very basic lightmeter application for use with my old 35mm cameras using my Galaxy S2 as the sensor.

I should point out first of all that there is a hidden/test mode available on this phone selected by entering star hash zero star hash, on the dialller keypad then selecting 'sensor'. This makes available the light sensor which shows a range of Lux values varying between 5 and over 2000 in steps of 5 as I vary the light level.

The very simple proof of concept code I have written will only show me three values, namely 10, 100 and 1000 over the same range of lighting condtions. My code is:

public class LightMeterActivity extends Activity implements SensorEventListener {

    private SensorManager mSensorManager;
    private Sensor mLightSensor;
    private float mLux = 0.0f;

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

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

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

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {}

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
            mLux = event.values[0];
            String luxStr = String.valueOf(mLux);
            TextView tv = (TextView) findViewById(R.id.textView1);
            tv.setText(luxStr);
            Log.d("LUXTAG", "Lux value: " + event.values[0] );

        }
    }
}

Can anybody suggest why this might be?

I have seen the question Light sensor on Nexus One returns only two distinct values which didn't help at all. I can't understand how the built in test mode can see the full range and my code can't.

like image 457
NickT Avatar asked Dec 17 '11 15:12

NickT


People also ask

What is the range of a light sensor?

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.

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).

What is light sensor in mobile?

The Ambient Light Sensor is responsible for a common feature on iPhones and Android devices called “Auto Brightness.” This sensor makes it possible for your phone to detect the lighting conditions around you and adjust the screen brightness accordingly.


1 Answers

I only tested this on the Galaxy S2 but the only way I could see the true value was to get it directly the system device file:

#cat "/sys/devices/virtual/lightsensor/switch_cmd/lightsensor_file_state"

or in java:

Scanner st = new Scanner(new File("/sys/devices/virtual/lightsensor/switch_cmd/lightsensor_file_state"));
int lux = st.nextInt();
st.close();
like image 119
devilabit Avatar answered Sep 22 '22 01:09

devilabit