From 5d5d26e5f2e70548bd77c41cee6abbc00a7aa64c Mon Sep 17 00:00:00 2001 From: Ayman-Barghout Date: Mon, 21 Dec 2020 14:10:11 +0200 Subject: [PATCH 1/3] [iOS] Bluetooth devices are now allowed when setting `enableSpeakerphone` to false. --- common/darwin/Classes/FlutterWebRTCPlugin.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 From 9f7025d84e14eeb6ac009ebe0134a150679bf924 Mon Sep 17 00:00:00 2001 From: Ayman-Barghout Date: Mon, 21 Dec 2020 16:54:04 +0200 Subject: [PATCH 2/3] [Android] Bluetooth devices are now connected by default when setting `enableSpeakerphone` to false. --- .../webrtc/utils/RTCAudioManager.java | 24 +++++++++++-------- .../webrtc/utils/RTCBluetoothManager.java | 6 +++++ 2 files changed, 20 insertions(+), 10 deletions(-) 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..d1554148bc 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; @@ -404,9 +404,13 @@ private void unregisterReceiver(BroadcastReceiver receiver) { /** Sets the speaker phone mode. */ public void setSpeakerphoneOn(boolean on) { - boolean wasOn = audioManager.isSpeakerphoneOn(); - 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; From 03852236441c4f930713301abe119111c6cf9a10 Mon Sep 17 00:00:00 2001 From: Ayman-Barghout Date: Mon, 21 Dec 2020 17:06:05 +0200 Subject: [PATCH 3/3] [Android] Re-added the guard if-statement for checking if passing the same value which was removed by mistake. --- .../java/com/cloudwebrtc/webrtc/utils/RTCAudioManager.java | 4 ++++ 1 file changed, 4 insertions(+) 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 d1554148bc..475059116d 100644 --- a/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCAudioManager.java +++ b/android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCAudioManager.java @@ -404,6 +404,10 @@ private void unregisterReceiver(BroadcastReceiver receiver) { /** Sets the speaker phone mode. */ public void setSpeakerphoneOn(boolean on) { + boolean wasOn = audioManager.isSpeakerphoneOn(); + if (wasOn == on) { + return; + } final RTCBluetoothManager.State btManagerState = bluetoothManager.getState(); final boolean isBTAvailable = btManagerState == RTCBluetoothManager.State.SCO_CONNECTED