Code: Select all
#include <Arduino.h>
#include "bt.h"
#include "btm_api.h"
#include "esp_gap_ble_api.h"
#include "esp_bt_main.h"
static bool connect = false;
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: {
//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:
//scan start complete event to indicate scan start successfully or failed
if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
ESP_LOGE("GAP", "Scan start failed");
}
break;
case 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:
for (int i = 0; i < 6; i++) {
ESP_LOGI("GAP", "%x:", scan_result->scan_rst.bda[i]);
}
ESP_LOGI("GAP", "Searched Adv Data Len %d, Scan Response Len %d\n", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
ESP_LOGI("GAP", "\n");
adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,
ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
ESP_LOGI("GAP", "Searched Device Name Len %d", adv_name_len);
for (int j = 0; j < adv_name_len; j++) {
ESP_LOGI("GAP", "%c", adv_name[j]);
}
break;
case ESP_GAP_SEARCH_INQ_CMPL_EVT:
break;
default:
break;
}
break;
}
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){
ESP_LOGE("GAP", "Scan stop failed");
}
else {
ESP_LOGI("GAP", "Stop scan successfully");
}
break;
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){
ESP_LOGE("GAP", "Adv stop failed");
}
else {
ESP_LOGI("GAP", "Stop adv successfully");
}
break;
default:
break;
}
}
void setup()
{
Serial.begin(115200);
delay(5000);
Serial.println("Starting BT");
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
esp_bt_controller_init(&bt_cfg);
esp_bt_controller_enable(ESP_BT_MODE_BTDM);
esp_bluedroid_init();
esp_bluedroid_enable();
esp_err_t status;
//register the scan callback function to the gap module
if ((status = esp_ble_gap_register_callback(esp_gap_cb)) != ESP_OK) {
ESP_LOGE("GAP", "gap register error, error code = %x", status);
return;
}
//register the callback function to the gattc module
/*if ((status = esp_ble_gattc_register_callback(esp_gattc_cb)) != ESP_OK) {
ESP_LOGE(GATTC_TAG, "gattc register error, error code = %x", status);
return;
}*/
}
int value = 0;
void loop()
{
}
after running the app i get this errors:
Code: Select all
Starting BT
BTDM CONTROLLER VERSION: 010101
btip start
copy .data from 4000d890 to 3ffae6e0, len 00001830
E (51371) BT: Conrollset .bss 0x0 from 3ffb8000 to 3ffbff70, len 00007f70
BTDM ROM VERSION 0101
BD_ADDR: 24:0A:C4:03:86:6A
NVDS MAGIC FAILED
RF Init OK with coex
er not initialised
E (51386) BT: Bludroid not initialised
E (51389) GAP: gap register error, error code = 103
Code: Select all
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 2M,
spiffs, data, spiffs, 0x210000,0x1F0000,