Android Sensors - Problem getting Proximity Sensor Values and problems with using different phones

StackOverflow https://stackoverflow.com/questions/5090495

سؤال

Have done some Android Dev in the past but it has been a while - want to build an app that uses Orientation and Proximity Sensors. so...

mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ALL) , SensorManager.SENSOR_DELAY_GAME);

So set up listener - listened to all sensors - wonder should I just listen to two? (would this save battery?)

Sensor mySensor = event.sensor;
    // if (mySensor.getType() != Sensor.TYPE_ORIENTATION) return;
    if (mySensor.getType() == Sensor.TYPE_ORIENTATION)
    {
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        tv.setText("Rotation around X, Azimuth = " + x);
        tv1.setText("Rotation around Y, Pitch = " + y);
        tv2.setText("Rotation around Z, Roll = " + z);
    }
    if (mySensor.getType() == SensorManager.SENSOR_PROXIMITY)
    {

        float p = event.values[0];
        tv3.setText("Proximity = " + p);

    }

Then in onSensorChanged - see above

on my LG Optimus One I can get it to display the values in the TextViews of the Orientation sensor (this phone doesn't have a proximity sensor)

But running the same code on my Nexus One leads to a lovely blank screen!?

Sorry for the long question! but any help would be good and if you need more infor just ask, LG phone is 2.2 Nexus One is 2.2.2 Thanks,

David

هل كانت مفيدة؟

المحلول

You can use the same listener for all sensors. But if you really want to listen to multiple or even all sensors, you need to rewrite this line:

mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ALL) , SensorManager.SENSOR_DELAY_GAME);

Because the actual implementation of getDefaultSensor(int type) returns only one sensor, as seen in the source code:

public Sensor getDefaultSensor(int type) {
    // TODO: need to be smarter, for now, just return the 1st sensor
    List<Sensor> l = getSensorList(type);
    return l.isEmpty() ? null : l.get(0);
}

public List<Sensor> getSensorList(int type) {
    // cache the returned lists the first time
    List<Sensor> list;
    final ArrayList<Sensor> fullList = sFullSensorsList;
    synchronized(fullList) {
        list = sSensorListByType.get(type);
        if (list == null) {
           if (type == Sensor.TYPE_ALL) {
               list = fullList;
           } else {
                list = new ArrayList<Sensor>();
                for (Sensor i : fullList) {
                     if (i.getType() == type)
                        list.add(i);
                }
           }
           list = Collections.unmodifiableList(list);
           sSensorListByType.append(type, list);
       }

    return list;
}

So in case of Sensor.TYPE_ALL the method getSensorList(int type) provides an (unordered) list of all sensors available and getDefaultSensor(int type) returns only the first one of it.

Because the list containing all sensors is unordered, getDefaultSensor(Sensor.TYPE_ALL) supplies a random sensor. I tried it and first got the accelerometer and next time the light sensor. Since you handle only orientation and proximity changes in your listener, I would suspect that getDefaultSensor(Sensor.TYPE_ALL) supplied a different sensor on your Nexus.

You should receive a list of all sensors available with mSensorManager.getSensorList(Sensor.TYPE_ALL) and iterate over the list, in order to register every contained sensor respectively.

Regarding your question about saving battery - I don't know, if the sensors are running permanently. Even if this is the case, one can always save battery power by reducing unnecessary processing. If you do not need the information a specific sensor provides, do not register to receive messages from it. That reduces messages being sent to your application.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top