Bluetooth: Consider Connecting TWSP devices for allowing connections

Consider connecting TWSP devices for allowing connections

CRs-fixed: 2536354

Change-Id: I912c3acd84f449b6f967d6ef2c7beaebcaeef05c
diff --git a/src/com/android/bluetooth/hfp/HeadsetService.java b/src/com/android/bluetooth/hfp/HeadsetService.java
index 0ca6734..fce6d43 100644
--- a/src/com/android/bluetooth/hfp/HeadsetService.java
+++ b/src/com/android/bluetooth/hfp/HeadsetService.java
@@ -861,13 +861,14 @@
         return null;
     }
 
-    private BluetoothDevice getConnectedTwspDevice() {
-        List<BluetoothDevice> connDevices = getConnectedDevices();
+    private BluetoothDevice getConnectedOrConnectingTwspDevice() {
+        List<BluetoothDevice> connDevices =
+            getAllDevicesMatchingConnectionStates(CONNECTING_CONNECTED_STATES);
         int size = connDevices.size();
         for(int i = 0; i < size; i++) {
             BluetoothDevice ConnectedDevice = connDevices.get(i);
             if (mAdapterService.isTwsPlusDevice(ConnectedDevice)) {
-                logD("getConnectedTwspDevice: found" + ConnectedDevice);
+                logD("getConnectedorConnectingTwspDevice: found" + ConnectedDevice);
                 return ConnectedDevice;
             }
         }
@@ -905,12 +906,14 @@
         if (connDevices.size() == 0) {
             allowSecondHfConnection = true;
         } else {
-            BluetoothDevice connectedTwspDev = getConnectedTwspDevice();
-            if (connectedTwspDev != null) {
+            BluetoothDevice connectedOrConnectingTwspDev =
+                    getConnectedOrConnectingTwspDevice();
+            if (connectedOrConnectingTwspDev != null) {
                 // There is TWSP connected earbud
                 if (adapterService.isTwsPlusDevice(device)) {
                    if (adapterService.getTwsPlusPeerAddress
-                           (device).equals(connectedTwspDev.getAddress())) {
+                           (device).equals(
+                             connectedOrConnectingTwspDev.getAddress())) {
                        //Allow connection only if the outgoing
                        //is peer of TWS connected earbud
                        allowSecondHfConnection = true;
@@ -919,7 +922,8 @@
                    }
                 } else {
                    reservedSlotForTwspPeer = 0;
-                   if (getTwsPlusConnectedPeer(connectedTwspDev) == null) {
+                   if (getTwsPlusConnectedPeer(
+                                connectedOrConnectingTwspDev) == null) {
                        //Peer of Connected Tws+ device is not Connected
                        //yet, reserve one slot
                        reservedSlotForTwspPeer = 1;
@@ -965,8 +969,9 @@
                      "is"+ adapterService.isTwsPlusDevice(device));
             Log.v(TAG, "TWS Peer Addr: " +
                       adapterService.getTwsPlusPeerAddress(device));
-            if (connectedTwspDev != null) {
-                Log.v(TAG, "Connected device" + connectedTwspDev.getAddress());
+            if (connectedOrConnectingTwspDev != null) {
+                Log.v(TAG, "Connected or Connecting device"
+                         + connectedOrConnectingTwspDev.getAddress());
             } else {
                 Log.v(TAG, "No Connected TWSP devices");
             }