I need to get the data from multiple sensors. I have tried creating a single listener for all the sensors and have tried creating individual listeners for each sensor. Neither method works if more then one sensor is added. If multiple sensors are added, the data produced by the sensor is invalid. Has anyone found a solution to this problem?
Here is the code to add sensors:
List <Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
//add sensor listeners
for(Sensor curSensor : sensorList){
sensorManager.registerListener(sensorListener,
curSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
Here is the code to respond to sensor changes:
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorListener = new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor arg0, int arg1){
}
@Override
public void onSensorChanged(SensorEvent event){
sensorValues.put(Integer.valueOf(event.sensor.getType()),event.values);
updateDisplayValues();
}
};
and finally, here is the code to update the display:
private void updateDisplayValues(){
Enumeration<Integer> keys = sensorValues.keys();
int curIndex = 0;
while(keys.hasMoreElements()){
Integer key = keys.nextElement();
String curStringForKey = getStringForKey(key);
if(curStringForKey==null)continue;
float [] value = sensorValues.get(key);
if(key==TYPE_ORIENTATION){
ValueDisplay[curIndex].setText(curStringForKey + ": " + getResources().getConfiguration().orientation);
}else{
String stringToPrint = new String();
int paramLength = value.length;
for(int i = 0; i <paramLength; i++)
stringToPrint += value[i]+", ";
ValueDisplay[curIndex].setText(curStringForKey + ": " + stringToPrint);
}
curIndex ++;
}
}