开发者

Reading LightSensor from LG P990

I am trying to Develop a Light Sensor App for my LG P990 to detect a Laser (don't ask why).

The Code looks like 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.TYP开发者_运维问答E_LIGHT);

   Start = (Button)findViewById(R.id.btStart);
   Start.setOnClickListener(btListener);    

   if (myLightSensor == null){
    textLightSensor.setText("No Light Sensor!"); //THIS IS WHAT I 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 I only get a "No Light Sensor" return on my Activity. The LG P990 does have one. In the "hidden menu" I can see the values of the light Sensor in the ALC / Proximity Test.

Do I have to change anything in my code to get it to run?


not sure about getDefaultSensor(), but the documentation for it says:

    Note that the returned sensor could be a composite sensor,
    and its data could be averaged or filtered. If you need to access
    the raw sensors, use getSensorList()

This is how I'm doing it:

    SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    // Check if there is at least one 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 then in your onSensorChanged() method, event.values[0] gives you the value of the light sensor, that is an integer in the 0..100 range, even if I have only seen values of 10 and 100 :-)

Good luck,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