Skip to content

Commit 111abcd

Browse files
authored
Add Android Bluetooth Support (#606)
* Ini test android bluetooth * Moved audio device setting to own function * Remove setSpeakerPhoneOn calls in audio type check
1 parent 7cbf102 commit 111abcd

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

android/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
1515
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
1616
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
17+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
18+
<uses-permission android:name="android.permission.BLUETOOTH"/>
1719
</manifest>

android/src/main/java/com/twiliorn/library/CustomTwilioVideoView.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import android.content.Intent;
1818
import android.content.IntentFilter;
1919
import android.media.AudioAttributes;
20+
import android.media.AudioDeviceInfo;
2021
import android.media.AudioFocusRequest;
2122
import android.media.AudioManager;
2223
import android.os.Build;
@@ -417,6 +418,7 @@ public void onHostDestroy() {
417418

418419
if (localAudioTrack != null) {
419420
localAudioTrack.release();
421+
audioManager.stopBluetoothSco();
420422
localAudioTrack = null;
421423
}
422424

@@ -553,6 +555,29 @@ public void connectToRoom() {
553555
room = Video.connect(getContext(), connectOptionsBuilder.build(), roomListener());
554556
}
555557

558+
public void setAudioType() {
559+
AudioDeviceInfo[] devicesInfo = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
560+
boolean hasNonSpeakerphoneDevice = false;
561+
for (int i = 0; i < devicesInfo.length; i++) {
562+
int deviceType = devicesInfo[i].getType();
563+
if (
564+
deviceType == AudioDeviceInfo.TYPE_WIRED_HEADSET ||
565+
deviceType == AudioDeviceInfo.TYPE_WIRED_HEADPHONES
566+
) {
567+
hasNonSpeakerphoneDevice = true;
568+
}
569+
if (
570+
deviceType == AudioDeviceInfo.TYPE_BLUETOOTH_A2DP ||
571+
deviceType == AudioDeviceInfo.TYPE_BLUETOOTH_SCO
572+
) {
573+
audioManager.startBluetoothSco();
574+
audioManager.setBluetoothScoOn(true);
575+
hasNonSpeakerphoneDevice = true;
576+
}
577+
}
578+
audioManager.setSpeakerphoneOn(!hasNonSpeakerphoneDevice);
579+
}
580+
556581
private void setAudioFocus(boolean focus) {
557582
if (focus) {
558583
previousAudioMode = audioManager.getMode();
@@ -581,7 +606,7 @@ private void setAudioFocus(boolean focus) {
581606
* speaker mode if this is not set.
582607
*/
583608
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
584-
audioManager.setSpeakerphoneOn(!audioManager.isWiredHeadsetOn());
609+
setAudioType();
585610
getContext().registerReceiver(myNoisyAudioStreamReceiver, intentFilter);
586611

587612
} else {
@@ -610,7 +635,7 @@ private class BecomingNoisyReceiver extends BroadcastReceiver {
610635
public void onReceive(Context context, Intent intent) {
611636
// audioManager.setSpeakerphoneOn(true);
612637
if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
613-
audioManager.setSpeakerphoneOn(!audioManager.isWiredHeadsetOn());
638+
setAudioType();
614639
}
615640
}
616641
}
@@ -629,10 +654,12 @@ public void disconnect() {
629654
if (localAudioTrack != null) {
630655
localAudioTrack.release();
631656
localAudioTrack = null;
657+
audioManager.stopBluetoothSco();
632658
}
633659
if (localVideoTrack != null) {
634660
localVideoTrack.release();
635661
localVideoTrack = null;
662+
audioManager.stopBluetoothSco();
636663
}
637664
setAudioFocus(false);
638665
if (cameraCapturer != null) {

0 commit comments

Comments
 (0)