Light sensor code returning a limited range of values

I am trying to encode a very basic indicator light application for use with my old 35mm cameras using Galaxy S2 as a sensor.

First of all, I have to indicate that this phone has a hidden / test mode, selected by entering a star hash symbol with a zero star , on the dialller keyboard, then selecting the “sensor”. This makes the light sensor available, which displays a range of Lux values ​​varying from 5 to 2000 in increments of 5 when the light level changes.

The simplest proof of concept code that I wrote will show only three values, namely 10, 100 and 1000 in the same light range. 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 anyone guess why this might be?

I saw the question The light sensor on the Nexus One returns only two different values, which did not help at all. I can’t understand how the built-in test mode can see the full range, but my code cannot.

+7
source share
2 answers

I tested this only on the Galaxy S2, but the only way to see the true value is to get it directly from 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(); 
+4
source

Perhaps there is another light sensor that is used by your system by default. I wrote an application that simply displays information about 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. Given a few meanings on the Nexus One. I tested your application on the Nexus One, and at first I got only two values ​​of 10 and 225. But then I put the phone under the lamp and got a lot of values. But the set of values ​​is limited, and the resolution of the sensor is 1f. I do not know why this is happening.

0
source

All Articles