Merge "print MTU size after get MTU request from iphone." into klp-modular-dev
diff --git a/bta/gatt/bta_gattc_act.c b/bta/gatt/bta_gattc_act.c
index bc23d5d..90e9504 100644
--- a/bta/gatt/bta_gattc_act.c
+++ b/bta/gatt/bta_gattc_act.c
@@ -2156,5 +2156,27 @@
         }
     }
 }
+
+/*******************************************************************************
+**
+** Function         bta_gattc_broadcast
+**
+** Description      Start or stop broadcasting
+**
+** Returns          void
+**
+********************************************************************************/
+void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
+{
+    tBTA_GATTC_RCB      *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
+    tBTA_GATTC          cb_data;
+    (void)(p_cb);
+
+    cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
+    cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start);
+
+    if (p_clreg && p_clreg->p_cback)
+        (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
+}
 #endif
 #endif
diff --git a/bta/gatt/bta_gattc_api.c b/bta/gatt/bta_gattc_api.c
index fff1a55..d264458 100644
--- a/bta/gatt/bta_gattc_api.c
+++ b/bta/gatt/bta_gattc_api.c
@@ -1056,5 +1056,31 @@
     return;
 }
 
+/*******************************************************************************
+**
+** Function         BTA_GATTC_Broadcast
+**
+** Description      Start broadcasting (non-connectable advertisements)
+**
+** Parameters       client_if: client interface.
+**                  start: to start or stop listening for connection
+**
+** Returns          void
+**
+*******************************************************************************/
+void BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if, BOOLEAN start)
+{
+    tBTA_GATTC_API_LISTEN  *p_buf;
+
+    if ((p_buf = (tBTA_GATTC_API_LISTEN *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL)
+    {
+        p_buf->hdr.event = BTA_GATTC_API_BROADCAST_EVT;
+        p_buf->client_if = client_if;
+        p_buf->start = start;
+        bta_sys_sendmsg(p_buf);
+    }
+    return;
+}
+
 #endif /* BTA_GATT_INCLUDED */
 
diff --git a/bta/gatt/bta_gattc_int.h b/bta/gatt/bta_gattc_int.h
index 1ff8f79..255090d 100644
--- a/bta/gatt/bta_gattc_int.h
+++ b/bta/gatt/bta_gattc_int.h
@@ -69,6 +69,7 @@
     BTA_GATTC_API_REG_EVT,
     BTA_GATTC_API_DEREG_EVT,
     BTA_GATTC_API_LISTEN_EVT,
+    BTA_GATTC_API_BROADCAST_EVT,
     BTA_GATTC_API_DISABLE_EVT,
     BTA_GATTC_ENC_CMPL_EVT
 };
@@ -484,6 +485,7 @@
 extern void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
 #if BLE_INCLUDED == TRUE
 extern void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
+extern void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
 #endif
 /* utility functions */
 extern tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda);
diff --git a/bta/gatt/bta_gattc_main.c b/bta/gatt/bta_gattc_main.c
index 650cf5e..638f2f9 100644
--- a/bta/gatt/bta_gattc_main.c
+++ b/bta/gatt/bta_gattc_main.c
@@ -385,6 +385,9 @@
         case BTA_GATTC_API_LISTEN_EVT:
             bta_gattc_listen(p_cb, (tBTA_GATTC_DATA *) p_msg);
             break;
+        case BTA_GATTC_API_BROADCAST_EVT:
+            bta_gattc_broadcast(p_cb, (tBTA_GATTC_DATA *) p_msg);
+            break;
 #endif
 
         case BTA_GATTC_ENC_CMPL_EVT:
diff --git a/bta/include/bta_gatt_api.h b/bta/include/bta_gatt_api.h
index 969a0d3..e5ce5a4 100644
--- a/bta/include/bta_gatt_api.h
+++ b/bta/include/bta_gatt_api.h
@@ -1031,6 +1031,21 @@
 BTA_API extern void BTA_GATTC_Listen(tBTA_GATTC_IF client_if, BOOLEAN start, BD_ADDR_PTR target_bda);
 
 /*******************************************************************************
+**
+** Function         BTA_GATTC_Broadcast
+**
+** Description      Start broadcasting (non-connectable advertisements)
+**
+** Parameters       client_if: client interface.
+**                  start: to start or stop listening for connection
+**
+** Returns          void
+**
+*******************************************************************************/
+BTA_API extern void BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if, BOOLEAN start);
+
+
+/*******************************************************************************
 **  BTA GATT Server API
 ********************************************************************************/
 
