diff --git a/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCAudioManager.java b/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCAudioManager.java index 0ea81cf5ce..475059116d 100644 --- a/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCAudioManager.java +++ b/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCAudioManager.java @@ -352,16 +352,16 @@ private void setAudioDeviceInternal(AudioDevice device) { public void setDefaultAudioDevice(AudioDevice defaultDevice) { ThreadUtils.checkIsOnMainThread(); switch (defaultDevice) { + case EARPIECE: + if (hasEarpiece()) { + defaultAudioDevice = defaultDevice; + } else { + defaultAudioDevice = AudioDevice.SPEAKER_PHONE; + } + break; case SPEAKER_PHONE: defaultAudioDevice = defaultDevice; break; - case EARPIECE: - if (hasEarpiece()) { - defaultAudioDevice = defaultDevice; - } else { - defaultAudioDevice = AudioDevice.SPEAKER_PHONE; - } - break; default: Log.e(TAG, "Invalid default audio device selection"); break; @@ -408,6 +408,14 @@ public void setSpeakerphoneOn(boolean on) { if (wasOn == on) { return; } + final RTCBluetoothManager.State btManagerState = bluetoothManager.getState(); + final boolean isBTAvailable = + btManagerState == RTCBluetoothManager.State.SCO_CONNECTED + || btManagerState == RTCBluetoothManager.State.SCO_CONNECTING + || btManagerState == RTCBluetoothManager.State.HEADSET_AVAILABLE; + if(!on && isBTAvailable){ + bluetoothManager.startScoAudio(); + } audioManager.setSpeakerphoneOn(on); } diff --git a/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCBluetoothManager.java b/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCBluetoothManager.java index 2cd72d6987..bc79b9af2c 100644 --- a/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCBluetoothManager.java +++ b/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCBluetoothManager.java @@ -324,6 +324,12 @@ public boolean startScoAudio() { Log.e(TAG, "BT SCO connection fails - no more attempts"); return false; } + List devices = bluetoothHeadset.getConnectedDevices(); + if (!devices.isEmpty()) { + bluetoothDevice = devices.get(0); + bluetoothState = State.HEADSET_AVAILABLE; + } + if (bluetoothState != State.HEADSET_AVAILABLE) { Log.e(TAG, "BT SCO connection fails - no headset available"); return false; diff --git a/common/darwin/Classes/FlutterWebRTCPlugin.m b/common/darwin/Classes/FlutterWebRTCPlugin.m index 438be2c8f9..e62c2c81b5 100644 --- a/common/darwin/Classes/FlutterWebRTCPlugin.m +++ b/common/darwin/Classes/FlutterWebRTCPlugin.m @@ -559,8 +559,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult) result _speakerOn = enable.boolValue; AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord - withOptions:_speakerOn ? AVAudioSessionCategoryOptionDefaultToSpeaker : 0 - error:nil]; + withOptions:_speakerOn ? AVAudioSessionCategoryOptionDefaultToSpeaker + : + AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP + error:nil]; [audioSession setActive:YES error:nil]; result(nil); #else