7

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.

Community
  • 1
  • 1
NickT
  • 23,844
  • 11
  • 78
  • 121
  • Aren't 50 rep a bit less for such a kinda complicated question? :p – poitroae Jan 07 '12 at 01:16
  • @Michael: You wasted your time then didn't you? If you think that sort of attitude will make you popular on Stack Overflow, you are badly mistaken. – NickT Jan 08 '12 at 11:00

2 Answers2

4

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();
devilabit
  • 56
  • 2
  • This actually works! Thank you. I thought it would need root privileges, but it doesn't. – NickT Feb 20 '12 at 16:01
0

Maybe there is another light sensor that is used by default by your system. I wrote an application that simply outputs the information about the sensors:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

    mLightSensorList = mSensorManager.getSensorList(Sensor.TYPE_LIGHT);
    tvSensorList = (TextView) findViewById(R.id.tvSensorList);
    StringBuilder sensorInfo = new StringBuilder("");
    if (mLightSensorList != null) {
        Log.d("Yury", mLightSensorList.toString());
        for (Sensor s : mLightSensorList) {
            sensorInfo.append("Sensor name: ");
            sensorInfo.append(s.getName());
            sensorInfo.append(" vendor: ");
            sensorInfo.append(s.getVendor());
            sensorInfo.append(" ver.: ");
            sensorInfo.append(s.getVersion());
            sensorInfo.append(" resolution: ");
            sensorInfo.append(s.getResolution());
            sensorInfo.append(" maxRange: ");
            sensorInfo.append(s.getMaximumRange());
            sensorInfo.append("\n");
        }
        tvSensorList.setText(sensorInfo.toString());
    }



    mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
}

You can test your device. Considering several values on Nexus One. I tested your application on Nexus One and at first I received only two values 10 and 225. But then I put the phone under the lamp and received a lot of values. But the set of the values is limited while the resolution of the sensor is 1f. I do not know why this happens.

Yury
  • 20,618
  • 7
  • 58
  • 86
  • The phone has only one light sensor, which is a CM3663 Light Sensor manufactured by Capella Microsystems, Inc. This is only to be expected, as I can think of no reason why a device would have two or more such sensors. – NickT Jan 07 '12 at 18:49
  • In the documentation I found that there can be several sensors in the device. getDefaultSensor can return just first sensor, or can make a fake sensor that will take average of several real sensors. However, try to put your phone under the lamp, and also check maxRange of your sensor. – Yury Jan 07 '12 at 18:54
  • I had already got a list of sensors of type Sensor.TYPE_LIGHT. There was one entry in it as described. Its maximum range is 3000.0. Of course I put the phone under a bright light. It read 1000.0. – NickT Jan 07 '12 at 19:00