espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.51k stars 7.26k forks source link

BLE gatt client connection loss, BT_APPL: bta_gattc_conn_cback() - cif=1 connected=0 conn_id=1 reason=0x003e #2810

Closed an-erd closed 5 years ago

an-erd commented 5 years ago

Environment

Problem Description

I used the code from the IDF from esp-idf/examples/bluetooth/gatt_client/main/ and only made the following changes:

complete code used


/*
   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

/****************************************************************************
*
* This file is for gatt client. It can scan ble device, connect one device.
* Run the gatt_server demo, the client demo will automatically connect to the gatt_server demo.
* Client demo will enable gatt_server's notify after connection. Then the two devices will exchange
* data.
*
****************************************************************************/

#include "../build/include/sdkconfig.h"     // TODO: nicer way to do it?
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "nvs.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gattc_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
#include "esp_gatt_common_api.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"

#define GATTC_TAG "GATTC_DEMO"
#define REMOTE_NOTIFY_CHAR_CUSTOM_UUID    0xff00
#define PROFILE_NUM      1
#define PROFILE_A_APP_ID 0
#define INVALID_HANDLE   0

static const char remote_device_name[] = "MilestonePod";
static bool connect    = false;
static bool get_server = false;
static esp_gattc_char_elem_t *char_elem_result   = NULL;
static esp_gattc_descr_elem_t *descr_elem_result = NULL;

/* eclare static functions */
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);

static esp_bt_uuid_t remote_filter_service_uuid = {
    .len = ESP_UUID_LEN_16,
    .uuid = {.uuid16 = ESP_GATT_UUID_RUNNING_SPEED_CADENCE_SVC,},
};

static esp_bt_uuid_t remote_filter_char_uuid = {
    .len = ESP_UUID_LEN_16,
    .uuid = {.uuid16 = ESP_GATT_UUID_RSC_MEASUREMENT,},
};

static esp_bt_uuid_t remote_filter_char_custom_uuid = {
    .len = ESP_UUID_LEN_16,
    .uuid = {.uuid16 = REMOTE_NOTIFY_CHAR_CUSTOM_UUID,},
};

static esp_bt_uuid_t notify_descr_uuid = {
    .len = ESP_UUID_LEN_16,
    .uuid = {.uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG,},
};

static esp_ble_scan_params_t ble_scan_params = {
    .scan_type              = BLE_SCAN_TYPE_ACTIVE,
    .own_addr_type          = BLE_ADDR_TYPE_PUBLIC,
    .scan_filter_policy     = BLE_SCAN_FILTER_ALLOW_ALL,
    .scan_interval          = 0x800, // 0x50,
    .scan_window            = 0x600,// 0x30,
    .scan_duplicate         = BLE_SCAN_DUPLICATE_ENABLE     // TODO: check, was BLE_SCAN_DUPLICATE_DISABLE
};

struct gattc_profile_inst {
    esp_gattc_cb_t gattc_cb;
    uint16_t gattc_if;
    uint16_t app_id;
    uint16_t conn_id;
    uint16_t service_start_handle;
    uint16_t service_end_handle;
    uint16_t char_handle;
    esp_bd_addr_t remote_bda;
};

/* One gatt-based profile one app_id and one gattc_if, this array will store the gattc_if returned by ESP_GATTS_REG_EVT */
static struct gattc_profile_inst gl_profile_tab[PROFILE_NUM] = {
    [PROFILE_A_APP_ID] = {
        .gattc_cb = gattc_profile_event_handler,
        .gattc_if = ESP_GATT_IF_NONE,       /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
    },
};

