// This method is called when the proximity sensor reports a state change, // e.g. from "NEAR to FAR" or from "FAR to NEAR". private void onProximitySensorChangedState() { // The proximity sensor should only be activated when there are exactly two // available audio devices. if (audioDevices.size() == 2 && audioDevices.contains(AppRTCAudioManager.AudioDevice.EARPIECE) && audioDevices.contains(AppRTCAudioManager.AudioDevice.SPEAKER_PHONE)) { if (proximitySensor.sensorReportsNearState()) { // Sensor reports that a "handset is being held up to a person's ear", // or "something is covering the light sensor". setAudioDevice(AppRTCAudioManager.AudioDevice.EARPIECE); } else { // Sensor reports that a "handset is removed from a person's ear", or // "the light sensor is no longer covered". setAudioDevice(AppRTCAudioManager.AudioDevice.SPEAKER_PHONE); } } }