简体   繁体   中英

Xperia light sensor [Xperia PRO]

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

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)

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