Bolster Bluetooth headset detection for Android 11 and older.

This commit is contained in:
Nicholas Tinsley 2023-05-30 15:00:49 -04:00
parent 05edc715ef
commit 83e84228f5
2 changed files with 10 additions and 3 deletions

View file

@ -3282,7 +3282,7 @@ public class ConversationParentFragment extends Fragment
@Override
public void onRecorderStarted() {
final AudioManagerCompat audioManager = ApplicationDependencies.getAndroidCallAudioManager();
if (audioManager.isBluetoothAvailable()) {
if (audioManager.isBluetoothHeadsetAvailable()) {
connectToBluetoothAndBeginRecording();
} else {
Log.d(TAG, "Recording from phone mic because no bluetooth devices were available.");

View file

@ -1,6 +1,8 @@
package org.thoughtcrime.securesms.webrtc.audio;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothHeadset;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.AudioAttributes;
@ -52,11 +54,16 @@ public abstract class AudioManagerCompat {
audioManager.stopBluetoothSco();
}
public boolean isBluetoothAvailable() {
public boolean isBluetoothHeadsetAvailable() {
if (Build.VERSION.SDK_INT >= 31) {
return audioManager.getAvailableCommunicationDevices().stream().anyMatch(it -> AudioDeviceMapping.fromPlatformType(it.getType()) == SignalAudioManager.AudioDevice.BLUETOOTH);
} else {
return isBluetoothScoAvailableOffCall();
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
return mBluetoothAdapter != null &&
mBluetoothAdapter.isEnabled() &&
// noinspection MissingPermission
mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothAdapter.STATE_CONNECTED &&
isBluetoothScoAvailableOffCall();
}
}