简体   繁体   中英

Light sensor code returning limited range of values

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.

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM