The app i am building makes use of the proximity sensor. Trying it in different phones i found that the proximity sensor has different (min,max) values in different phones (my htc sensation has 0 for covered and 9 for uncovered while xperia has 0 for covered and 1 for uncovered).So the question is: How do i get the proximity sensor's min max values ??

有帮助吗?

解决方案

I think minimum value for proximity Sensor is 0 for all devices. and to get maximum range...

    SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    Sensor proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
    proximitySensor.getMaximumRange();

其他提示

According documentation :

Some proximity sensors only support a binary near or far measurement.
In this case, the sensor should report its maximum range value in the far state
and a lesser value in the near state

So proximity sensor could even had no min and max values. Then you should just compare its value with proximitySensor.getMaximumRange().

Each proximity sensor is different. You have to monitor the values in onSensorChanged() and remember min and max value. It's working fine. Here is my code for checking near state.

    private float proximityCalibratedMax = Float.MIN_VALUE;

    ...

    public void onSensorChanged(SensorEvent event) {

            if(event.values[0] > proximityCalibratedMax){
                proximityCalibratedMax = event.values[0];
            }

            if (event.sensor.getType() == Sensor.TYPE_PROXIMITY && proximityCalibratedMax != Float.MIN_VALUE) {
                if (event.values[0] < proximityCalibratedMax) {
                    prevNear = true;
                    sensorTimestamp = new Date().getTime();

                } else {
                    long currentTimestamp = new Date().getTime();
                    prevNear = false;
                    openAutifyCarMode();

                }
            }
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top