我(或者我认为)拼凑了代码,这些代码应该允许我接收所有传感器数据更改并将它们输出到日志中。但是,我得到的唯一传感器是光传感器(传感器#5)。
Inside OnCreate:
{
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Log.i("aaa","---sensors on");
sm.registerListener(sensorListener,sm.getDefaultSensor(Sensor.TYPE_ALL),SensorManager.SENSOR_DELAY_UI);
List<Sensor> typedSensors = sm.getSensorList(Sensor.TYPE_ALL);
for(Iterator<Sensor> i = typedSensors.iterator(); i.hasNext();)
{
Sensor item = i.next();
Log.i("aaa","SENSOR: "+item.getName());
}
}
private static SensorEventListener sensorListener=new SensorEventListener() {
public void onSensorChanged(SensorEvent e) {
writeSensorLog(e.sensor.getType()+":"+e.values[0]);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// unused
}
};
public static void writeSensorLog(String data)
{
Log.i("aaa","sensor string: "+data);
}
答案 0 :(得分:2)
TYPE_ALL
用于获取设备中可用的所有传感器的列表。要注册所有传感器,请调用getSensorList
并注册列表中的每个成员。
List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor : sensors)
{
sm.registerListener(sensorListener, sensor, SensorManager.SENSOR_DELAY_UI);
}