Skip to content

Commit a9406c5

Browse files
authored
[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
1 parent 6268139 commit a9406c5

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

android/src/main/java/com/zxcpoiu/incallmanager/AppRTC/AppRTCBluetoothManager.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
package com.zxcpoiu.incallmanager.AppRTC;
1111
import android.annotation.SuppressLint;
12+
import android.bluetooth.BluetoothClass;
1213
import android.bluetooth.BluetoothAdapter;
1314
import android.bluetooth.BluetoothDevice;
1415
import android.bluetooth.BluetoothHeadset;
@@ -31,6 +32,7 @@
3132

3233
import java.util.List;
3334
import java.util.Set;
35+
import java.util.ArrayList;
3436
import com.zxcpoiu.incallmanager.AppRTC.AppRTCUtils;
3537
import com.zxcpoiu.incallmanager.AppRTC.ThreadUtils;
3638
import com.zxcpoiu.incallmanager.InCallManagerModule;
@@ -41,9 +43,9 @@
4143
public class AppRTCBluetoothManager {
4244
private static final String TAG = "AppRTCBluetoothManager";
4345
// Timeout interval for starting or stopping audio to a Bluetooth SCO device.
44-
private static final int BLUETOOTH_SCO_TIMEOUT_MS = 4000;
46+
private static final int BLUETOOTH_SCO_TIMEOUT_MS = 6000;
4547
// Maximum number of SCO connection attempts.
46-
private static final int MAX_SCO_CONNECTION_ATTEMPTS = 2;
48+
private static final int MAX_SCO_CONNECTION_ATTEMPTS = 10;
4749
// Bluetooth connection state.
4850
public enum State {
4951
// Bluetooth is not available; no adapter or Bluetooth is off.
@@ -391,6 +393,19 @@ public boolean startScoAudio() {
391393
}
392394
return true;
393395
}
396+
private List<BluetoothDevice> getFinalConnectedDevices() {
397+
List<BluetoothDevice> connectedDevices = bluetoothHeadset.getConnectedDevices();
398+
List<BluetoothDevice> finalDevices = new ArrayList<BluetoothDevice>();
399+
400+
for (BluetoothDevice device : connectedDevices) {
401+
int majorClass = device.getBluetoothClass().getMajorDeviceClass();
402+
403+
if (majorClass == BluetoothClass.Device.Major.AUDIO_VIDEO) {
404+
finalDevices.add(device);
405+
}
406+
}
407+
return finalDevices;
408+
}
394409
/** Stops Bluetooth SCO connection with remote device. */
395410
public void stopScoAudio() {
396411
ThreadUtils.checkIsOnMainThread();
@@ -436,7 +451,7 @@ public void updateDevice() {
436451
// Get connected devices for the headset profile. Returns the set of
437452
// devices which are in state STATE_CONNECTED. The BluetoothDevice class
438453
// is just a thin wrapper for a Bluetooth hardware address.
439-
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
454+
List<BluetoothDevice> devices = getFinalConnectedDevices();
440455
if (devices.isEmpty()) {
441456
bluetoothDevice = null;
442457
bluetoothState = State.HEADSET_UNAVAILABLE;
@@ -491,7 +506,7 @@ protected void logBluetoothAdapterInfo(BluetoothAdapter localAdapter) {
491506
if (!pairedDevices.isEmpty()) {
492507
Log.d(TAG, "paired devices:");
493508
for (BluetoothDevice device : pairedDevices) {
494-
Log.d(TAG, " name=" + device.getName() + ", address=" + device.getAddress());
509+
Log.d(TAG, " name=" + device.getName() + ", address=" + device.getAddress() + ", deviceClass=" + String.valueOf(device.getBluetoothClass().getDeviceClass()) + ", deviceMajorClass=" + String.valueOf(device.getBluetoothClass().getMajorDeviceClass()));
495510
}
496511
}
497512
}
@@ -534,7 +549,7 @@ private void bluetoothTimeout() {
534549
}
535550
// Bluetooth SCO should be connecting; check the latest result.
536551
boolean scoConnected = false;
537-
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
552+
List<BluetoothDevice> devices = getFinalConnectedDevices();
538553
if (devices.size() > 0) {
539554
bluetoothDevice = devices.get(0);
540555
if (bluetoothHeadset.isAudioConnected(bluetoothDevice)) {

0 commit comments

Comments
 (0)