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

The following examples show how to use android.media.AudioManager#stopBluetoothSco() . 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 mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public void setWantsConnection(boolean enabled) {
  synchronized (LOCK) {
    AudioManager audioManager = ServiceUtil.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: 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 3
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 4
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 5
Source File: BluetoothScoReceiver.java    From Jumble with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int audioState = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);
    switch (audioState) {
        case AudioManager.SCO_AUDIO_STATE_CONNECTED:
            mBluetoothScoOn = true;
            mListener.onBluetoothScoConnected();
            break;
        case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
        case AudioManager.SCO_AUDIO_STATE_ERROR:
            am.stopBluetoothSco();
            mBluetoothScoOn = false;
            mListener.onBluetoothScoDisconnected();
            break;
    }
}
 
Example 6
Source File: AudioManagerUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 停止蓝牙 SCO 音频连接
 * @return {@code true} success, {@code false} fail
 */
public static boolean stopBluetoothSco() {
    AudioManager audioManager = AppUtils.getAudioManager();
    if (audioManager != null) {
        try {
            audioManager.stopBluetoothSco();
            return true;
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "stopBluetoothSco");
        }
    }
    return false;
}
 
Example 7
Source File: AssistantService.java    From AssistantBySDK with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "blueConnectStateBroadcastReceiver action>>>>" + intent.getAction());
    if (intent.getAction() == null)
        return;
    if (intent.getAction().equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
        int blueconState = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0);
        switch (blueconState) {
            case BluetoothAdapter.STATE_CONNECTED:
                Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_CONNECTED");
                voiceMediator.setBlueHeadSet(true);
                isBlueToothHeadsetConnected();
                break;
            case BluetoothAdapter.STATE_CONNECTING:
                Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_CONNECTING");
                break;
            case BluetoothAdapter.STATE_DISCONNECTED:
                Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_DISCONNECTED");
                voiceMediator.setBlueHeadSet(false);
                voiceMediator.setSuportA2DP(false);
                AudioManager mAudioManager_ = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                mAudioManager_.setBluetoothScoOn(false);
                mAudioManager_.stopBluetoothSco();
                break;
            case BluetoothAdapter.STATE_DISCONNECTING:
                Log.i(TAG, "blueConnectStateBroadcastReceiver>>>>STATE_DISCONNECTING");
                voiceMediator.setSuportA2DP(false);
                break;
            default:
                break;
        }
    }
}
 
Example 8
Source File: CustomTwilioVideoView.java    From react-native-twilio-video-webrtc with MIT License 5 votes vote down vote up
public void toggleBluetoothHeadset(boolean enabled) {
    AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
    if(enabled){
        audioManager.startBluetoothSco();
    } else {
        audioManager.stopBluetoothSco();
    }
}
 
Example 9
Source File: BluetoothManager.java    From BlueSound with The Unlicense 5 votes vote down vote up
public boolean streamAudioStop(Context context) {
	mAudioStatus = false;
	AudioManager localAudioManager = (AudioManager) context
			.getSystemService(Context.AUDIO_SERVICE);
	if( localAudioManager == null ){ return false; }
	localAudioManager.setBluetoothScoOn(false);
	localAudioManager.stopBluetoothSco();
	localAudioManager.setMode(AudioManager.MODE_NORMAL);
	return true;
}
 
Example 10
Source File: VoIPBaseService.java    From TelePlus-Android 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.didEndedCall);
		}
	});
	if (controller != null && controllerStarted) {
		lastKnownDuration = controller.getCallDuration();
		updateStats();
		StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (lastKnownDuration / 1000) % 5);
		onControllerPreRelease();
		controller.release();
		controller = null;
	}
	cpuWakelock.release();
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		if(isBtHeadsetConnected && !playingSound){
			am.stopBluetoothSco();
			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(systemCallConnection!=null && !playingSound){
			systemCallConnection.destroy();
		}
	}

	ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
	VoIPHelper.lastCallTime=System.currentTimeMillis();
}
 
Example 11
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 12
Source File: VoIPBaseService.java    From TelePlus-Android 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.didEndedCall);
		}
	});
	if (controller != null && controllerStarted) {
		lastKnownDuration = controller.getCallDuration();
		updateStats();
		StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (lastKnownDuration / 1000) % 5);
		onControllerPreRelease();
		controller.release();
		controller = null;
	}
	cpuWakelock.release();
	AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
	if(!USE_CONNECTION_SERVICE){
		if(isBtHeadsetConnected && !playingSound){
			am.stopBluetoothSco();
			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(systemCallConnection!=null && !playingSound){
			systemCallConnection.destroy();
		}
	}

	ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
	VoIPHelper.lastCallTime=System.currentTimeMillis();
}
 
Example 13
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 14
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();
}