I want to activate Bluetooth BLE on the ESP32 with the Espressif framework, but the following code works for me when compiling with PlatformIO, but when using Bluetooth applications such as nRF Connect or LightBlue, the ESP32 is not possible. I would like to see the Bluetooth advertising coming from the ESP32
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "nvs_flash.h"
#define TAG "BLE_BEACON"
// Manejador de eventos GAP
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
if (event == ESP_GAP_BLE_ADV_START_COMPLETE_EVT) {
ESP_LOGI(TAG, "Publicidad iniciada con éxito.");
}
}
// Inicializar Bluetooth y publicar el nombre
void bluetooth_init() {
esp_err_t ret;
// Inicializar NVS
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);
// Inicializar controlador Bluetooth
ret = esp_bluedroid_init();
ESP_ERROR_CHECK(ret);
ret = esp_bluedroid_enable();
ESP_ERROR_CHECK(ret);
// Registrar manejador de eventos
ret = esp_ble_gap_register_callback(gap_event_handler);
ESP_ERROR_CHECK(ret);
// Configurar el nombre del dispositivo
const char *device_name = "MiESP32"; // Cambia este nombre al que prefieras
ret = esp_ble_gap_set_device_name(device_name);
ESP_ERROR_CHECK(ret);
// Configurar publicidad
esp_ble_adv_data_t adv_data = {
.set_scan_rsp = false,
.include_name = true, // Incluir el nombre del dispositivo
.include_txpower = true,
.min_interval = 0x20,
.max_interval = 0x40,
.appearance = 0x00,
.manufacturer_len = 0,
.p_manufacturer_data = NULL,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = 0,
.p_service_uuid = NULL,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
esp_ble_gap_config_adv_data(&adv_data);
// Parámetros de publicidad
esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
esp_ble_gap_start_advertising(&adv_params);
}
void app_main() {
bluetooth_init();
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000)); // Mantener el programa activo
}
}
I want to activate Bluetooth BLE on the ESP32 with the Espressif framework, but the following code works for me when compiling with PlatformIO, but when using Bluetooth applications such as nRF Connect or LightBlue, the ESP32 is not possible. I would like to see the Bluetooth advertising coming from the ESP32
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "nvs_flash.h"
#define TAG "BLE_BEACON"
// Manejador de eventos GAP
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
if (event == ESP_GAP_BLE_ADV_START_COMPLETE_EVT) {
ESP_LOGI(TAG, "Publicidad iniciada con éxito.");
}
}
// Inicializar Bluetooth y publicar el nombre
void bluetooth_init() {
esp_err_t ret;
// Inicializar NVS
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);
// Inicializar controlador Bluetooth
ret = esp_bluedroid_init();
ESP_ERROR_CHECK(ret);
ret = esp_bluedroid_enable();
ESP_ERROR_CHECK(ret);
// Registrar manejador de eventos
ret = esp_ble_gap_register_callback(gap_event_handler);
ESP_ERROR_CHECK(ret);
// Configurar el nombre del dispositivo
const char *device_name = "MiESP32"; // Cambia este nombre al que prefieras
ret = esp_ble_gap_set_device_name(device_name);
ESP_ERROR_CHECK(ret);
// Configurar publicidad
esp_ble_adv_data_t adv_data = {
.set_scan_rsp = false,
.include_name = true, // Incluir el nombre del dispositivo
.include_txpower = true,
.min_interval = 0x20,
.max_interval = 0x40,
.appearance = 0x00,
.manufacturer_len = 0,
.p_manufacturer_data = NULL,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = 0,
.p_service_uuid = NULL,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
esp_ble_gap_config_adv_data(&adv_data);
// Parámetros de publicidad
esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
esp_ble_gap_start_advertising(&adv_params);
}
void app_main() {
bluetooth_init();
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000)); // Mantener el programa activo
}
}
Share
Improve this question
edited Feb 7 at 15:03
rzickler
7331 gold badge5 silver badges21 bronze badges
asked Feb 5 at 3:54
Jose LopezJose Lopez
113 bronze badges
1
- The function esp_ble_gap_start_advertising(&adv_params) returns an error code which you should print/check. If you can't see any adverts, it's most probably failing silently, and you can't know until you check the error code. See this link for more info: docs.espressif.com/projects/esp-idf/en/stable/esp32/… – Youssif Saeed Commented Feb 5 at 7:33
1 Answer
Reset to default 0Use the ESP_ERROR_CHECK function to avoid the errors you had in the previous version of the code.
#include <stdio.h>
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#define TAG "BLE_VISIBILITY"
// Manejador de eventos GAP
static void gap_event_handler(esp_gap_ble_cb_event_t event,
esp_ble_gap_cb_param_t *param) {
if (event == ESP_GAP_BLE_ADV_START_COMPLETE_EVT) {
ESP_LOGI(TAG, "Publicidad BLE iniciada correctamente.");
} else if (event == ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT) {
ESP_LOGI(TAG, "Publicidad BLE detenida.");
}
}
void bluetooth_init() {
esp_err_t ret;
// Inicializar NVS
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);
// Inicializar Bluetooth
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); // Liberar memoria para Bluetooth clásico
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
// Inicializar Bluedroid
ESP_ERROR_CHECK(esp_bluedroid_init());
ESP_ERROR_CHECK(esp_bluedroid_enable());
// Registrar manejador de eventos GAP
ESP_ERROR_CHECK(esp_ble_gap_register_callback(gap_event_handler));
// Configurar el nombre del dispositivo
ESP_ERROR_CHECK(esp_ble_gap_set_device_name("ANGELBLE"));
// Configurar datos de publicidad
esp_ble_adv_data_t adv_data = {
.set_scan_rsp = false,
.include_name = true,
.include_txpower = true,
.min_interval = 0x100,
.max_interval = 0x200,
.appearance = 0x00,
.manufacturer_len = 0,
.p_manufacturer_data = NULL,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = 0,
.p_service_uuid = NULL,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
ESP_ERROR_CHECK(esp_ble_gap_config_adv_data(&adv_data));
// Configurar parámetros de publicidad
esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x100,
.adv_int_max = 0x200,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
ESP_ERROR_CHECK(esp_ble_gap_start_advertising(&adv_params));
}
void app_main() {
ESP_LOGI(TAG, "Iniciando ejemplo de visibilidad BLE...");
bluetooth_init();
}