static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
{
    esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param;

    switch (event) {
    case ESP_GATTC_REG_EVT:
        ESP_LOGI(GATTC_TAG, "REG_EVT");
        esp_err_t scan_ret = esp_ble_gap_set_scan_params(&ble_scan_params);
        if (scan_ret){
            ESP_LOGE(GATTC_TAG, "set scan params error, error code = %x", scan_ret);
        }
        break;
    case ESP_GATTC_CONNECT_EVT:{
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d", p_data->connect.conn_id, gattc_if);
        gl_profile_tab[PROFILE_A_APP_ID].conn_id = p_data->connect.conn_id;
        memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t));
        ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
        esp_log_buffer_hex(GATTC_TAG, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
        esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->connect.conn_id);
        if (mtu_ret){
            ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret);
        }
        break;
    }
    case ESP_GATTC_OPEN_EVT:
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_OPEN_EVT");
        if (param->open.status != ESP_GATT_OK){
            ESP_LOGE(GATTC_TAG, "open failed, status %d", p_data->open.status);
            break;
        }
        ESP_LOGI(GATTC_TAG, "open success");
        break;
    case ESP_GATTC_CFG_MTU_EVT:
        if (param->cfg_mtu.status != ESP_GATT_OK){
            ESP_LOGE(GATTC_TAG,"config mtu failed, error status = %x", param->cfg_mtu.status);
        }
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_CFG_MTU_EVT, Status %d, MTU %d, conn_id %d", param->cfg_mtu.status, param->cfg_mtu.mtu, param->cfg_mtu.conn_id);
        esp_ble_gattc_search_service(gattc_if, param->cfg_mtu.conn_id, &remote_filter_service_uuid);
        break;
    case ESP_GATTC_SEARCH_RES_EVT: {
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_SEARCH_RES_EVT");
        ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x is primary service %d", p_data->search_res.conn_id, p_data->search_res.is_primary);
        ESP_LOGI(GATTC_TAG, "start handle %d end handle %d current handle value %d", p_data->search_res.start_handle, p_data->search_res.end_handle, p_data->search_res.srvc_id.inst_id);
        if (p_data->search_res.srvc_id.uuid.len == ESP_UUID_LEN_16 && p_data->search_res.srvc_id.uuid.uuid.uuid16 == ESP_GATT_UUID_RUNNING_SPEED_CADENCE_SVC) {
            ESP_LOGI(GATTC_TAG, "service found");
            get_server = true;
            gl_profile_tab[PROFILE_A_APP_ID].service_start_handle = p_data->search_res.start_handle;
            gl_profile_tab[PROFILE_A_APP_ID].service_end_handle = p_data->search_res.end_handle;
            ESP_LOGI(GATTC_TAG, "UUID16: %x", p_data->search_res.srvc_id.uuid.uuid.uuid16);
        }
        break;
    }
    case ESP_GATTC_SEARCH_CMPL_EVT:
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_SEARCH_CMPL_EVT");
        if (p_data->search_cmpl.status != ESP_GATT_OK){
            ESP_LOGE(GATTC_TAG, "search service failed, error status = %x", p_data->search_cmpl.status);
            break;
        }
        if(p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_REMOTE_DEVICE) {
            ESP_LOGI(GATTC_TAG, "Get service information from remote device");
        } else if (p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_NVS_FLASH) {
            ESP_LOGI(GATTC_TAG, "Get service information from flash");
        } else {
            ESP_LOGI(GATTC_TAG, "unknown service source");
        }
        if (get_server){
            uint16_t count = 0;
            esp_gatt_status_t status = esp_ble_gattc_get_attr_count( gattc_if,
                                                                     p_data->search_cmpl.conn_id,
                                                                     ESP_GATT_DB_CHARACTERISTIC,
                                                                     gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
                                                                     gl_profile_tab[PROFILE_A_APP_ID].service_end_handle,
                                                                     INVALID_HANDLE,
                                                                     &count);
            if (status != ESP_GATT_OK){
                ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error");
            }

            if (count > 0){
                char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count);
                if (!char_elem_result){
                    ESP_LOGE(GATTC_TAG, "gattc no mem");
                }else{
                    status = esp_ble_gattc_get_char_by_uuid( gattc_if,
                                                             p_data->search_cmpl.conn_id,
                                                             gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
                                                             gl_profile_tab[PROFILE_A_APP_ID].service_end_handle,
                                                             remote_filter_char_uuid,
                                                             char_elem_result,
                                                             &count);
                    if (status != ESP_GATT_OK){
                        ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error");
                    }

                    /*  Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */
                    if (count > 0 && (char_elem_result[0].properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)){
                        ESP_LOGI(GATTC_TAG, "esp_ble_gattc_write_char_descr() if %d, handle %d",
                                             gattc_if,
                                             char_elem_result[0].char_handle);
                        ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
                        esp_log_buffer_hex(GATTC_TAG, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t));

                        gl_profile_tab[PROFILE_A_APP_ID].char_handle = char_elem_result[0].char_handle;
                        esp_ble_gattc_register_for_notify (gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, char_elem_result[0].char_handle);
                    }
                }
                /* free char_elem_result */
                free(char_elem_result);
            }else{
                ESP_LOGE(GATTC_TAG, "no char found");
            }
        }
         break;
    case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_REG_FOR_NOTIFY_EVT");
        if (p_data->reg_for_notify.status != ESP_GATT_OK){
            ESP_LOGE(GATTC_TAG, "REG FOR NOTIFY failed: error status = %d", p_data->reg_for_notify.status);
        }
        else{
            uint16_t count = 0;
            static uint16_t notify_en = 0x1;
            esp_gatt_status_t ret_status = esp_ble_gattc_get_attr_count( gattc_if,
                                                                         gl_profile_tab[PROFILE_A_APP_ID].conn_id,
                                                                         ESP_GATT_DB_DESCRIPTOR,
                                                                         gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
                                                                         gl_profile_tab[PROFILE_A_APP_ID].service_end_handle,
                                                                         gl_profile_tab[PROFILE_A_APP_ID].char_handle,
                                                                         &count);
            if (ret_status != ESP_GATT_OK){
                ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error");
            }
            if (count > 0){
                descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count);
                if (!descr_elem_result){
                    ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem");
                }else{
                    ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if,
                                                                         gl_profile_tab[PROFILE_A_APP_ID].conn_id,
                                                                         p_data->reg_for_notify.handle,
                                                                         notify_descr_uuid,
                                                                         descr_elem_result,
                                                                         &count);
                    if (ret_status != ESP_GATT_OK){
                        ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error");
                    }
                    /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */
                    if (count > 0 && descr_elem_result[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){
                        ESP_LOGI(GATTC_TAG, "esp_ble_gattc_write_char_descr() if %d, conn_id %d, handle %d, size %u, value %u, type_rsp %u, auth req %u",
                                                                     gattc_if,
                                                                     gl_profile_tab[PROFILE_A_APP_ID].conn_id,
                                                                     descr_elem_result[0].handle,
                                                                     sizeof(notify_en),
                                                                     notify_en, //  (uint8_t*) &notify_en,
                                                                     ESP_GATT_WRITE_TYPE_RSP,
                                                                     ESP_GATT_AUTH_REQ_NONE);

                        ret_status = esp_ble_gattc_write_char_descr( gattc_if,
                                                                     gl_profile_tab[PROFILE_A_APP_ID].conn_id,
                                                                     descr_elem_result[0].handle,
                                                                     sizeof(notify_en),
                                                                     (uint8_t*) &notify_en,
                                                                     ESP_GATT_WRITE_TYPE_RSP,
                                                                     ESP_GATT_AUTH_REQ_NONE);
                    }

                    if (ret_status != ESP_GATT_OK){
                        ESP_LOGE(GATTC_TAG, "esp_ble_gattc_write_char_descr error");
                    }

                    /* free descr_elem_result */
                    free(descr_elem_result);
                }
            }
            else{
                ESP_LOGE(GATTC_TAG, "decsr not found");
            }

        }
        break;
    }
    case ESP_GATTC_NOTIFY_EVT:
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT");
        if (p_data->notify.is_notify){
            ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, receive notify value:");
        }else{
            ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, receive indicate value:");
        }
        esp_log_buffer_hex(GATTC_TAG, p_data->notify.value, p_data->notify.value_len);
        break;
    case ESP_GATTC_WRITE_DESCR_EVT:
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_WRITE_DESCR_EVT");
        if (p_data->write.status != ESP_GATT_OK){
            ESP_LOGE(GATTC_TAG, "write descr failed, error status = %x", p_data->write.status);
            break;
        }
        ESP_LOGI(GATTC_TAG, "write descr success ");
        // uint8_t write_char_data[35];
        // for (int i = 0; i < sizeof(write_char_data); ++i)
        // {
        //     write_char_data[i] = i % 256;
        // }

        // ESP_LOGI(GATTC_TAG, "ESP_GATTC_WRITE_DESCR_EVT if %d, conn_id %d, char_handle %d, size %u, data %s",
        //                        gattc_if,
        //                        gl_profile_tab[PROFILE_A_APP_ID].conn_id,
        //                        gl_profile_tab[PROFILE_A_APP_ID].char_handle,
        //                           sizeof(write_char_data),
        //                           write_char_data);
        // esp_ble_gattc_write_char( gattc_if,
        //                           gl_profile_tab[PROFILE_A_APP_ID].conn_id,
        //                           gl_profile_tab[PROFILE_A_APP_ID].char_handle,
        //                           sizeof(write_char_data),
        //                           write_char_data,
        //                           ESP_GATT_WRITE_TYPE_RSP,
        //                           ESP_GATT_AUTH_REQ_NONE);
        break;
    case ESP_GATTC_SRVC_CHG_EVT: {
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_SRVC_CHG_EVT");
        esp_bd_addr_t bda;
        memcpy(bda, p_data->srvc_chg.remote_bda, sizeof(esp_bd_addr_t));
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_SRVC_CHG_EVT, bd_addr:");
        esp_log_buffer_hex(GATTC_TAG, bda, sizeof(esp_bd_addr_t));
        break;
    }
    case ESP_GATTC_WRITE_CHAR_EVT:
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_WRITE_CHAR_EVT");
        if (p_data->write.status != ESP_GATT_OK){
            ESP_LOGE(GATTC_TAG, "write char failed, error status = %x", p_data->write.status);
            break;
        }
        ESP_LOGI(GATTC_TAG, "write char success ");
        break;
    case ESP_GATTC_DISCONNECT_EVT:
        connect = false;
        get_server = false;
        ESP_LOGI(GATTC_TAG, "ESP_GATTC_DISCONNECT_EVT, reason = %d", p_data->disconnect.reason);
        break;
    default:
        break;
    }
}

