Java Code Examples for android.media.AudioManager#setBluetoothScoOn()

The following examples show how to use android.media.AudioManager#setBluetoothScoOn() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BluetoothStateManager.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
public void setWantsConnection(boolean enabled) {
    synchronized (LOCK) {
        AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);

        this.wantsConnection = enabled;

        if (wantsConnection && isBluetoothAvailable() && scoConnection == ScoConnection.DISCONNECTED) {
            audioManager.startBluetoothSco();
            scoConnection = ScoConnection.IN_PROGRESS;
        } else if (!wantsConnection && scoConnection == ScoConnection.CONNECTED) {
            audioManager.stopBluetoothSco();
            audioManager.setBluetoothScoOn(false);
            scoConnection = ScoConnection.DISCONNECTED;
        } else if (!wantsConnection && scoConnection == ScoConnection.IN_PROGRESS) {
            audioManager.stopBluetoothSco();
            scoConnection = ScoConnection.DISCONNECTED;
        }
    }
}
 
Example 2
Source File: SignalAudioManager.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
public void stop(boolean playDisconnected) {
  AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);

  incomingRinger.stop();
  outgoingRinger.stop();

  if (playDisconnected) {
    soundPool.play(disconnectedSoundId, 1.0f, 1.0f, 0, 0, 1.0f);
  }

  if (audioManager.isBluetoothScoOn()) {
    audioManager.setBluetoothScoOn(false);
    audioManager.stopBluetoothSco();
  }

  audioManager.setSpeakerphoneOn(false);
  audioManager.setMicrophoneMute(false);
  audioManager.setMode(AudioManager.MODE_NORMAL);
  audioManager.abandonAudioFocus(null);
}
 
Example 3
Source File: CallActivity.java    From sealrtc-android with MIT License 6 votes vote down vote up
@Override
public void onNotifySCOAudioStateChange(int scoAudioState) {
    switch (scoAudioState) {
        case AudioManager.SCO_AUDIO_STATE_CONNECTED:
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if (am != null) {
                am.setBluetoothScoOn(true);
            }
            break;
        case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
            Log.d("onNotifyHeadsetState",
                "onNotifySCOAudioStateChange: " + headsetPlugReceiver.isBluetoothConnected());
            if (headsetPlugReceiver.isBluetoothConnected()) {
                startBluetoothSco();
            }
            break;
    }
}
 
Example 4
Source File: BluetoothManager.java    From BlueSound with The Unlicense 6 votes vote down vote up
public boolean streamAudioStart(Context context) {
	try{
		AudioManager localAudioManager = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		if( localAudioManager == null ){ return false; }
		localAudioManager.setMode(0);
		localAudioManager.setBluetoothScoOn(true);
		localAudioManager.startBluetoothSco();
		localAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
		mAudioStatus = true;
	}catch( Exception ex){
		System.out.println("Exception on Start " + ex.getMessage() );
		mAudioStatus = false;
		return false;
	}

	return mAudioStatus;
}
 
Example 5
Source File: AssistantService.java    From AssistantBySDK with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(AudioManager audioManager) {
    if (voiceMediator.isBlueToothHeadSet()) {
        if (!voiceMediator.isSuportA2DP()) {
            if (audioManager.getMode() != AudioManager.MODE_NORMAL) {
                Log.e(TAG, "playInChannel>>setMode(AudioManager.MODE_NORMAL)");
                audioManager.setMode(AudioManager.MODE_NORMAL);
            }
            if (audioManager.isBluetoothScoOn()) {
                audioManager.setBluetoothScoOn(false);
                audioManager.stopBluetoothSco();
            }
        } else {
            if (!audioManager.isBluetoothA2dpOn()) {
                Log.e(TAG, "playInChannel>>setBluetoothA2dpOn(true)");
                audioManager.setBluetoothA2dpOn(true);
            }
        }
    }
}
 
Example 6
Source File: BluetoothStateManager.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
  if (intent == null) return;
  Log.i(TAG, "onReceive");

  synchronized (LOCK) {
    if (getScoChangeIntent().equals(intent.getAction())) {
      int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);

      if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
        if (bluetoothHeadset != null) {
          List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();

          for (BluetoothDevice device : devices) {
            if (bluetoothHeadset.isAudioConnected(device)) {
              int deviceClass = device.getBluetoothClass().getDeviceClass();

              if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
                  deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO ||
                  deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET)
              {
                scoConnection = ScoConnection.CONNECTED;

                if (wantsConnection) {
                  AudioManager audioManager = ServiceUtil.getAudioManager(context);
                  audioManager.setBluetoothScoOn(true);
                }
              }
            }
          }

        }
      }
    }
  }

  handleBluetoothStateChange();
}
 
