Fix "no instances found for adv" when stop BLE scanning.

Bug:17703806
Change-Id: I103d3637ad38d33b4370850a300b0d72bdaeb562
diff --git a/btif/include/btif_gatt_multi_adv_util.h b/btif/include/btif_gatt_multi_adv_util.h
index e6c26a0..1175958 100644
--- a/btif/include/btif_gatt_multi_adv_util.h
+++ b/btif/include/btif_gatt_multi_adv_util.h
@@ -72,8 +72,8 @@
 } btgatt_multi_adv_common_data;
 
 extern btgatt_multi_adv_common_data *btif_obtain_multi_adv_data_cb();
-extern void btif_gattc_init_multi_adv_cb(void);
-extern void btif_gattc_destroy_multi_adv_cb();
+extern void btif_gattc_incr_app_count(void);
+extern void btif_gattc_decr_app_count(void);
 extern int btif_multi_adv_add_instid_map(int client_if, int inst_id,
         BOOLEAN gen_temp_instid);
 extern int btif_multi_adv_instid_for_clientif(int client_if);
diff --git a/btif/src/btif_gatt_client.c b/btif/src/btif_gatt_client.c
index abe35c1..5699649 100644
--- a/btif/src/btif_gatt_client.c
+++ b/btif/src/btif_gatt_client.c
@@ -1087,13 +1087,13 @@
     {
         case BTIF_GATTC_REGISTER_APP:
             btif_to_bta_uuid(&uuid, &p_cb->uuid);
-            btif_gattc_init_multi_adv_cb();
+            btif_gattc_incr_app_count();
             BTA_GATTC_AppRegister(&uuid, bta_gattc_cback);
             break;
 
         case BTIF_GATTC_UNREGISTER_APP:
             btif_gattc_clear_clientif(p_cb->client_if);
-            btif_gattc_destroy_multi_adv_cb();
+            btif_gattc_decr_app_count();
             BTA_GATTC_AppDeregister(p_cb->client_if);
             break;
 
diff --git a/btif/src/btif_gatt_multi_adv_util.c b/btif/src/btif_gatt_multi_adv_util.c
index b8f9233..3d7d96c 100644
--- a/btif/src/btif_gatt_multi_adv_util.c
+++ b/btif/src/btif_gatt_multi_adv_util.c
@@ -42,7 +42,7 @@
 /*******************************************************************************
 **  Static variables
 ********************************************************************************/
-static int multi_adv_enable_count = 0;
+static int user_app_count = 0;
 static btgatt_multi_adv_common_data *p_multi_adv_com_data_cb = NULL;
 
 btgatt_multi_adv_common_data *btif_obtain_multi_adv_data_cb()
@@ -52,9 +52,15 @@
         max_adv_inst = 1;
 
     BTIF_TRACE_DEBUG("%s, Count:%d", __FUNCTION__, max_adv_inst);
+    if(0 == BTM_BleMaxMultiAdvInstanceCount())
+    {
+        BTIF_TRACE_WARNING("BTM_BleMaxMultiAdvInstanceCount - No instances found");
+        return NULL;
+    }
+
+    BTIF_TRACE_DEBUG("BTM_BleMaxMultiAdvInstanceCount count:%d", BTM_BleMaxMultiAdvInstanceCount());
     if (NULL == p_multi_adv_com_data_cb)
     {
-        BTIF_TRACE_DEBUG("Initializing in %s", __FUNCTION__);
         p_multi_adv_com_data_cb = GKI_getbuf(sizeof(btgatt_multi_adv_common_data));
         if (NULL != p_multi_adv_com_data_cb)
         {
@@ -82,27 +88,24 @@
     return p_multi_adv_com_data_cb;
 }
 
-void btif_gattc_init_multi_adv_cb(void)
+void btif_gattc_incr_app_count(void)
 {
     // TODO: Instead of using a fragile reference counter here, one could
     //       simply track the client_if instances that are in the map.
-    ++multi_adv_enable_count;
+    ++user_app_count;
 }
 
-void btif_gattc_destroy_multi_adv_cb(int client_if)
+void btif_gattc_decr_app_count(void)
 {
-    if (multi_adv_enable_count > 0)
-        multi_adv_enable_count --;
+    if (user_app_count > 0)
+        user_app_count --;
 
-    if(multi_adv_enable_count == 0 && p_multi_adv_com_data_cb != 0)
+    if(user_app_count == 0 && NULL != p_multi_adv_com_data_cb)
     {
-        if (NULL != p_multi_adv_com_data_cb)
-        {
-            GKI_freebuf (p_multi_adv_com_data_cb->clntif_map);
-            GKI_freebuf (p_multi_adv_com_data_cb->inst_cb);
-            GKI_freebuf(p_multi_adv_com_data_cb);
-            p_multi_adv_com_data_cb = NULL;
-        }
+       GKI_freebuf (p_multi_adv_com_data_cb->clntif_map);
+       GKI_freebuf (p_multi_adv_com_data_cb->inst_cb);
+       GKI_freebuf(p_multi_adv_com_data_cb);
+       p_multi_adv_com_data_cb = NULL;
     }
 }