Proximity Sensor

414 views Asked by At

I want to implement proximity sensor in an application. But how can I make the application work in different phone.

Problem : There is an issue with Micromax A177 and Moto E as they seems to have proximity sensor but the min and max value is different. In case of Micromax I found it to be 0 and 1 on Sensor Box application, In case of Moto E it shows 3 and 100. Now what should be the comparing condition for an application as if I compare it with greater then 0 then it will be called all time in Moto E.

Any help is appreciated.

After registering the sensor, implemented listener code is Code used :

    proximitySensorListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {
        // Here I have issue, How to make it working in case of all different phone.
        if(event.values[0]>0)
        {
            methodCalled();
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
};
1

There are 1 answers

0
kapilgm On

According to 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, you should just compare its value with your declared sensor as proximitySensor.getMaximumRange() and use it in your code.

proximitySensorListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {
        // Here I have issue, How to make it working in case of all different phone.
        if(event.values[0]<proximitySensor.getMaximumRange())
        {
            methodCalled();
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
};