diff --git a/btif/include/btif_storage.h b/btif/include/btif_storage.h
index 632fc53..9a69b00 100644
--- a/btif/include/btif_storage.h
+++ b/btif/include/btif_storage.h
@@ -30,6 +30,7 @@
 #define BTIF_STORAGE_FILL_PROPERTY(p_prop, t, l, p_v) \
          (p_prop)->type = t;(p_prop)->len = l; (p_prop)->val = (p_v);
 
+#define  BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE 512
 
 /*******************************************************************************
 **  Functions
diff --git a/btif/src/btif_gatt_client.c b/btif/src/btif_gatt_client.c
index 10332b4..80c50d0 100644
--- a/btif/src/btif_gatt_client.c
+++ b/btif/src/btif_gatt_client.c
@@ -463,10 +463,31 @@
         case BTIF_GATT_OBSERVE_EVT:
         {
             btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param;
-            if (!btif_gattc_find_bdaddr(p_btif_cb->bd_addr.address))
+            uint8_t remote_name_len;
+            uint8_t *p_eir_remote_name=NULL;
+
+            p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
+                                         BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
+
+            if(p_eir_remote_name == NULL)
             {
-                btif_gattc_add_remote_bdaddr(p_btif_cb->bd_addr.address, p_btif_cb->addr_type);
-                btif_gattc_update_properties(p_btif_cb);
+                p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value,
+                                BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
+            }
+
+            if ((p_btif_cb->addr_type != BLE_ADDR_RANDOM) || (p_eir_remote_name))
+            {
+               if (!btif_gattc_find_bdaddr(p_btif_cb->bd_addr.address))
+               {
+                  static const char* exclude_filter[] =
+                        {"LinkKey", "LE_KEY_PENC", "LE_KEY_PID", "LE_KEY_PCSRK", "LE_KEY_LENC", "LE_KEY_LCSRK"};
+
+                  btif_gattc_add_remote_bdaddr(p_btif_cb->bd_addr.address, p_btif_cb->addr_type);
+                  btif_gattc_update_properties(p_btif_cb);
+                  btif_config_filter_remove("Remote", exclude_filter, sizeof(exclude_filter)/sizeof(char*),
+                  BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE);
+               }
+
             }
             HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb,
                       &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->value);
@@ -822,7 +843,7 @@
 #if (defined(BLE_PERIPHERAL_MODE_SUPPORT) && (BLE_PERIPHERAL_MODE_SUPPORT == TRUE))
             BTA_GATTC_Listen(p_cb->client_if, p_cb->start, NULL);
 #else
-            BTA_DmBleBroadcast(p_cb->start);
+            BTA_GATTC_Broadcast(p_cb->client_if, p_cb->start);
 #endif
             break;
 
diff --git a/btif/src/btif_storage.c b/btif/src/btif_storage.c
index 27f078f..8f41ce5 100644
--- a/btif/src/btif_storage.c
+++ b/btif/src/btif_storage.c
@@ -38,7 +38,7 @@
 #define LOG_TAG "BTIF_STORAGE"
 
 #include "btif_api.h"
-
+#include "btif_storage.h"
 #include "btif_util.h"
 #include "bd.h"
 #include "gki.h"
@@ -150,7 +150,6 @@
 #define BTIF_STORAGE_HL_APP_DATA     "hl_app_data_"
 #define BTIF_STORAGE_HL_APP_MDL_DATA "hl_app_mdl_data_"
 
-#define  BTIF_STORAGE_MAX_ALLOWED_REMOTE_DEVICE 512
 /************************************************************************************
 **  Local type definitions
 ************************************************************************************/
diff --git a/include/bt_target.h b/include/bt_target.h
index 23e4fdb..8950207 100644
--- a/include/bt_target.h
+++ b/include/bt_target.h
@@ -1407,6 +1407,10 @@
 #define LOCAL_BLE_CONTROLLER_ID         (1)
 #endif
 
+#ifndef BTM_BLE_PRIVACY_SPT
+#define BTM_BLE_PRIVACY_SPT      TRUE
+#endif
+
 /******************************************************************************
 **
 ** ATT/GATT Protocol/Profile Settings
diff --git a/stack/btm/btm_acl.c b/stack/btm/btm_acl.c
index 68ac35f..0a4ed1a 100644
--- a/stack/btm/btm_acl.c
+++ b/stack/btm/btm_acl.c
@@ -179,6 +179,59 @@
     /* If here, no BD Addr found */
     return(xx);
 }
