Try this...
@Override
public void onSensorChanged(SensorEvent event) {
if(event.values[0] < event.sensor.getMaximumRange()) {
// Object is near to the Sensor. increment the value
}
}
and according to your code
private int nearCount; // global variable.
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[0] < event.sensor.getMaximumRange()) {
nearCount++;
Toast.makeText(getApplicationContext(), "Count = "+nearCount, Toast.LENGTH_SHORT).show();
}
}