Example 7
Source File: MediaController.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private void setUseFrontSpeaker(boolean value) {
    useFrontSpeaker = value;
    AudioManager audioManager = NotificationsController.audioManager;
    if (useFrontSpeaker) {
        audioManager.setBluetoothScoOn(false);
        audioManager.setSpeakerphoneOn(false);
    } else {
        audioManager.setSpeakerphoneOn(true);
    }
}
 
Example 8
Source File: BluetoothStateManager.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent == null) {
        return;
    }
    Log.w(TAG, "onReceive");

    synchronized (LOCK) {
        if (getScoChangeIntent().equals(intent.getAction())) {
            int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);

            if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED && bluetoothHeadset != null) {
                List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();

                for (BluetoothDevice device : devices) {
                    if (bluetoothHeadset.isAudioConnected(device)) {
                        int deviceClass = device.getBluetoothClass().getDeviceClass();

                        if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
                                deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO ||
                                deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
                            scoConnection = ScoConnection.CONNECTED;

                            if (wantsConnection) {
                                AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);
                                audioManager.setBluetoothScoOn(true);
                            }
                        }
                    }
                }
            }
        }
    }

    handleBluetoothStateChange();
}
 
Example 9
Source File: MediaController.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
private void setUseFrontSpeaker(boolean value) {
    useFrontSpeaker = value;
    AudioManager audioManager = NotificationsController.audioManager;
    if (useFrontSpeaker) {
        audioManager.setBluetoothScoOn(false);
        audioManager.setSpeakerphoneOn(false);
    } else {
        audioManager.setSpeakerphoneOn(true);
    }
}
 
Example 10
Source File: VoIPBaseService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
protected void updateBluetoothHeadsetState(boolean connected){
	if(connected==isBtHeadsetConnected)
		return;
	if(BuildVars.LOGS_ENABLED)
		FileLog.d("updateBluetoothHeadsetState: "+connected);
	isBtHeadsetConnected=connected;
	final AudioManager am=(AudioManager)getSystemService(AUDIO_SERVICE);
	if(connected && !isRinging() && currentState!=0){
		if(bluetoothScoActive){
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("SCO already active, setting audio routing");
			am.setSpeakerphoneOn(false);
			am.setBluetoothScoOn(true);
		}else{
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("startBluetoothSco");
			needSwitchToBluetoothAfterScoActivates=true;
			// some devices ignore startBluetoothSco when called immediately after the headset is connected, so delay it
			AndroidUtilities.runOnUIThread(new Runnable(){
				@Override
				public void run(){
					try {
						am.startBluetoothSco();
					} catch (Throwable ignore) {

					}
				}
			}, 500);
		}
	}else{
		bluetoothScoActive=false;
	}
	for (StateListener l : stateListeners)
		l.onAudioSettingsChanged();
}
 
Example 11
Source File: AudioManagerUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 设置是否使用蓝牙 SCO 耳机进行通讯
 * @param on {@code true} yes, {@code false} no
 * @return {@code true} success, {@code false} fail
 */
public static boolean setBluetoothScoOn(final boolean on) {
    AudioManager audioManager = AppUtils.getAudioManager();
    if (audioManager != null) {
        try {
            audioManager.setBluetoothScoOn(on);
            return true;
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "setBluetoothScoOn");
        }
    }
    return false;
}
 
Example 12
Source File: VoIPBaseService.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
protected void updateBluetoothHeadsetState(boolean connected){
	if(connected==isBtHeadsetConnected)
		return;
	if(BuildVars.LOGS_ENABLED)
		FileLog.d("updateBluetoothHeadsetState: "+connected);
	isBtHeadsetConnected=connected;
	final AudioManager am=(AudioManager)getSystemService(AUDIO_SERVICE);
	if(connected && !isRinging() && currentState!=0){
		if(bluetoothScoActive){
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("SCO already active, setting audio routing");
			am.setSpeakerphoneOn(false);
			am.setBluetoothScoOn(true);
		}else{
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("startBluetoothSco");
			needSwitchToBluetoothAfterScoActivates=true;
			// some devices ignore startBluetoothSco when called immediately after the headset is connected, so delay it
			AndroidUtilities.runOnUIThread(new Runnable(){
				@Override
				public void run(){
					try {
						am.startBluetoothSco();
					} catch (Throwable ignore) {

					}
				}
			}, 500);
		}
	}else{
		bluetoothScoActive=false;
	}
	for (StateListener l : stateListeners)
		l.onAudioSettingsChanged();
}
 