static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
    uint8_t *adv_name = NULL;
    uint8_t adv_name_len = 0;
    switch (event) {
    case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: {
        ESP_LOGI(GATTC_TAG, "ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT");
        //the unit of the duration is second
        uint32_t duration = 30;
        esp_ble_gap_start_scanning(duration);
        break;
    }
    case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
        ESP_LOGI(GATTC_TAG, "ESP_GAP_BLE_SCAN_START_COMPLETE_EVT");

        //scan start complete event to indicate scan start successfully or failed
        if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
            ESP_LOGE(GATTC_TAG, "scan start failed, error status = %x", param->scan_start_cmpl.status);
            break;
        }
        ESP_LOGI(GATTC_TAG, "scan start success");

        break;
    case ESP_GAP_BLE_SCAN_RESULT_EVT: {
        ESP_LOGI(GATTC_TAG, "ESP_GAP_BLE_SCAN_RESULT_EVT");

        esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
        switch (scan_result->scan_rst.search_evt) {
        case ESP_GAP_SEARCH_INQ_RES_EVT:
            esp_log_buffer_hex(GATTC_TAG, scan_result->scan_rst.bda, 6);
            ESP_LOGI(GATTC_TAG, "searched Adv Data Len %d, Scan Response Len %d", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
            adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,
                                                ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
            ESP_LOGI(GATTC_TAG, "searched Device Name Len %d", adv_name_len);
            esp_log_buffer_char(GATTC_TAG, adv_name, adv_name_len);
            ESP_LOGI(GATTC_TAG, "\n");
            if (adv_name != NULL) {
                // TODO: better control of device we're searching for
                if (strlen(remote_device_name) <= adv_name_len && strncmp((char *)adv_name, remote_device_name, strlen(remote_device_name)) == 0) {
                    ESP_LOGI(GATTC_TAG, "searched device %s\n", remote_device_name);
                    if (connect == false) {
                        connect = true;
                        ESP_LOGI(GATTC_TAG, "connect to the remote device.");
                        esp_ble_gap_stop_scanning();
                        esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true);
                    }
                }
            }
            break;
        case ESP_GAP_SEARCH_INQ_CMPL_EVT:
            ESP_LOGI(GATTC_TAG, "ESP_GAP_SEARCH_INQ_CMPL_EVT");
            break;
        default:
            break;
        }
        break;
    }

    case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
        ESP_LOGI(GATTC_TAG, "ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT");

        if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){
            ESP_LOGE(GATTC_TAG, "scan stop failed, error status = %x", param->scan_stop_cmpl.status);
            break;
        }
        ESP_LOGI(GATTC_TAG, "stop scan successfully");
        break;

    case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
        ESP_LOGI(GATTC_TAG, "ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT");

        if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){
            ESP_LOGE(GATTC_TAG, "adv stop failed, error status = %x", param->adv_stop_cmpl.status);
            break;
        }
        ESP_LOGI(GATTC_TAG, "stop adv successfully");
        break;
    case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT:
        ESP_LOGI(GATTC_TAG, "ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT");
        ESP_LOGI(GATTC_TAG, "update connection params status = %d, min_int = %d, max_int = %d,conn_int = %d,latency = %d, timeout = %d",
                  param->update_conn_params.status,
                  param->update_conn_params.min_int,
                  param->update_conn_params.max_int,
                  param->update_conn_params.conn_int,
                  param->update_conn_params.latency,
                  param->update_conn_params.timeout);
        break;
    default:
        break;
    }
}

