0

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 ++;

    }

}
user1832287
  • 199
  • 2
  • 10
  • Hmm, post your code so we can see what you are doing. I haven't heard of this problem before: [Is there a way to retrieve multiple sensor data in Android](http://stackoverflow.com/q/4343342/1267661). – Sam Nov 17 '12 at 17:55
  • What datatype is `sensorValues`? – Sam Nov 17 '12 at 18:38
  • It is declared like so: private volatile Hashtable sensorValues; – user1832287 Nov 17 '12 at 18:45
  • I didn't use your `updateDisplayValues()`, I simply used `Log.v(event.sensor.getName(), Arrays.toString(event.values));` in `onSensorChanged()`. The data I saw was valid. I even singled out the light sensor like you mentioned below, but I'm unable to reproduce what you described. I guess that something is awry in `updateDisplayValues()`. – Sam Nov 17 '12 at 19:12

1 Answers1

0

What do you mean by:

the data produced by the sensor is invalid

I believe the problem is not in registering multiple sensors, if you simply printed the sensor type and value in the method onSensorChanged i.e. use Log.d() it should work fine.

The problem in the sensorValues object and the way you access it in the method updateDisplayValues, make sure that it is a thread-safe type.

Sensors updates are very fast and doing a loop in the updateDisplayValues() does not seem to be right design.

EDIT:

String stringToPrint = new String();
int paramLength = value.length;
        for(int i = 0; i <paramLength; i++)
            stringToPrint += value[i]+", ";

It is not related to your question but the above code is really not good practice. Strings are immutable objects so every time you try to modify a String (e.g. stringToPrint += "value") a new string object will be created. Use StringBuffer or StringBuilder instead (one is thread-safe and the other is not)

iTech
  • 17,211
  • 4
  • 52
  • 78
  • What I mean is that, for example, the light sensor returns an array of three floats ranging from negative 1 to 200 when other sensors are enabled and returns a value between 30-40 when other sensors are not enabled. sensorValues is a Hashtable and is declared volatile. I changed the updateDisplay function to only execute once per second. The error is still present. – user1832287 Nov 17 '12 at 18:44
  • Try to drop the Hashtable and process the data directly in the onSensorChanged i.e. put the code from updateDisplayValues() directly in the onSensorChanged and see if it will work. – iTech Nov 17 '12 at 20:03
  • I ended up calling the updateDisplay function at a set interval regardless of how quickly the sensors were updating. That fixed the problem. Thank you iTech for that information about setting up a string correctly. I wasn't really sure how to do this. – user1832287 Nov 18 '12 at 13:11