Example 13
Source File: VoIPBaseService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
protected void updateBluetoothHeadsetState(boolean connected){
	if(connected==isBtHeadsetConnected)
		return;
	if(BuildVars.LOGS_ENABLED)
		FileLog.d("updateBluetoothHeadsetState: "+connected);
	isBtHeadsetConnected=connected;
	final AudioManager am=(AudioManager)getSystemService(AUDIO_SERVICE);
	if(connected && !isRinging() && currentState!=0){
		if(bluetoothScoActive){
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("SCO already active, setting audio routing");
			am.setSpeakerphoneOn(false);
			am.setBluetoothScoOn(true);
		}else{
			if(BuildVars.LOGS_ENABLED)
				FileLog.d("startBluetoothSco");
			needSwitchToBluetoothAfterScoActivates=true;
			// some devices ignore startBluetoothSco when called immediately after the headset is connected, so delay it
			AndroidUtilities.runOnUIThread(new Runnable(){
				@Override
				public void run(){
					try {
						am.startBluetoothSco();
					} catch (Throwable ignore) {

					}
				}
			}, 500);
		}
	}else{
		bluetoothScoActive=false;
	}
	for (StateListener l : stateListeners)
		l.onAudioSettingsChanged();
}
 
Example 14
Source File: VoIPBaseService.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onDestroy() {
	if (BuildVars.LOGS_ENABLED) {
		FileLog.d("=============== VoIPService STOPPING ===============");
	}
	stopForeground(true);
	stopRinging();
	NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.appDidLogout);
	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	if (proximity != null) {
		sm.unregisterListener(this);
	}
	if (proximityWakelock != null && proximityWakelock.isHeld()) {
		proximityWakelock.release();
	}
	unregisterReceiver(receiver);
	if(timeoutRunnable!=null){
		AndroidUtilities.cancelRunOnUIThread(timeoutRunnable);
		timeoutRunnable=null;
	}
	super.onDestroy();
	sharedInstance = null;
	AndroidUtilities.runOnUIThread(new Runnable(){
		@Override
		public void run(){
			NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didEndCall);
		}
	});
	if (tgVoip != null) {
		updateTrafficStats();
		StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (getCallDuration() / 1000) % 5);
		onTgVoipPreStop();
		onTgVoipStop(tgVoip.stop());
		prevTrafficStats = null;
		callStartTime = 0;
		tgVoip = null;
	}
	cpuWakelock.release();
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		if(isBtHeadsetConnected && !playingSound){
			am.stopBluetoothSco();
			am.setBluetoothScoOn(false);
			am.setSpeakerphoneOn(false);
		}
		try{
			am.setMode(AudioManager.MODE_NORMAL);
		}catch(SecurityException x){
			if(BuildVars.LOGS_ENABLED){
				FileLog.e("Error setting audio more to normal", x);
			}
		}
		am.abandonAudioFocus(this);
	}
	am.unregisterMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
	if (haveAudioFocus)
		am.abandonAudioFocus(this);

	if (!playingSound)
		soundPool.release();

	if(USE_CONNECTION_SERVICE){
		if(!didDeleteConnectionServiceContact)
			ContactsController.getInstance(currentAccount).deleteConnectionServiceContact();
		if(systemCallConnection!=null && !playingSound){
			systemCallConnection.destroy();
		}
	}

	ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
	VoIPHelper.lastCallTime=System.currentTimeMillis();
}
 
