Question

public class MainActivity extends Activity implements SensorEventListener {
    final String tag = "myLogs";
    SensorManager sm = null;
    Sensor lightSensor;
    float lightQuantity;

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

        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        lightSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);

        if(lightSensor == null)
            Log.d(tag, "no sensor:(");
        else
            Log.d(tag, "GOT IT!");

    }

    @Override
    protected void onResume() {
        super.onResume();
        sm.registerListener((SensorEventListener)this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onStop() {
        sm.unregisterListener((SensorEventListener)this);
        super.onStop();
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.d(tag,"onAccuracyChanged: " + sensor.getType() + ", accuracy: " + accuracy);

    }

    public void onSensorChanged(SensorEvent event) {
        lightQuantity = event.values[0];
        Log.d(tag,"onSensorChanged: " + event.sensor.getType() + ", result: " + lightQuantity);

    }
}

I can't get light sensor stats with this code. "no sensor:(" message always. getSensorList() method doesn't show light sensor either. My device: Xperia PRO (mk16i). SE light sensor test works good

Était-ce utile?

La solution

The problem is that Xperia doesn't have the sensor implemented in a standard Android way through drivers. There's only a proprietary implementation which can be used through reading the kernel variables as detailed below. (c)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top