android - Reading LightSensor from LG P990 -
i trying develop light sensor app lg p990 detect laser (don't ask why).
the code looks this:
package soma.de.light; import android.app.activity; import android.content.context; import android.hardware.sensor; import android.hardware.sensorevent; import android.hardware.sensoreventlistener; import android.hardware.sensormanager; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; public class lightsensoractivity extends activity { sensormanager mysensormanager; sensor mylightsensor; textview textlightsensordata; textview textlightsensor; button start; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); textlightsensor = (textview)findviewbyid(r.id.tvlight); textlightsensordata = (textview)findviewbyid(r.id.tvacc); mysensormanager = (sensormanager)getsystemservice(context.sensor_service); mylightsensor = mysensormanager.getdefaultsensor(sensor.type_light); start = (button)findviewbyid(r.id.btstart); start.setonclicklistener(btlistener); if (mylightsensor == null){ textlightsensor.settext("no light sensor!"); //this get! }else{ textlightsensor.settext(mylightsensor.getname() + "\n " + mylightsensor.getresolution() ); mysensormanager.registerlistener(lightsensoreventlistener, mylightsensor, sensormanager.sensor_delay_normal); } } private onclicklistener btlistener = new onclicklistener() { public void onclick(view v){ mylightsensor = mysensormanager.getdefaultsensor(sensor.type_light); if (mylightsensor == null){ textlightsensor.settext("no light sensor!"); }else{ textlightsensor.settext(mylightsensor.getname() + "\n " + mylightsensor.getresolution() ); mysensormanager.registerlistener(lightsensoreventlistener, mylightsensor, sensormanager.sensor_delay_normal); } } }; sensoreventlistener lightsensoreventlistener = new sensoreventlistener(){ @override public void onaccuracychanged(sensor arg0, int arg1) { // todo auto-generated method stub } @override public void onsensorchanged(sensorevent arg0) { // todo auto-generated method stub if(arg0.sensor.gettype()==sensor.type_light){ textlightsensordata.settext("light sensor date:" + string.valueof(arg0.values[0])); }else if(arg0.sensor.gettype()==sensor.type_proximity){ textlightsensordata.settext("test " + string.valueof(arg0.values[0]) +" " + string.valueof(arg0.values[1])); } }}; }
but "no light sensor" return on activity. lg p990 have one. in "hidden menu" can see values of light sensor in alc / proximity test.
do have change in code run?
not sure getdefaultsensor()
, documentation says:
note returned sensor composite sensor, , data averaged or filtered. if need access raw sensors, use getsensorlist()
this how i'm doing it:
sensormanager sm = (sensormanager) getsystemservice(context.sensor_service); // check if there @ least 1 light sensor if (sm.getsensorlist(sensor.type_light).size() == 0) { // no light sensor installed } else { sensor light = sm.getsensorlist(sensor.type_light).get(0); if (!sm.registerlistener(this, light, sensormanager.sensor_delay_game)) { // couldn't register light sensor listener } }
and in onsensorchanged()
method, event.values[0]
gives value of light sensor, integer in 0..100 range, if have seen values of 10 , 100 :-)
good luck,
Comments
Post a Comment