|
9 | 9 | */ |
10 | 10 | package com.zxcpoiu.incallmanager.AppRTC; |
11 | 11 | import android.annotation.SuppressLint; |
| 12 | +import android.bluetooth.BluetoothClass; |
12 | 13 | import android.bluetooth.BluetoothAdapter; |
13 | 14 | import android.bluetooth.BluetoothDevice; |
14 | 15 | import android.bluetooth.BluetoothHeadset; |
|
31 | 32 |
|
32 | 33 | import java.util.List; |
33 | 34 | import java.util.Set; |
| 35 | +import java.util.ArrayList; |
34 | 36 | import com.zxcpoiu.incallmanager.AppRTC.AppRTCUtils; |
35 | 37 | import com.zxcpoiu.incallmanager.AppRTC.ThreadUtils; |
36 | 38 | import com.zxcpoiu.incallmanager.InCallManagerModule; |
|
41 | 43 | public class AppRTCBluetoothManager { |
42 | 44 | private static final String TAG = "AppRTCBluetoothManager"; |
43 | 45 | // 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; |
45 | 47 | // 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; |
47 | 49 | // Bluetooth connection state. |
48 | 50 | public enum State { |
49 | 51 | // Bluetooth is not available; no adapter or Bluetooth is off. |
@@ -391,6 +393,19 @@ public boolean startScoAudio() { |
391 | 393 | } |
392 | 394 | return true; |
393 | 395 | } |
| 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 | + } |
394 | 409 | /** Stops Bluetooth SCO connection with remote device. */ |
395 | 410 | public void stopScoAudio() { |
396 | 411 | ThreadUtils.checkIsOnMainThread(); |
@@ -436,7 +451,7 @@ public void updateDevice() { |
436 | 451 | // Get connected devices for the headset profile. Returns the set of |
437 | 452 | // devices which are in state STATE_CONNECTED. The BluetoothDevice class |
438 | 453 | // is just a thin wrapper for a Bluetooth hardware address. |
439 | | - List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices(); |
| 454 | + List<BluetoothDevice> devices = getFinalConnectedDevices(); |
440 | 455 | if (devices.isEmpty()) { |
441 | 456 | bluetoothDevice = null; |
442 | 457 | bluetoothState = State.HEADSET_UNAVAILABLE; |
@@ -491,7 +506,7 @@ protected void logBluetoothAdapterInfo(BluetoothAdapter localAdapter) { |
491 | 506 | if (!pairedDevices.isEmpty()) { |
492 | 507 | Log.d(TAG, "paired devices:"); |
493 | 508 | 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())); |
495 | 510 | } |
496 | 511 | } |
497 | 512 | } |
@@ -534,7 +549,7 @@ private void bluetoothTimeout() { |
534 | 549 | } |
535 | 550 | // Bluetooth SCO should be connecting; check the latest result. |
536 | 551 | boolean scoConnected = false; |
537 | | - List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices(); |
| 552 | + List<BluetoothDevice> devices = getFinalConnectedDevices(); |
538 | 553 | if (devices.size() > 0) { |
539 | 554 | bluetoothDevice = devices.get(0); |
540 | 555 | if (bluetoothHeadset.isAudioConnected(bluetoothDevice)) { |
|
0 commit comments