Example 15
Source File: VoIPBaseService.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
protected void configureDeviceForCall() {
	needPlayEndSound = true;
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		am.setMode(AudioManager.MODE_IN_COMMUNICATION);
		am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					if(!bluetoothScoActive){
						needSwitchToBluetoothAfterScoActivates=true;
						try {
							am.startBluetoothSco();
						} catch (Throwable ignore) {

						}
					}else{
						am.setBluetoothScoOn(true);
						am.setSpeakerphoneOn(false);
					}
					break;
				case AUDIO_ROUTE_EARPIECE:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(false);
					break;
				case AUDIO_ROUTE_SPEAKER:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(true);
					break;
			}
		}else if(isBluetoothHeadsetConnected()){
			am.setBluetoothScoOn(speakerphoneStateToSet);
		}else{
			am.setSpeakerphoneOn(speakerphoneStateToSet);
		}
	}/*else{
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
					break;
				case AUDIO_ROUTE_EARPIECE:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
					break;
				case AUDIO_ROUTE_SPEAKER:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
					break;
			}
		}else{
			if(hasEarpiece())
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
			else
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
		}
	}*/
	updateOutputGainControlState();
	audioConfigured=true;

	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	try{
		if(proximity!=null){
			proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
			sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}catch(Exception x){
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("Error initializing proximity sensor", x);
		}
	}
}
 
Example 16
Source File: VoIPBaseService.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onDestroy() {
	if (BuildVars.LOGS_ENABLED) {
		FileLog.d("=============== VoIPService STOPPING ===============");
	}
	stopForeground(true);
	stopRinging();
	NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.appDidLogout);
	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	if (proximity != null) {
		sm.unregisterListener(this);
	}
	if (proximityWakelock != null && proximityWakelock.isHeld()) {
		proximityWakelock.release();
	}
	unregisterReceiver(receiver);
	if(timeoutRunnable!=null){
		AndroidUtilities.cancelRunOnUIThread(timeoutRunnable);
		timeoutRunnable=null;
	}
	super.onDestroy();
	sharedInstance = null;
	AndroidUtilities.runOnUIThread(new Runnable(){
		@Override
		public void run(){
			NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didEndCall);
		}
	});
	if (tgVoip != null) {
		updateTrafficStats();
		StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (getCallDuration() / 1000) % 5);
		onTgVoipPreStop();
		onTgVoipStop(tgVoip.stop());
		prevTrafficStats = null;
		callStartTime = 0;
		tgVoip = null;
	}
	cpuWakelock.release();
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		if(isBtHeadsetConnected && !playingSound){
			am.stopBluetoothSco();
			am.setBluetoothScoOn(false);
			am.setSpeakerphoneOn(false);
		}
		try{
			am.setMode(AudioManager.MODE_NORMAL);
		}catch(SecurityException x){
			if(BuildVars.LOGS_ENABLED){
				FileLog.e("Error setting audio more to normal", x);
			}
		}
		am.abandonAudioFocus(this);
	}
	am.unregisterMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
	if (haveAudioFocus)
		am.abandonAudioFocus(this);

	if (!playingSound)
		soundPool.release();

	if(USE_CONNECTION_SERVICE){
		if(!didDeleteConnectionServiceContact)
			ContactsController.getInstance(currentAccount).deleteConnectionServiceContact();
		if(systemCallConnection!=null && !playingSound){
			systemCallConnection.destroy();
		}
	}

	ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
	VoIPHelper.lastCallTime=System.currentTimeMillis();
}
 
Example 17
Source File: VoIPBaseService.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
protected void configureDeviceForCall() {
	needPlayEndSound = true;
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		am.setMode(AudioManager.MODE_IN_COMMUNICATION);
		am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					if(!bluetoothScoActive){
						needSwitchToBluetoothAfterScoActivates=true;
						try {
							am.startBluetoothSco();
						} catch (Throwable ignore) {

						}
					}else{
						am.setBluetoothScoOn(true);
						am.setSpeakerphoneOn(false);
					}
					break;
				case AUDIO_ROUTE_EARPIECE:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(false);
					break;
				case AUDIO_ROUTE_SPEAKER:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(true);
					break;
			}
		}else if(isBluetoothHeadsetConnected()){
			am.setBluetoothScoOn(speakerphoneStateToSet);
		}else{
			am.setSpeakerphoneOn(speakerphoneStateToSet);
		}
	}/*else{
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
					break;
				case AUDIO_ROUTE_EARPIECE:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
					break;
				case AUDIO_ROUTE_SPEAKER:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
					break;
			}
		}else{
			if(hasEarpiece())
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
			else
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
		}
	}*/
	updateOutputGainControlState();
	audioConfigured=true;

	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	try{
		if(proximity!=null){
			proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
			sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}catch(Exception x){
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("Error initializing proximity sensor", x);
		}
	}
}
 
