LE: Add devices to the inquiry db before connecting

Ensure that the remote device type is set correctly by reading the
device information from non-volatile storage and (re-)adding it to
the inquiry database before connecting to a remote GATT device.
bug 11573208
Change-Id: Ifa69da3d5871aed5293ea5e611144874b67afe72
diff --git a/btif/include/btif_gatt_util.h b/btif/include/btif_gatt_util.h
index 2f952b5..74ac6bc 100644
--- a/btif/include/btif_gatt_util.h
+++ b/btif/include/btif_gatt_util.h
@@ -20,6 +20,8 @@
 #ifndef BTIF_GATT_UTIL_H
 #define BTIF_GATT_UTIL_H
 
+#include "bta_api.h"
+
 void btif_to_bta_uuid(tBT_UUID *p_dest, bt_uuid_t *p_src);
 void btif_to_bta_gatt_id(tBTA_GATT_ID *p_dest, btgatt_gatt_id_t *p_src);
 void btif_to_bta_srvc_id(tBTA_GATT_SRVC_ID *p_dest, btgatt_srvc_id_t *p_src);
@@ -34,5 +36,7 @@
 
 void btif_gatt_check_encrypted_link(BD_ADDR bd_addr);
 
+BOOLEAN btif_get_device_type(BD_ADDR bd_addr, int *addr_type, int *device_type);
+
 #endif
 
diff --git a/btif/src/btif_gatt_client.c b/btif/src/btif_gatt_client.c
index ad33c51..ce51ce2 100644
--- a/btif/src/btif_gatt_client.c
+++ b/btif/src/btif_gatt_client.c
@@ -597,11 +597,23 @@
             break;
 
         case BTIF_GATTC_OPEN:
+        {
+            // Ensure device is in inquiry database
+            int addr_type = 0;
+            int device_type = 0;
+
+            if (btif_get_device_type(p_cb->bd_addr.address, &addr_type, &device_type) == TRUE
+                  && device_type != BT_DEVICE_TYPE_BREDR)
+                BTA_DmAddBleDevice(p_cb->bd_addr.address, addr_type, device_type);
+
+            // Mark background connections
             if (!p_cb->is_direct)
                 BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
 
+            // Connect!
             BTA_GATTC_Open(p_cb->client_if, p_cb->bd_addr.address, p_cb->is_direct);
             break;
+        }
 
         case BTIF_GATTC_CLOSE:
             // Disconnect established connections
diff --git a/btif/src/btif_gatt_server.c b/btif/src/btif_gatt_server.c
index 6194302..9fc4fad 100644
--- a/btif/src/btif_gatt_server.c
+++ b/btif/src/btif_gatt_server.c
@@ -365,11 +365,24 @@
             break;
 
         case BTIF_GATTS_OPEN:
+        {
+            // Ensure device is in inquiry database
+            int addr_type = 0;
+            int device_type = 0;
+
+            if (btif_get_device_type(p_cb->bd_addr.address, &addr_type, &device_type) == TRUE
+                  && device_type != BT_DEVICE_TYPE_BREDR)
+                BTA_DmAddBleDevice(p_cb->bd_addr.address, addr_type, device_type);
+
+            // Mark background connections
             if (!p_cb->is_direct)
                 BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
+
+            // Connect!
             BTA_GATTS_Open(p_cb->server_if, p_cb->bd_addr.address,
                            p_cb->is_direct);
             break;
+        }
 
         case BTIF_GATTS_CLOSE:
             // Cancel pending foreground/background connections
diff --git a/btif/src/btif_gatt_util.c b/btif/src/btif_gatt_util.c
index e53df00..ab56f99 100644
--- a/btif/src/btif_gatt_util.c
+++ b/btif/src/btif_gatt_util.c
@@ -36,6 +36,7 @@
 #include "btif_util.h"
 #include "btif_gatt.h"
 #include "btif_gatt_util.h"
+#include "btif_config.h"
 
 #if BTA_GATT_INCLUDED == TRUE
 
@@ -295,4 +296,29 @@
     }
 }
 
+/*******************************************************************************
+ * Device information
+ *******************************************************************************/
+
+BOOLEAN btif_get_device_type(BD_ADDR bd_addr, int *addr_type, int *device_type)
+{
+    if (device_type == NULL || addr_type == NULL)
+        return FALSE;
+
+    bt_bdaddr_t bda;
+    bdcpy(bda.address, bd_addr);
+
+    char bd_addr_str[18] = {0};
+    bd2str(&bda, &bd_addr_str);
+
+    if (!btif_config_get_int("Remote", bd_addr_str, "DevType", device_type))
+        return FALSE;
+
+    if (!btif_config_get_int("Remote", bd_addr_str, "AddrType", addr_type))
+        return FALSE;
+
+    ALOGD("%s: Device [%s] type %d, addr. type %d", __FUNCTION__, bd_addr_str, *device_type, *addr_type);
+    return TRUE;
+}
+
 #endif
diff --git a/stack/btm/btm_ble.c b/stack/btm/btm_ble.c
index 24fd197..6f2f454 100644
--- a/stack/btm/btm_ble.c
+++ b/stack/btm/btm_ble.c
@@ -596,8 +596,10 @@
         {
             *p_dev_type = p_inq_info->results.device_type ;
             *p_addr_type = p_inq_info->results.ble_addr_type;
+        } else {
+            /* unknown device, assume BR/EDR */
+            BTM_TRACE_DEBUG0 ("btm_find_dev_type - unknown device, BR/EDR assumed");
         }
-        /* unknown device, assume BR/EDR */
     }
     else /* there is a security device record exisitng */
     {