+
+#if BTM_BLE_PRIVACY_SPT == TRUE
+/*******************************************************************************
+**
+** Function         btm_ble_get_acl_remote_addr
+**
+** Description      This function reads the active remote address used for the
+**                  connection.
+**
+** Returns          success return TRUE, otherwise FALSE.
+**
+*******************************************************************************/
+BOOLEAN btm_ble_get_acl_remote_addr(tBTM_SEC_DEV_REC *p_dev_rec, BD_ADDR conn_addr,
+                                    tBLE_ADDR_TYPE *p_addr_type)
+{
+#if BLE_INCLUDED == TRUE
+    BOOLEAN         st = TRUE;
+
+    if (p_dev_rec == NULL)
+    {
+        BTM_TRACE_ERROR0("btm_ble_get_acl_remote_addr can not find device with matching address");
+        return FALSE;
+    }
+
+    switch (p_dev_rec->ble.active_addr_type)
+    {
+    case BTM_BLE_ADDR_PSEUDO:
+        memcpy(conn_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
+        * p_addr_type = p_dev_rec->ble.ble_addr_type;
+        break;
+
+    case BTM_BLE_ADDR_RRA:
+        memcpy(conn_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
+        * p_addr_type = BLE_ADDR_RANDOM;
+        break;
+
+    case BTM_BLE_ADDR_STATIC:
+        memcpy(conn_addr, p_dev_rec->ble.static_addr, BD_ADDR_LEN);
+        * p_addr_type = p_dev_rec->ble.static_addr_type;
+        break;
+
+    default:
+        BTM_TRACE_ERROR1("Unknown active address: %d", p_dev_rec->ble.active_addr_type);
+        st = FALSE;
+        break;
+    }
+
+    return st;
+#else
+    return FALSE;
+#endif
+}
+#endif
 /*******************************************************************************
 **
 ** Function         btm_acl_created
@@ -298,6 +351,10 @@
             /* If here, features are not known yet */
             if (p_dev_rec && is_le_link)
             {
+#if BTM_BLE_PRIVACY_SPT == TRUE
+                btm_ble_get_acl_remote_addr (p_dev_rec, p->active_remote_addr,
+                    &p->active_remote_addr_type);
+#endif
                 btm_establish_continue(p);
 
 #if (!defined(BTA_SKIP_BLE_READ_REMOTE_FEAT) || BTA_SKIP_BLE_READ_REMOTE_FEAT == FALSE)
diff --git a/stack/btm/btm_ble.c b/stack/btm/btm_ble.c
index 6a933bf..5b61b76 100644
--- a/stack/btm/btm_ble.c
+++ b/stack/btm/btm_ble.c
@@ -35,6 +35,9 @@
 #include "btm_ble_api.h"
 #include "smp_api.h"
 #include "l2c_int.h"
+#if (defined BLE_BRCM_INCLUDED && BLE_BRCM_INCLUDED == TRUE)
+#include "brcm_ble.h"
+#endif
 #include "gap_api.h"
 #include "bt_utils.h"
 
@@ -341,12 +344,24 @@
 {
     BOOLEAN         st = TRUE;
     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(pseudo_addr);
+#if BTM_BLE_PRIVACY_SPT == TRUE
+    tACL_CONN       *p = btm_bda_to_acl (pseudo_addr);
 
+    if (p == NULL)
+    {
+        BTM_TRACE_ERROR0("BTM_ReadRemoteConnectionAddr can not find matching address");
+        return FALSE;
+    }
+
+    memcpy(conn_addr, p->active_remote_addr, BD_ADDR_LEN);
+    *p_addr_type = p->active_remote_addr_type;
+#else
     memcpy(conn_addr, pseudo_addr, BD_ADDR_LEN);
     if (p_dev_rec != NULL)
     {
         *p_addr_type = p_dev_rec->ble.ble_addr_type;
     }
+#endif
     return st;
 }
 /*******************************************************************************
@@ -1462,6 +1477,61 @@
     return callback_rc;
 }
 
+#if (BTM_BLE_PRIVACY_SPT == TRUE )
+/*******************************************************************************
+**
+** Function         btm_ble_resolve_random_addr_on_conn_cmpl
+**
+** Description      resolve random address complete on connection complete event.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void btm_ble_resolve_random_addr_on_conn_cmpl(void * p_rec, void *p_data)
+{
+    UINT8   *p = (UINT8 *)p_data;
+    tBTM_SEC_DEV_REC    *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
+    UINT8       role, status, bda_type;
+    UINT16      handle;
+    BD_ADDR     bda;
+    UINT16      conn_interval, conn_latency, conn_timeout;
+    BOOLEAN     match = FALSE;
+
+    STREAM_TO_UINT8   (status, p);
+    STREAM_TO_UINT16   (handle, p);
+    STREAM_TO_UINT8    (role, p);
+    STREAM_TO_UINT8    (bda_type, p);
+    STREAM_TO_BDADDR   (bda, p);
+    STREAM_TO_UINT16   (conn_interval, p);
+    STREAM_TO_UINT16   (conn_latency, p);
+    STREAM_TO_UINT16   (conn_timeout, p);
+
+    handle = HCID_GET_HANDLE (handle);
+
+    BTM_TRACE_EVENT0 ("btm_ble_resolve_random_addr_master_cmpl");
+
+    if (match_rec)
+    {
+        BTM_TRACE_ERROR0("Random match");
+        match = TRUE;
+        match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
+        memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
+        memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
+    }
+    else
+    {
+        BTM_TRACE_ERROR0("Random unmatch");
+    }
+
+    btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
+
+    l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
+                      conn_latency, conn_timeout);
+
+    return;
+}
+#endif
+
 /*******************************************************************************
 **
 ** Function         btm_ble_connected
@@ -1516,9 +1586,18 @@
     p_dev_rec->hci_handle = handle;
     p_dev_rec->ble.ble_addr_type = addr_type;
 
+    p_dev_rec->role_master = FALSE;
     if (role == HCI_ROLE_MASTER)
         p_dev_rec->role_master = TRUE;
 
+#if (defined BTM_BLE_PRIVACY_SPT && BTM_BLE_PRIVACY_SPT == TRUE)
+    if (!addr_matched)
+        p_dev_rec->ble.active_addr_type = BTM_BLE_ADDR_PSEUDO;
+
+    if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_RANDOM && !addr_matched)
+        memcpy(p_dev_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
+#endif
+
     if (role == HCI_ROLE_SLAVE)
         p_cb->inq_var.adv_mode  = BTM_BLE_ADV_DISABLE;
     p_cb->inq_var.directed_conn = FALSE;
@@ -1534,6 +1613,9 @@
 ******************************************************************************/
 void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len)
 {
+#if (BTM_BLE_PRIVACY_SPT == TRUE )
+    UINT8       *p_data = p;
+#endif
     UINT8       role, status, bda_type;
     UINT16      handle;
     BD_ADDR     bda;
@@ -1549,21 +1631,53 @@
 
     if (status == 0)
     {
-        STREAM_TO_UINT16   (conn_interval, p);
-        STREAM_TO_UINT16   (conn_latency, p);
-        STREAM_TO_UINT16   (conn_timeout, p);
-        handle = HCID_GET_HANDLE (handle);
+#if (BTM_BLE_PRIVACY_SPT == TRUE )
+        /* possiblly receive connection complete with resolvable random on
+           slave role while the device has been paired */
+        if (!match && BTM_BLE_IS_RESOLVE_BDA(bda))
+        {
+            btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_conn_cmpl, p_data);
+        }
+        else
+#endif
+        {
+            STREAM_TO_UINT16   (conn_interval, p);
+            STREAM_TO_UINT16   (conn_latency, p);
+            STREAM_TO_UINT16   (conn_timeout, p);
+            handle = HCID_GET_HANDLE (handle);
 
-        btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
-        l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
+            btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
+            l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
                               conn_latency, conn_timeout);
+        }
     }
     else
     {
         role = HCI_ROLE_UNKNOWN;
 
         if (status == HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT)
+        {
             btm_ble_dir_adv_tout();
+        }
+        /* this is to work around broadcom firmware problem to handle
+         * unsolicited command complete event for HCI_LE_Create_Connection_Cancel
+         * and LE connection complete event with status error code (0x2)
+         * unknown connection identifier from bluetooth controller
+         * the workaround is to release the HCI connection to avoid out of sync
+         * with bluetooth controller, which cause BT can't be turned off.
+        */
+        else if ((status == HCI_ERR_NO_CONNECTION) &&
+                 (btm_ble_get_conn_st() != BLE_CONN_CANCEL))
+        {
+            tL2C_LCB    *p_lcb;
+            handle = HCID_GET_HANDLE (handle);
+            p_lcb = l2cu_find_lcb_by_handle (handle);
+            if (p_lcb != NULL)
+            {
+                l2c_link_hci_disc_comp (handle, HCI_ERR_PEER_USER);
+                btm_sec_disconnected (handle, HCI_ERR_PEER_USER);
+            }
+        }
     }
 
     btm_ble_set_conn_st(BLE_CONN_IDLE);
diff --git a/stack/btm/btm_ble_gap.c b/stack/btm/btm_ble_gap.c
index 5e5b5db..6ab0415 100644
--- a/stack/btm/btm_ble_gap.c
+++ b/stack/btm/btm_ble_gap.c
@@ -288,6 +288,49 @@
 #endif
 }
 