Example 18
Source File: CallActivity.java    From sealrtc-android with MIT License 4 votes vote down vote up
@Override
public void onNotifyHeadsetState(boolean connected, int type) {
    try {
        if (connected) {
            HeadsetPlugReceiverState = true;
            if (type == 0) {
                startBluetoothSco();
            }
            if (null != btnMuteSpeaker) {
                btnMuteSpeaker.setBackgroundResource(R.drawable.img_capture_gray);
                btnMuteSpeaker.setSelected(false);
                btnMuteSpeaker.setEnabled(false);
                btnMuteSpeaker.setClickable(false);
                audioManager.onToggleSpeaker(false);
            }
        } else {
            if (type == 1 && BluetoothUtil.hasBluetoothA2dpConnected()) {
                return;
            }
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if (am != null) {
                if (am.getMode() != AudioManager.MODE_IN_COMMUNICATION) {
                    am.setMode(AudioManager.MODE_IN_COMMUNICATION);
                }
                if (type == 0) {
                    am.stopBluetoothSco();
                    am.setBluetoothScoOn(false);
                    am.setSpeakerphoneOn(!muteSpeaker);
                } else {
                    RCRTCEngine.getInstance().enableSpeaker(!this.muteSpeaker);
                }
                audioManager.onToggleSpeaker(!muteSpeaker);
            }
            if (null != btnMuteSpeaker) {
                btnMuteSpeaker.setBackgroundResource(R.drawable.selector_checkbox_capture);
                btnMuteSpeaker.setSelected(false);
                btnMuteSpeaker.setEnabled(true);
                btnMuteSpeaker.setClickable(true);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 19
Source File: VoIPBaseService.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
protected void configureDeviceForCall() {
	needPlayEndSound = true;
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		am.setMode(AudioManager.MODE_IN_COMMUNICATION);
		am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					if(!bluetoothScoActive){
						needSwitchToBluetoothAfterScoActivates=true;
						try {
							am.startBluetoothSco();
						} catch (Throwable ignore) {

						}
					}else{
						am.setBluetoothScoOn(true);
						am.setSpeakerphoneOn(false);
					}
					break;
				case AUDIO_ROUTE_EARPIECE:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(false);
					break;
				case AUDIO_ROUTE_SPEAKER:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(true);
					break;
			}
		}else if(isBluetoothHeadsetConnected()){
			am.setBluetoothScoOn(speakerphoneStateToSet);
		}else{
			am.setSpeakerphoneOn(speakerphoneStateToSet);
		}
	}/*else{
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
					break;
				case AUDIO_ROUTE_EARPIECE:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
					break;
				case AUDIO_ROUTE_SPEAKER:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
					break;
			}
		}else{
			if(hasEarpiece())
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
			else
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
		}
	}*/
	updateOutputGainControlState();
	audioConfigured=true;

	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	try{
		if(proximity!=null){
			proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
			sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}catch(Exception x){
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("Error initializing proximity sensor", x);
		}
	}
}
 
Example 20
Source File: VoIPBaseService.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
protected void configureDeviceForCall() {
	needPlayEndSound = true;
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		am.setMode(AudioManager.MODE_IN_COMMUNICATION);
		am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					if(!bluetoothScoActive){
						needSwitchToBluetoothAfterScoActivates=true;
						try {
							am.startBluetoothSco();
						} catch (Throwable ignore) {

						}
					}else{
						am.setBluetoothScoOn(true);
						am.setSpeakerphoneOn(false);
					}
					break;
				case AUDIO_ROUTE_EARPIECE:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(false);
					break;
				case AUDIO_ROUTE_SPEAKER:
					am.setBluetoothScoOn(false);
					am.setSpeakerphoneOn(true);
					break;
			}
		}else if(isBluetoothHeadsetConnected()){
			am.setBluetoothScoOn(speakerphoneStateToSet);
		}else{
			am.setSpeakerphoneOn(speakerphoneStateToSet);
		}
	}/*else{
		if(isBluetoothHeadsetConnected() && hasEarpiece()){
			switch(audioRouteToSet){
				case AUDIO_ROUTE_BLUETOOTH:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
					break;
				case AUDIO_ROUTE_EARPIECE:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
					break;
				case AUDIO_ROUTE_SPEAKER:
					systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
					break;
			}
		}else{
			if(hasEarpiece())
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
			else
				systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
		}
	}*/
	updateOutputGainControlState();
	audioConfigured=true;

	SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
	Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
	try{
		if(proximity!=null){
			proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
			sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
		}
	}catch(Exception x){
		if (BuildVars.LOGS_ENABLED) {
			FileLog.e("Error initializing proximity sensor", x);
		}
	}
}