From a9406c5eb6181977ef9c1c4e4e6c53815d179dde Mon Sep 17 00:00:00 2001 From: Marq Roldan Date: Fri, 26 Jul 2024 16:29:54 +0800 Subject: [PATCH] [android] Ensure connected devices are part of AUDIO_VIDEO (Device.Major) Right now, querying of connected devices doesn't filter out irrelevant devices (i.e. watch, etc) This causes `BLUETOOTH` to be added to the audio devices even though it will never connect --- .../AppRTC/AppRTCBluetoothManager.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/zxcpoiu/incallmanager/AppRTC/AppRTCBluetoothManager.java b/android/src/main/java/com/zxcpoiu/incallmanager/AppRTC/AppRTCBluetoothManager.java index dd9f852..a37f611 100644 --- a/android/src/main/java/com/zxcpoiu/incallmanager/AppRTC/AppRTCBluetoothManager.java +++ b/android/src/main/java/com/zxcpoiu/incallmanager/AppRTC/AppRTCBluetoothManager.java @@ -9,6 +9,7 @@ */ package com.zxcpoiu.incallmanager.AppRTC; import android.annotation.SuppressLint; +import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothHeadset; @@ -31,6 +32,7 @@ import java.util.List; import java.util.Set; +import java.util.ArrayList; import com.zxcpoiu.incallmanager.AppRTC.AppRTCUtils; import com.zxcpoiu.incallmanager.AppRTC.ThreadUtils; import com.zxcpoiu.incallmanager.InCallManagerModule; @@ -41,9 +43,9 @@ public class AppRTCBluetoothManager { private static final String TAG = "AppRTCBluetoothManager"; // Timeout interval for starting or stopping audio to a Bluetooth SCO device. - private static final int BLUETOOTH_SCO_TIMEOUT_MS = 4000; + private static final int BLUETOOTH_SCO_TIMEOUT_MS = 6000; // Maximum number of SCO connection attempts. - private static final int MAX_SCO_CONNECTION_ATTEMPTS = 2; + private static final int MAX_SCO_CONNECTION_ATTEMPTS = 10; // Bluetooth connection state. public enum State { // Bluetooth is not available; no adapter or Bluetooth is off. @@ -391,6 +393,19 @@ public boolean startScoAudio() { } return true; } + private List getFinalConnectedDevices() { + List connectedDevices = bluetoothHeadset.getConnectedDevices(); + List finalDevices = new ArrayList(); + + for (BluetoothDevice device : connectedDevices) { + int majorClass = device.getBluetoothClass().getMajorDeviceClass(); + + if (majorClass == BluetoothClass.Device.Major.AUDIO_VIDEO) { + finalDevices.add(device); + } + } + return finalDevices; + } /** Stops Bluetooth SCO connection with remote device. */ public void stopScoAudio() { ThreadUtils.checkIsOnMainThread(); @@ -436,7 +451,7 @@ public void updateDevice() { // Get connected devices for the headset profile. Returns the set of // devices which are in state STATE_CONNECTED. The BluetoothDevice class // is just a thin wrapper for a Bluetooth hardware address. - List devices = bluetoothHeadset.getConnectedDevices(); + List devices = getFinalConnectedDevices(); if (devices.isEmpty()) { bluetoothDevice = null; bluetoothState = State.HEADSET_UNAVAILABLE; @@ -491,7 +506,7 @@ protected void logBluetoothAdapterInfo(BluetoothAdapter localAdapter) { if (!pairedDevices.isEmpty()) { Log.d(TAG, "paired devices:"); for (BluetoothDevice device : pairedDevices) { - Log.d(TAG, " name=" + device.getName() + ", address=" + device.getAddress()); + Log.d(TAG, " name=" + device.getName() + ", address=" + device.getAddress() + ", deviceClass=" + String.valueOf(device.getBluetoothClass().getDeviceClass()) + ", deviceMajorClass=" + String.valueOf(device.getBluetoothClass().getMajorDeviceClass())); } } } @@ -534,7 +549,7 @@ private void bluetoothTimeout() { } // Bluetooth SCO should be connecting; check the latest result. boolean scoConnected = false; - List devices = bluetoothHeadset.getConnectedDevices(); + List devices = getFinalConnectedDevices(); if (devices.size() > 0) { bluetoothDevice = devices.get(0); if (bluetoothHeadset.isAudioConnected(bluetoothDevice)) {