8000 Bluetooth switching enabled when switching `enableSpeakerphone` valu… · next-coder/flutter-webrtc@da12051 · GitHub
[go: up one dir, main page]

Skip to content

Commit da12051

Browse files
Bluetooth switching enabled when switching enableSpeakerphone value (if they are connected). flutter-webrtc#201 (flutter-webrtc#435)
* [iOS] Bluetooth devices are now allowed when setting `enableSpeakerphone` to false. * [Android] Bluetooth devices are now connected by default when setting `enableSpeakerphone` to false. * [Android] Re-added the guard if-statement for checking if passing the same value which was removed by mistake. Co-authored-by: Ayman-Barghout <ayman.a.barghout@gmail.com>
1 parent bb4dafd commit da12051

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCAudioManager.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,16 +352,16 @@ private void setAudioDeviceInternal(AudioDevice device) {
352352
public void setDefaultAudioDevice(AudioDevice defaultDevice) {
353353
ThreadUtils.checkIsOnMainThread();
354354
switch (defaultDevice) {
355+
case EARPIECE:
356+
if (hasEarpiece()) {
357+
defaultAudioDevice = defaultDevice;
358+
} else {
359+
defaultAudioDevice = AudioDevice.SPEAKER_PHONE;
360+
}
361+
break;
355362
case SPEAKER_PHONE:
356363
defaultAudioDevice = defaultDevice;
357364
break;
358-
case EARPIECE:
359-
if (hasEarpiece()) {
360-
defaultAudioDevice = defaultDevice;
361-
} else {
362-
defaultAudioDevice = AudioDevice.SPEAKER_PHONE;
363-
}
364-
break;
365365
default:
366366
Log.e(TAG, "Invalid default audio device selection");
367367
break;
@@ -408,6 +408,14 @@ public void setSpeakerphoneOn(boolean on) {
408408
if (wasOn == on) {
409409
return 8000 ;
410410
}
411+
final RTCBluetoothManager.State btManagerState = bluetoothManager.getState();
412+
final boolean isBTAvailable =
413+
btManagerState == RTCBluetoothManager.State.SCO_CONNECTED
414+
|| btManagerState == RTCBluetoothManager.State.SCO_CONNECTING
415+
|| btManagerState == RTCBluetoothManager.State.HEADSET_AVAILABLE;
416+
if(!on && isBTAvailable){
417+
bluetoothManager.startScoAudio();
418+
}
411419
audioManager.setSpeakerphoneOn(on);
412420
}
413421

android/src/main/java/com/cloudwebrtc/webrtc/utils/RTCBluetoothManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ public boolean startScoAudio() {
324324
Log.e(TAG, "BT SCO connection fails - no more attempts");
325325
return false;
326326
}
327+
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
328+
if (!devices.isEmpty()) {
329+
bluetoothDevice = devices.get(0);
330+
bluetoothState = State.HEADSET_AVAILABLE;
331+
}
332+
327333
if (bluetoothState != State.HEADSET_AVAILABLE) {
328334
Log.e(TAG, "BT SCO connection fails - no headset available");
329335
return false;

common/darwin/Classes/FlutterWebRTCPlugin.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult) result
559559
_speakerOn = enable.boolValue;
560560
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
561561
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
562-
withOptions:_speakerOn ? AVAudioSessionCategoryOptionDefaultToSpeaker : 0
563-
error:nil];
562+
withOptions:_speakerOn ? AVAudioSessionCategoryOptionDefaultToSpeaker
563+
:
564+
AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP
565+
error:nil];
564566
[audioSession setActive:YES error:nil];
565567
result(nil);
566568
#else

0 commit comments

Comments
 (0)
0