+#if BTM_BLE_PRIVACY_SPT == TRUE
+/*******************************************************************************
+**
+** Function         btm_ble_resolve_random_addr_on_adv
+**
+** Description      resolve random address complete callback.
+**
+** Returns          void
+**
+*******************************************************************************/
+static void btm_ble_resolve_random_addr_on_adv(void * p_rec, void *p)
+{
+    tBTM_SEC_DEV_REC    *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
+    UINT8       addr_type = BLE_ADDR_RANDOM;
+    BD_ADDR     bda;
+    UINT8       *pp = (UINT8 *)p + 1;
+    UINT8           evt_type;
+
+    BTM_TRACE_EVENT0 ("btm_ble_resolve_random_addr_on_adv ");
+
+    STREAM_TO_UINT8    (evt_type, pp);
+    STREAM_TO_UINT8    (addr_type, pp);
+    STREAM_TO_BDADDR   (bda, pp);
+
+    if (match_rec)
+    {
+        BTM_TRACE_ERROR0("Random match");
+        match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
+        memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
+        memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
+        addr_type = match_rec->ble.ble_addr_type;
+    }
+    else
+    {
+        BTM_TRACE_ERROR0("Random unmatch");
+    }
+
+    btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, pp);
+
+    return;
+}
+#endif
+
 /*******************************************************************************
 **
 ** Function         BTM_BleSetBgConnType
@@ -1672,6 +1715,9 @@
     BD_ADDR             bda;
     UINT8               evt_type = 0, *p = p_data;
     UINT8               addr_type = 0;
+#if (defined BTM_BLE_PRIVACY_SPT && BTM_BLE_PRIVACY_SPT == TRUE)
+    BOOLEAN             match = FALSE;
+#endif
 
     /* always get one device at a time */
     p ++;
