Merge "Bluetooth: Add PXP profile reconnection  after link loss"
diff --git a/src/org/codeaurora/bluetooth/pxpservice/DeviceProperties.java b/src/org/codeaurora/bluetooth/pxpservice/DeviceProperties.java
index c903cd7..5df9025 100644
--- a/src/org/codeaurora/bluetooth/pxpservice/DeviceProperties.java
+++ b/src/org/codeaurora/bluetooth/pxpservice/DeviceProperties.java
@@ -65,6 +65,8 @@
 
     public boolean connectionState = false;
 
+    public boolean AddedToWhitelist = false;
+
     public boolean hasIasService = false;
 
     public boolean hasLlsService = false;
@@ -91,6 +93,7 @@
         isReading = false;
         isAlerting = false;
         connectionState = false;
+        AddedToWhitelist = false;
         hasIasService = false;
         hasLlsService = false;
         hasTxpService = false;
diff --git a/src/org/codeaurora/bluetooth/pxpservice/PxpMonitorService.java b/src/org/codeaurora/bluetooth/pxpservice/PxpMonitorService.java
index 6d5b1be..59614f2 100644
--- a/src/org/codeaurora/bluetooth/pxpservice/PxpMonitorService.java
+++ b/src/org/codeaurora/bluetooth/pxpservice/PxpMonitorService.java
@@ -166,8 +166,19 @@
                 deviceProp.hasLlsService = false;
                 deviceProp.hasIasService = false;
                 deviceProp.hasTxpService = false;
+                String address = leDevice.getAddress();
 
-                broadcastUpdate(LINKLOSS_ALERT, leDevice);
+                if (deviceProp.deviceAddress != null && address.equals(deviceProp.deviceAddress)
+                    && deviceProp.gatt != null) {
+
+                   Log.e(TAG, "Trying to use an existing mBluetoothGatt for connection.");
+                   if(deviceProp.AddedToWhitelist == false) {
+                       boolean ret_val = deviceProp.gatt.connect();
+                       deviceProp.connectionState = true;
+                       deviceProp.AddedToWhitelist = true;
+                   }
+                   broadcastUpdate(LINKLOSS_ALERT, leDevice);
+               }
             }
         }
 
@@ -642,22 +653,10 @@
 
         } else {
 
-            if (deviceProp.deviceAddress != null && address.equals(deviceProp.deviceAddress)
-                    && deviceProp.gatt != null) {
+            if (deviceProp.deviceAddress != null && address.equals(deviceProp.deviceAddress)) {
 
-                Log.e(TAG, "Trying to use an existing mBluetoothGatt for connection.");
-                boolean status = deviceProp.gatt.connect();
 
-                if (status == true) {
-                    Log.d(TAG, "Gatt reconnected.");
-                    deviceProp.connectionState = true;
-                    return true;
-
-                } else {
-                    Log.e(TAG, "Failed to reconnect.");
-                    deviceProp.connectionState = false;
-                    return false;
-                }
+                deviceProp.gatt = leDevice.connectGatt(this, false, mGattCallback);
 
             }
         }
@@ -688,6 +687,11 @@
         deviceProp.qcRssiProximityMonitor.close();
 
         deviceProp.gatt.disconnect();
+        if (deviceProp.gatt != null) {
+            deviceProp.gatt.close();
+            deviceProp.gatt = null;
+        }
+
     }
 
     /**