Avoid race condition that gets PanService stuck in STATE_DISCONNECTED.

http://b/12788418

Change-Id: I7cc43db047457eb6a4ac501770a714eef0ed6422
diff --git a/src/com/android/bluetooth/pan/PanService.java b/src/com/android/bluetooth/pan/PanService.java
index 36add63..682fe2d 100755
--- a/src/com/android/bluetooth/pan/PanService.java
+++ b/src/com/android/bluetooth/pan/PanService.java
@@ -421,6 +421,17 @@
             prevState = panDevice.mState;
             ifaceAddr = panDevice.mIfaceAddr;
         }
+
+        // Avoid race condition that gets this class stuck in STATE_DISCONNECTING. While we
+        // are in STATE_CONNECTING, if a BluetoothPan#disconnect call comes in, the original
+        // connect call will put us in STATE_DISCONNECTED. Then, the disconnect completes and
+        // changes the state to STATE_DISCONNECTING. All future calls to BluetoothPan#connect
+        // will fail until the caller explicitly calls BluetoothPan#disconnect.
+        if (prevState == BluetoothProfile.STATE_DISCONNECTED && state == BluetoothProfile.STATE_DISCONNECTING) {
+            Log.d(TAG, "Ignoring state change from " + prevState + " to " + state);
+            return;
+        }
+
         Log.d(TAG, "handlePanDeviceStateChange preState: " + prevState + " state: " + state);
         if (prevState == state) return;
         if (remote_role == BluetoothPan.LOCAL_PANU_ROLE) {