static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
{
    ESP_LOGI(GATTC_TAG, "esp_gattc_cb()");

    /* If event is register event, store the gattc_if for each profile */
    if (event == ESP_GATTC_REG_EVT) {
        if (param->reg.status == ESP_GATT_OK) {
            gl_profile_tab[param->reg.app_id].gattc_if = gattc_if;
        } else {
            ESP_LOGI(GATTC_TAG, "reg app failed, app_id %04x, status %d",
                    param->reg.app_id,
                    param->reg.status);
            return;
        }
    }

    /* If the gattc_if equal to profile A, call profile A cb handler,
     * so here call each profile's callback */
    do {
        int idx;
        for (idx = 0; idx < PROFILE_NUM; idx++) {
            if (gattc_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
                    gattc_if == gl_profile_tab[idx].gattc_if) {
                if (gl_profile_tab[idx].gattc_cb) {
                    gl_profile_tab[idx].gattc_cb(event, gattc_if, param);
                }
            }
        }
    } while (0);
}

void app_main()
{
    esp_log_level_set("phy_init", ESP_LOG_INFO);

    // Initialize NVS.
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK( ret );

    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    ret = esp_bt_controller_init(&bt_cfg);
    if (ret) {
        ESP_LOGE(GATTC_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
    if (ret) {
        ESP_LOGE(GATTC_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_bluedroid_init();
    if (ret) {
        ESP_LOGE(GATTC_TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
        return;
    }

    ret = esp_bluedroid_enable();
    if (ret) {
        ESP_LOGE(GATTC_TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
        return;
    }

    //register the callback function to the gap module
    ret = esp_ble_gap_register_callback(esp_gap_cb);
    if (ret){
        ESP_LOGE(GATTC_TAG, "%s gap register failed, error code = %x\n", __func__, ret);
        return;
    }

    //register the callback function to the gattc module
    ret = esp_ble_gattc_register_callback(esp_gattc_cb);
    if(ret){
        ESP_LOGE(GATTC_TAG, "%s gattc register failed, error code = %x\n", __func__, ret);
        return;
    }

    ret = esp_ble_gattc_app_register(PROFILE_A_APP_ID);
    if (ret){
        ESP_LOGE(GATTC_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret);
    }
    esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
    if (local_mtu_ret){
        ESP_LOGE(GATTC_TAG, "set local  MTU failed, error code = %x", local_mtu_ret);
    }

}

When I use the WEMOS LOLIN32 boards, everything is fine. I can connect to the service and get notifications. This works in about 99% of the trials. Only sometimes connecting to the service or finding the device does not work. Log file from this successful runs look like:


Toolchain path: /opt/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc
Toolchain version: crosstool-ng-1.22.0-80-g6c4433a5
Compiler version: 5.2.0
Python requirements from C:/msys32/home/AKAEM/esp/esp-idf/requirements.txt are satisfied.
MONITOR
--- idf_monitor on COM3 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6104
load:0x40078000,len:10084
load:0x40080400,len:6552
entry 0x40080764
I (28) boot: ESP-IDF v3.2-beta1-dirty 2nd stage bootloader
I (28) boot: compile time 10:01:56
I (29) boot: Enabling RNG early entropy source...
I (34) boot: SPI Speed      : 40MHz
I (38) boot: SPI Mode       : DIO
I (42) boot: SPI Flash Size : 4MB
I (46) boot: Partition Table:
I (50) boot: ## Label            Usage          Type ST Offset   Length
I (57) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (64) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (72) boot:  2 factory          factory app      00 00 00010000 00100000
I (79) boot: End of partition table
I (84) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x22d98 (142744) map
I (142) esp_image: segment 1: paddr=0x00032dc0 vaddr=0x3ff80000 size=0x00000 (     0) load
I (143) esp_image: segment 2: paddr=0x00032dc8 vaddr=0x3ff80000 size=0x00000 (     0) load
I (149) esp_image: segment 3: paddr=0x00032dd0 vaddr=0x3ffbdb60 size=0x02c90 ( 11408) load
I (163) esp_image: segment 4: paddr=0x00035a68 vaddr=0x3ffc07f0 size=0x00000 (     0) load
I (167) esp_image: segment 5: paddr=0x00035a70 vaddr=0x40080000 size=0x00400 (  1024) load
0x40080000: _WindowOverflow4 at C:/msys32/home/AKAEM/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

I (176) esp_image: segment 6: paddr=0x00035e78 vaddr=0x40080400 size=0x0a198 ( 41368) load
I (202) esp_image: segment 7: paddr=0x00040018 vaddr=0x400d0018 size=0x6486c (411756) map
0x400d0018: _stext at ??:?

I (347) esp_image: segment 8: paddr=0x000a488c vaddr=0x4008a598 size=0x0751c ( 29980) load
0x4008a598: ram_write_gain_mem at ??:?

I (359) esp_image: segment 9: paddr=0x000abdb0 vaddr=0x400c0000 size=0x00000 (     0) load
I (360) esp_image: segment 10: paddr=0x000abdb8 vaddr=0x50000000 size=0x00000 (     0) load
I (366) esp_image: segment 11: paddr=0x000abdc0 vaddr=0x50000000 size=0x00000 (     0) load
I (386) boot: Loaded app from partition at offset 0x10000
I (386) boot: Disabling RNG early entropy source...
I (387) cpu_start: Pro cpu up.
I (391) cpu_start: Starting app cpu, entry point is 0x40081050
0x40081050: call_start_cpu1 at C:/msys32/home/AKAEM/esp/esp-idf/components/esp32/cpu_start.c:245

I (0) cpu_start: App cpu up.
D (401) memory_layout: Checking 11 reserved memory ranges:
D (406) memory_layout: Reserved memory range 0x3ffae000 - 0x3ffae6e0
D (413) memory_layout: Reserved memory range 0x3ffae6e0 - 0x3ffaff10
D (419) memory_layout: Reserved memory range 0x3ffb0000 - 0x3ffb6388
D (425) memory_layout: Reserved memory range 0x3ffb8000 - 0x3ffb9a20
D (432) memory_layout: Reserved memory range 0x3ffbdb28 - 0x3ffbdb5c
D (438) memory_layout: Reserved memory range 0x3ffbdb60 - 0x3ffc7598
D (445) memory_layout: Reserved memory range 0x3ffe0000 - 0x3ffe0440
D (451) memory_layout: Reserved memory range 0x3ffe3f20 - 0x3ffe4350
D (458) memory_layout: Reserved memory range 0x40070000 - 0x40078000
D (464) memory_layout: Reserved memory range 0x40078000 - 0x40080000
0x40080000: _WindowOverflow4 at C:/msys32/home/AKAEM/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

D (470) memory_layout: Reserved memory range 0x40080000 - 0x40091ab4
0x40080000: _WindowOverflow4 at C:/msys32/home/AKAEM/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

D (477) memory_layout: Building list of available memory regions:
D (483) memory_layout: Available memory region 0x3ffaff10 - 0x3ffb0000
D (490) memory_layout: Available memory region 0x3ffb6388 - 0x3ffb8000
D (496) memory_layout: Available memory region 0x3ffb9a20 - 0x3ffbdb28
D (503) memory_layout: Available memory region 0x3ffbdb5c - 0x3ffbdb60
D (509) memory_layout: Available memory region 0x3ffc7598 - 0x3ffc8000
D (516) memory_layout: Available memory region 0x3ffc8000 - 0x3ffca000
D (523) memory_layout: Available memory region 0x3ffca000 - 0x3ffcc000
D (529) memory_layout: Available memory region 0x3ffcc000 - 0x3ffce000
D (536) memory_layout: Available memory region 0x3ffce000 - 0x3ffd0000
D (542) memory_layout: Available memory region 0x3ffd0000 - 0x3ffd2000
D (549) memory_layout: Available memory region 0x3ffd2000 - 0x3ffd4000
D (556) memory_layout: Available memory region 0x3ffd4000 - 0x3ffd6000
D (562) memory_layout: Available memory region 0x3ffd6000 - 0x3ffd8000
D (569) memory_layout: Available memory region 0x3ffd8000 - 0x3ffda000
D (575) memory_layout: Available memory region 0x3ffda000 - 0x3ffdc000
D (582) memory_layout: Available memory region 0x3ffdc000 - 0x3ffde000
D (589) memory_layout: Available memory region 0x3ffde000 - 0x3ffe0000
D (595) memory_layout: Available memory region 0x3ffe0440 - 0x3ffe3f20
D (602) memory_layout: Available memory region 0x3ffe4350 - 0x3ffe8000
D (608) memory_layout: Available memory region 0x3ffe8000 - 0x3fff0000
D (615) memory_layout: Available memory region 0x3fff0000 - 0x3fff8000
D (621) memory_layout: Available memory region 0x3fff8000 - 0x3fffc000
D (628) memory_layout: Available memory region 0x3fffc000 - 0x40000000
D (635) memory_layout: Available memory region 0x40091ab4 - 0x40092000
D (641) memory_layout: Available memory region 0x40092000 - 0x40094000
D (648) memory_layout: Available memory region 0x40094000 - 0x40096000
D (654) memory_layout: Available memory region 0x40096000 - 0x40098000
D (661) memory_layout: Available memory region 0x40098000 - 0x4009a000
D (668) memory_layout: Available memory region 0x4009a000 - 0x4009c000
D (674) memory_layout: Available memory region 0x4009c000 - 0x4009e000
D (681) memory_layout: Available memory region 0x4009e000 - 0x400a0000
I (688) heap_init: Initializing. RAM available for dynamic allocation:
D (695) heap_init: New heap initialised at 0x3ffaff10
I (700) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
D (706) heap_init: New heap initialised at 0x3ffb6388
I (711) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
D (717) heap_init: New heap initialised at 0x3ffb9a20
I (722) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (728) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAM
D (734) heap_init: New heap initialised at 0x3ffc7598
I (740) heap_init: At 3FFC7598 len 00018A68 (98 KiB): DRAM
I (746) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (752) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
D (758) heap_init: New heap initialised at 0x40091ab4
I (764) heap_init: At 40091AB4 len 0000E54C (57 KiB): IRAM
I (770) cpu_start: Pro cpu start user code
D (782) clk: RTC_SLOW_CLK calibration value: 3268813
D (120) intr_alloc: Connected src 46 to int 2 (cpu 0)
D (121) intr_alloc: Connected src 57 to int 3 (cpu 0)
D (121) stack_chk: Intialize random stack guard
D (126) intr_alloc: Connected src 24 to int 9 (cpu 0)
I (131) cpu_start: Starting scheduler on PRO CPU.
D (0) intr_alloc: Connected src 25 to int 2 (cpu 1)
I (0) cpu_start: Starting scheduler on APP CPU.
D (146) heap_init: New heap initialised at 0x3ffe0440
D (156) heap_init: New heap initialised at 0x3ffe4350
D (166) intr_alloc: Connected src 16 to int 12 (cpu 0)
D (166) nvs: nvs_flash_init_custom partition=nvs start=9 count=6
D (216) BTDM_INIT: Release DRAM [0x3ffb2730] - [0x3ffb6388]
I (216) BTDM_INIT: BT controller compile version [6a842ec]
D (216) BTDM_INIT: .data initialise [0x3ffae6e0] <== [0x4000d890]
D (226) BTDM_INIT: .bss initialise [0x3ffb0000] - [0x3ffb09a8]
D (226) BTDM_INIT: .bss initialise [0x3ffb09a8] - [0x3ffb1ddc]
D (236) BTDM_INIT: .bss initialise [0x3ffb1ddc] - [0x3ffb2730]
D (236) BTDM_INIT: .bss initialise [0x3ffb8000] - [0x3ffb9a20]
D (246) BTDM_INIT: .bss initialise [0x3ffbdb28] - [0x3ffbdb5c]
I (256) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
D (266) nvs: nvs_open_from_partition phy 0
D (266) nvs: nvs_get cal_version 4
D (266) nvs: nvs_get_str_or_blob cal_mac
D (276) nvs: nvs_get_str_or_blob cal_data
D (286) nvs: nvs_close 1
I (346) phy: phy_version: 4000, b6198fa, Sep  3 2018, 15:11:06, 0, 0
I (686) GATTC_DEMO: esp_gattc_cb()
I (686) GATTC_DEMO: REG_EVT
I (686) GATTC_DEMO: ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT
I (696) GATTC_DEMO: ESP_GAP_BLE_SCAN_START_COMPLETE_EVT
I (696) GATTC_DEMO: scan start success
I (786) GATTC_DEMO: ESP_GAP_BLE_SCAN_RESULT_EVT
I (786) GATTC_DEMO: 43 bf 2b af c2 5c
I (786) GATTC_DEMO: searched Adv Data Len 14, Scan Response Len 0
I (786) GATTC_DEMO: searched Device Name Len 0
I (796) GATTC_DEMO:

I (6276) GATTC_DEMO: ESP_GAP_BLE_SCAN_RESULT_EVT
I (6276) GATTC_DEMO: 50 33 8b 1c ef f6
I (6276) GATTC_DEMO: searched Adv Data Len 9, Scan Response Len 26
I (6276) GATTC_DEMO: searched Device Name Len 15
I (6286) GATTC_DEMO: MilestonePod F6
I (6286) GATTC_DEMO:

I (6296) GATTC_DEMO: searched device MilestonePod

I (6296) GATTC_DEMO: connect to the remote device.
I (6306) GATTC_DEMO: ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT
I (6316) GATTC_DEMO: stop scan successfully
I (7336) GATTC_DEMO: esp_gattc_cb()
I (7336) GATTC_DEMO: ESP_GATTC_CONNECT_EVT conn_id 0, if 1
I (7336) GATTC_DEMO: REMOTE BDA:
I (7336) GATTC_DEMO: 50 33 8b 1c ef f6
I (7346) GATTC_DEMO: esp_gattc_cb()
I (7346) GATTC_DEMO: ESP_GATTC_OPEN_EVT
I (7356) GATTC_DEMO: open success
I (8526) GATTC_DEMO: esp_gattc_cb()
I (8526) GATTC_DEMO: ESP_GATTC_CFG_MTU_EVT, Status 0, MTU 23, conn_id 0
I (8526) GATTC_DEMO: esp_gattc_cb()
I (8526) GATTC_DEMO: ESP_GATTC_SEARCH_RES_EVT
I (8536) GATTC_DEMO: SEARCH RES: conn_id = 0 is primary service 1
I (8536) GATTC_DEMO: start handle 16 end handle 29 current handle value 16
I (8546) GATTC_DEMO: service found
I (8546) GATTC_DEMO: UUID16: 1814
I (8556) GATTC_DEMO: esp_gattc_cb()
I (8556) GATTC_DEMO: ESP_GATTC_SEARCH_CMPL_EVT
I (8566) GATTC_DEMO: Get service information from remote device
I (8566) GATTC_DEMO: esp_ble_gattc_write_char_descr() if 1, handle 18
I (8576) GATTC_DEMO: REMOTE BDA:
I (8586) GATTC_DEMO: 50 33 8b 1c ef f6
I (8586) GATTC_DEMO: esp_gattc_cb()
I (8586) GATTC_DEMO: ESP_GATTC_REG_FOR_NOTIFY_EVT
I (8596) GATTC_DEMO: esp_ble_gattc_write_char_descr() if 1, conn_id 0, handle 19, size 2, value 1, type_rsp 2, auth req 0
I (8636) GATTC_DEMO: esp_gattc_cb()
I (8636) GATTC_DEMO: ESP_GATTC_WRITE_DESCR_EVT
I (8636) GATTC_DEMO: write descr success
I (12436) GATTC_DEMO: ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT
I (12436) GATTC_DEMO: update connection params status = 0, min_int = 16, max_int = 32,conn_int = 32,latency = 0, timeout = 600
I (55836) GATTC_DEMO: esp_gattc_cb()
I (55836) GATTC_DEMO: ESP_GATTC_NOTIFY_EVT
I (55836) GATTC_DEMO: ESP_GATTC_NOTIFY_EVT, receive notify value:
I (55846) GATTC_DEMO: 03 0f 02 4b 51 00 10 00 00 00
I (57916) GATTC_DEMO: esp_gattc_cb()
I (57916) GATTC_DEMO: ESP_GATTC_NOTIFY_EVT
I (57916) GATTC_DEMO: ESP_GATTC_NOTIFY_EVT, receive notify value:
I (57926) GATTC_DEMO: 03 35 02 48 46 00 4c 00 00 00
I (60796) GATTC_DEMO: esp_gattc_cb()
I (60796) GATTC_DEMO: ESP_GATTC_NOTIFY_EVT
I (60796) GATTC_DEMO: ESP_GATTC_NOTIFY_EVT, receive notify value:
I (60806) GATTC_DEMO: 03 00 00 00 00 00 5d 00 00 00

When I used the M5Stack-FIRE device, I experience an issue for almost every try (~90-95%):

...
W (6655) BT_APPL: bta_gattc_conn_cback() - cif=1 connected=0 conn_id=1 reason=0x003e
I (6655) GATTC_DEMO: esp_gattc_cb()
I (6655) GATTC_DEMO: ESP_GATTC_DISCONNECT_EVT, reason = 62
I (6665) GATTC_DEMO: esp_gattc_cb()
I (6665) GATTC_DEMO: ESP_GATTC_OPEN_EVT
E (6675) GATTC_DEMO: open failed, status 133
...

Complete Log


rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5832
load:0x40078000,len:9176
load:0x40080000,len:6008
0x40080000: _WindowOverflow4 at C:/msys32/home/AKAEM/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

entry 0x4008032c
0x4008032c: _KernelExceptionVector at ??:?

I (28) boot: ESP-IDF v3.1.1 2nd stage bootloader
I (28) boot: compile time 20:56:11
I (28) boot: Enabling RNG early entropy source...
I (33) boot: SPI Speed      : 40MHz
I (37) boot: SPI Mode       : DIO
I (41) boot: SPI Flash Size : 16MB
I (45) boot: Partition Table:
I (49) boot: ## Label            Usage          Type ST Offset   Length
I (56) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (63) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (71) boot:  2 factory          factory app      00 00 00010000 00100000
I (78) boot: End of partition table
I (83) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x22d98 (142744) map
I (141) esp_image: segment 1: paddr=0x00032dc0 vaddr=0x3ff80000 size=0x00000 (     0) load
I (142) esp_image: segment 2: paddr=0x00032dc8 vaddr=0x3ff80000 size=0x00000 (     0) load
I (148) esp_image: segment 3: paddr=0x00032dd0 vaddr=0x3ffbdb60 size=0x02c90 ( 11408) load
I (162) esp_image: segment 4: paddr=0x00035a68 vaddr=0x3ffc07f0 size=0x00000 (     0) load
I (166) esp_image: segment 5: paddr=0x00035a70 vaddr=0x40080000 size=0x00400 (  1024) load0x40080000: _WindowOverflow4 at C:/msys32/home/AKAEM/esp/esp-idf/components/freertos/xtens
a_vectors.S:1685

I (175) esp_image: segment 6: paddr=0x00035e78 vaddr=0x40080400 size=0x0a198 ( 41368) load
I (201) esp_image: segment 7: paddr=0x00040018 vaddr=0x400d0018 size=0x6486c (411756) map
0x400d0018: _stext at ??:?

I (346) esp_image: segment 8: paddr=0x000a488c vaddr=0x4008a598 size=0x0751c ( 29980) load
0x4008a598: ram_write_gain_mem at ??:?

I (358) esp_image: segment 9: paddr=0x000abdb0 vaddr=0x400c0000 size=0x00000 (     0) load
I (359) esp_image: segment 10: paddr=0x000abdb8 vaddr=0x50000000 size=0x00000 (     0) load
I (365) esp_image: segment 11: paddr=0x000abdc0 vaddr=0x50000000 size=0x00000 (     0) load
I (385) boot: Loaded app from partition at offset 0x10000
I (385) boot: Disabling RNG early entropy source...
I (386) cpu_start: Pro cpu up.
I (390) cpu_start: Starting app cpu, entry point is 0x40081050
0x40081050: call_start_cpu1 at C:/msys32/home/AKAEM/esp/esp-idf/components/esp32/cpu_start.c:245

I (0) cpu_start: App cpu up.
D (400) memory_layout: Checking 11 reserved memory ranges:
D (405) memory_layout: Reserved memory range 0x3ffae000 - 0x3ffae6e0
D (412) memory_layout: Reserved memory range 0x3ffae6e0 - 0x3ffaff10
D (418) memory_layout: Reserved memory range 0x3ffb0000 - 0x3ffb6388
D (424) memory_layout: Reserved memory range 0x3ffb8000 - 0x3ffb9a20
D (431) memory_layout: Reserved memory range 0x3ffbdb28 - 0x3ffbdb5c
D (437) memory_layout: Reserved memory range 0x3ffbdb60 - 0x3ffc7598
D (444) memory_layout: Reserved memory range 0x3ffe0000 - 0x3ffe0440
D (450) memory_layout: Reserved memory range 0x3ffe3f20 - 0x3ffe4350
D (457) memory_layout: Reserved memory range 0x40070000 - 0x40078000
D (463) memory_layout: Reserved memory range 0x40078000 - 0x40080000
0x40080000: _WindowOverflow4 at C:/msys32/home/AKAEM/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

D (469) memory_layout: Reserved memory range 0x40080000 - 0x40091ab4
0x40080000: _WindowOverflow4 at C:/msys32/home/AKAEM/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

D (476) memory_layout: Building list of available memory regions:
D (482) memory_layout: Available memory region 0x3ffaff10 - 0x3ffb0000
D (489) memory_layout: Available memory region 0x3ffb6388 - 0x3ffb8000
D (495) memory_layout: Available memory region 0x3ffb9a20 - 0x3ffbdb28
D (502) memory_layout: Available memory region 0x3ffbdb5c - 0x3ffbdb60
D (508) memory_layout: Available memory region 0x3ffc7598 - 0x3ffc8000
D (515) memory_layout: Available memory region 0x3ffc8000 - 0x3ffca000
D (522) memory_layout: Available memory region 0x3ffca000 - 0x3ffcc000
D (528) memory_layout: Available memory region 0x3ffcc000 - 0x3ffce000
D (535) memory_layout: Available memory region 0x3ffce000 - 0x3ffd0000
D (541) memory_layout: Available memory region 0x3ffd0000 - 0x3ffd2000
D (548) memory_layout: Available memory region 0x3ffd2000 - 0x3ffd4000
D (555) memory_layout: Available memory region 0x3ffd4000 - 0x3ffd6000
D (561) memory_layout: Available memory region 0x3ffd6000 - 0x3ffd8000
D (568) memory_layout: Available memory region 0x3ffd8000 - 0x3ffda000
D (574) memory_layout: Available memory region 0x3ffda000 - 0x3ffdc000
D (581) memory_layout: Available memory region 0x3ffdc000 - 0x3ffde000
D (588) memory_layout: Available memory region 0x3ffde000 - 0x3ffe0000
D (594) memory_layout: Available memory region 0x3ffe0440 - 0x3ffe3f20
D (601) memory_layout: Available memory region 0x3ffe4350 - 0x3ffe8000
D (607) memory_layout: Available memory region 0x3ffe8000 - 0x3fff0000
D (614) memory_layout: Available memory region 0x3fff0000 - 0x3fff8000
D (621) memory_layout: Available memory region 0x3fff8000 - 0x3fffc000
D (627) memory_layout: Available memory region 0x3fffc000 - 0x40000000
D (634) memory_layout: Available memory region 0x40091ab4 - 0x40092000
D (640) memory_layout: Available memory region 0x40092000 - 0x40094000
D (647) memory_layout: Available memory region 0x40094000 - 0x40096000
D (654) memory_layout: Available memory region 0x40096000 - 0x40098000
D (660) memory_layout: Available memory region 0x40098000 - 0x4009a000
D (667) memory_layout: Available memory region 0x4009a000 - 0x4009c000
D (673) memory_layout: Available memory region 0x4009c000 - 0x4009e000
D (680) memory_layout: Available memory region 0x4009e000 - 0x400a0000
I (687) heap_init: Initializing. RAM available for dynamic allocation:
D (694) heap_init: New heap initialised at 0x3ffaff10
I (699) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
D (705) heap_init: New heap initialised at 0x3ffb6388
I (710) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
D (716) heap_init: New heap initialised at 0x3ffb9a20
I (721) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (727) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAM
D (733) heap_init: New heap initialised at 0x3ffc7598
I (739) heap_init: At 3FFC7598 len 00018A68 (98 KiB): DRAM
I (745) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (751) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
D (758) heap_init: New heap initialised at 0x40091ab4
I (763) heap_init: At 40091AB4 len 0000E54C (57 KiB): IRAM
I (769) cpu_start: Pro cpu start user code
D (781) clk: RTC_SLOW_CLK calibration value: 3278464
D (119) intr_alloc: Connected src 46 to int 2 (cpu 0)
D (120) intr_alloc: Connected src 57 to int 3 (cpu 0)
D (120) stack_chk: Intialize random stack guard
D (125) intr_alloc: Connected src 24 to int 9 (cpu 0)
I (130) cpu_start: Starting scheduler on PRO CPU.
D (0) intr_alloc: Connected src 25 to int 2 (cpu 1)
I (0) cpu_start: Starting scheduler on APP CPU.
D (145) heap_init: New heap initialised at 0x3ffe0440
D (155) heap_init: New heap initialised at 0x3ffe4350
D (165) intr_alloc: Connected src 16 to int 12 (cpu 0)
D (165) nvs: nvs_flash_init_custom partition=nvs start=9 count=6
D (245) BTDM_INIT: Release DRAM [0x3ffb2730] - [0x3ffb6388]
I (245) BTDM_INIT: BT controller compile version [6a842ec]
D (245) BTDM_INIT: .data initialise [0x3ffae6e0] <== [0x4000d890]
D (245) BTDM_INIT: .bss initialise [0x3ffb0000] - [0x3ffb09a8]
D (255) BTDM_INIT: .bss initialise [0x3ffb09a8] - [0x3ffb1ddc]
D (265) BTDM_INIT: .bss initialise [0x3ffb1ddc] - [0x3ffb2730]
D (265) BTDM_INIT: .bss initialise [0x3ffb8000] - [0x3ffb9a20]
D (275) BTDM_INIT: .bss initialise [0x3ffbdb28] - [0x3ffbdb5c]
I (275) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
D (295) nvs: nvs_open_from_partition phy 0
D (295) nvs: nvs_get cal_version 4
D (295) nvs: nvs_get_str_or_blob cal_mac
D (305) nvs: nvs_get_str_or_blob cal_data
D (315) nvs: nvs_close 1
I (385) phy: phy_version: 4000, b6198fa, Sep  3 2018, 15:11:06, 0, 0
I (715) GATTC_DEMO: esp_gattc_cb()
I (715) GATTC_DEMO: REG_EVT
I (715) GATTC_DEMO: ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT
I (725) GATTC_DEMO: ESP_GAP_BLE_SCAN_START_COMPLETE_EVT
I (725) GATTC_DEMO: scan start success
I (765) GATTC_DEMO: ESP_GAP_BLE_SCAN_RESULT_EVT
I (765) GATTC_DEMO: 43 bf 2b af c2 5c
I (765) GATTC_DEMO: searched Adv Data Len 14, Scan Response Len 0
I (775) GATTC_DEMO: searched Device Name Len 0
I (775) GATTC_DEMO:

I (2545) GATTC_DEMO: ESP_GAP_BLE_SCAN_RESULT_EVT
I (2545) GATTC_DEMO: 50 33 8b 1c ef f6
I (2545) GATTC_DEMO: searched Adv Data Len 30, Scan Response Len 26
I (2555) GATTC_DEMO: searched Device Name Len 15
I (2555) GATTC_DEMO: MilestonePod F6
I (2565) GATTC_DEMO:

I (2565) GATTC_DEMO: searched device MilestonePod

I (2565) GATTC_DEMO: connect to the remote device.
I (2575) GATTC_DEMO: ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT
I (2585) GATTC_DEMO: stop scan successfully
W (6655) BT_APPL: bta_gattc_conn_cback() - cif=1 connected=0 conn_id=1 reason=0x003e
I (6655) GATTC_DEMO: esp_gattc_cb()
I (6655) GATTC_DEMO: ESP_GATTC_DISCONNECT_EVT, reason = 62
I (6665) GATTC_DEMO: esp_gattc_cb()
I (6665) GATTC_DEMO: ESP_GATTC_OPEN_EVT
E (6675) GATTC_DEMO: open failed, status 133

I don't now to follow up on this issue. It works with 5 different WEMOS LOLIN boards, and I experience the problem with the M5Stack-FIRE device, which is my target device.

Any ideas?

chegewara commented 5 years ago

Check disconnect reason (62 = 0x3e): https://github.com/chegewara/esp32-ble-wiki/issues/5

an-erd commented 5 years ago

@chegewara Ok, I saw that's 0x3E Connection Failed to be Established With the other devices it works fine, but with the M5Stack devices I got this problems. I currently have no idea where to start looking for differences and how to maybe debug the lower level communication.

chegewara commented 5 years ago

M5-stack is enclosed, it can be problem. Start with testing m5-stack with nRF connect. Check RSSI value, check if you have problem to connect m5-stack from other than esp32 device.

PS As you can see its not easy to write ble app if your device does not cooperate.

an-erd commented 5 years ago

I made some more tests and moved the client and server devices. I see an impact. Also, as you mentioned, the enclosure seems to have a significant impact, too. On top there's a display, and then there is a (removable) bottom plate, battery and lot of other stuff inside. Without bottom plate its much better, but still not good enough. Ok, issue is resolved for the moment since I don't have a 95% failure rate anymore.

@chegewara Thanks!