Search code examples
javaandroidandroid-sensors

Android Sensor Issue


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

    }

}

Solution

  • 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)