@@ -1698,7 +1744,19 @@
          btm_cb.ble_ctr_cb.p_select_cback == NULL))
         return;
 
-    btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, p);
+#if (defined BTM_BLE_PRIVACY_SPT && BTM_BLE_PRIVACY_SPT == TRUE)
+#if SMP_INCLUDED == TRUE
+    /* always do RRA resolution on host */
+    if (!match && BTM_BLE_IS_RESOLVE_BDA(bda))
+    {
+        btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_adv, p_data);
+    }
+    else
+#endif
+#endif
+    {
+        btm_ble_process_adv_pkt_cont(bda, addr_type, evt_type, p);
+    }
 }
 
 /*******************************************************************************
diff --git a/stack/btm/btm_int.h b/stack/btm/btm_int.h
index b030c2a..5129efc 100644
--- a/stack/btm/btm_int.h
+++ b/stack/btm/btm_int.h
@@ -493,6 +493,14 @@
     tBLE_ADDR_TYPE      ble_addr_type;  /* LE device type: public or random address */
     tBLE_ADDR_TYPE      static_addr_type;   /* static address type */
     BD_ADDR             static_addr;    /* static address */
+#if BTM_BLE_PRIVACY_SPT == TRUE
+    BD_ADDR             cur_rand_addr;  /* current random address */
+
+#define BTM_BLE_ADDR_PSEUDO         0   /* address index device record */
+#define BTM_BLE_ADDR_RRA            1   /* cur_rand_addr */
+#define BTM_BLE_ADDR_STATIC         2   /* static_addr  */
+    UINT8               active_addr_type;
+#endif
 
 #if SMP_INCLUDED == TRUE
     tBTM_LE_KEY_TYPE    key_type;       /* bit mask of valid key types in record */
diff --git a/stack/l2cap/l2c_ble.c b/stack/l2cap/l2c_ble.c
index 2f2646e..ba0d7ec 100644
--- a/stack/l2cap/l2c_ble.c
+++ b/stack/l2cap/l2c_ble.c
@@ -616,6 +616,14 @@
     init_addr_type = p_lcb->ble_addr_type;
     memcpy(init_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
 
+#if BTM_BLE_PRIVACY_SPT == TRUE
+    if (p_dev_rec->ble.active_addr_type == BTM_BLE_ADDR_RRA)
+    {
+        init_addr_type = BLE_ADDR_RANDOM;
+        memcpy(init_addr, p_dev_rec->ble.cur_rand_addr, BD_ADDR_LEN);
+    }
+#endif
+
     if (!btsnd_hcic_ble_create_ll_conn (scan_int,/* UINT16 scan_int      */
                                         scan_win, /* UINT16 scan_win      */
                                         FALSE,                   /* UINT8 white_list     */