Error while connecting esp32's BLE

mehbstnc
Posts: 8
Joined: Mon Aug 22, 2022 12:15 pm

Error while connecting esp32's BLE

Postby mehbstnc » Tue Nov 15, 2022 7:38 am

Hello guys,
I am trying to send data to a mobile phone via BLE.
When ı try to connect esp32 from android device using Lightblue app, program says failed to establish connection. I tried maybe 4 5 times to connect esp32. Sometimes it connects second or third attemption sometimes it cant connect. What is wrong with my code ? Can someone help me? I am using Nimble

CODE :

#include <stdio.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"

#include "esp_event.h"
#include "esp_bt.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "esp_nimble_hci.h"
#include "nimble/ble.h"
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
#include "host/ble_uuid.h"
#include "console/console.h"
#include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h"
#include "services/ans/ble_svc_ans.h"
#include "sdkconfig.h"
#include "voyager_adc.h"


#define GATT_SVR_SVC_ALERT_UUID 0x1811
//-------------------------------------------------------------------
/* 59462f12-9543-9999-12c8-58b459a2712d Service UUID*/
static const ble_uuid128_t gatt_svr_svc_sec_test_uuid =
BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
//-------------------------------------------------------------------
/* 5c3a659e-897e-45e1-b016-007107c96df6 Gidon data characteristic UUID*/
static const ble_uuid128_t gatt_svr_chr_gidon =
BLE_UUID128_INIT(0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
//-------------------------------------------------------------------
/* 5c3a659e-897e-45e1-b016-007107c96df7 Static data characteristic UUID*/
static const ble_uuid128_t gatt_svr_chr_write_pos =
BLE_UUID128_INIT(0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);

//-------------------------------------------------------------------
uint8_t ble_addr_type;
void ble_app_advertise(void);
void ble_app_on_sync(void);
//------------------------------------------------------------------
const char* tag = {"BLE"};
//-------------------------------------------------------------------
extern adc_data adc_data1; // The value that will updating for adc
//-------------------------------------------------------------------
/*Callback Function for Write pos*/
static int device_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
{

uint16_t stepper_data = (uint16_t)*(ctxt->om->om_data);
printf("Data from the client: %d \n",stepper_data);
return 0;
}
//--------------------------------------------------------------------------------
/*Callback Function for Gidon*/
static int device_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
{
int16_t data = adc_data1.adc_data;
os_mbuf_append(ctxt->om, &data, sizeof(data));
return 0;
}
//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
{
/*** Service: Security test. */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = &gatt_svr_svc_sec_test_uuid.u,
.characteristics = (struct ble_gatt_chr_def[]){
{
/*** Characteristic: Gidon sensor . */
.uuid = &gatt_svr_chr_gidon.u,
.access_cb = device_read,
.flags = BLE_GATT_CHR_F_READ,
},
{
/*** Characteristic: Read data. */
.uuid = &gatt_svr_chr_write_pos.u,
.access_cb = device_write,
.flags = BLE_GATT_CHR_F_WRITE ,
},
{
0, /* No more characteristics in this service. */
}},
},
{
0, /* No more services. */
},
};
//--------------------------------------------------------------------
//--------------------------------------------------------------------
static int ble_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
int rc;

switch (event->type)
{
case BLE_GAP_EVENT_CONNECT:
/* A new connection was established or a connection attempt failed. */
ESP_LOGI(tag, "connection %s; status=%d ",
event->connect.status == 0 ? "established" : "failed",
event->connect.status);
if (event->connect.status == 0)
{
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
assert(rc == 0);
}
ESP_LOGI(tag, "\n");

if (event->connect.status != 0)
{
/* Connection failed; resume advertising. */
ble_app_advertise();
}
return 0;

case BLE_GAP_EVENT_DISCONNECT:
ESP_LOGI(tag, "disconnect; reason=%d ", event->disconnect.reason);
ESP_LOGI(tag, "\n");

/* Connection terminated; resume advertising. */
ble_app_advertise();
return 0;

case BLE_GAP_EVENT_CONN_UPDATE:
/* The central has updated the connection parameters. */
ESP_LOGI("INFO", "connection updated; status=%d ",
event->conn_update.status);
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
assert(rc == 0);
ESP_LOGI("INFO", "\n");
return 0;

case BLE_GAP_EVENT_ENC_CHANGE:
/* Encryption has been enabled or disabled for this connection. */
ESP_LOGI("INFO", "encryption change event; status=%d ",
event->enc_change.status);
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
assert(rc == 0);
ESP_LOGI("INFO", "\n");
return 0;

case BLE_GAP_EVENT_SUBSCRIBE:
ESP_LOGI("INFO", "subscribe event; conn_handle=%d attr_handle=%d "
"reason=%d prevn=%d curn=%d previ=%d curi=%d\n",
event->subscribe.conn_handle,
event->subscribe.attr_handle,
event->subscribe.reason,
event->subscribe.prev_notify,
event->subscribe.cur_notify,
event->subscribe.prev_indicate,
event->subscribe.cur_indicate);
return 0;
}

return 0;
}
//-------------------------------------------------------------------
void ble_app_advertise(void)
{


struct ble_hs_adv_fields fields;
int rc;
const char *device_name;
memset(&fields, 0, sizeof(fields));
device_name = ble_svc_gap_device_name(); // Read the BLE device name
fields.name = (uint8_t *)device_name;
fields.name_len = strlen(device_name);
fields.name_is_complete = 1;
rc = ble_gap_adv_set_fields(&fields);
if(rc != 0)
{
ESP_LOGI(tag,"error setting advertisement data %d \n",rc);
}
// GAP - device connectivity definition
struct ble_gap_adv_params adv_params;
memset(&adv_params, 0, sizeof(adv_params));
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; // connectable or non-connectable
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; // discoverable or non-discoverable
ble_gap_adv_start(ble_addr_type, NULL, BLE_HS_FOREVER, &adv_params, ble_gap_event, NULL);



}
//---------------------------------------------------------------------
void ble_app_on_sync(void)
{
ble_hs_id_infer_auto(0, &ble_addr_type); //Determines the best address type automatically
ble_app_advertise();
}
//------------------------------------------------------------------------
void host_task(void *params)
{

nimble_port_run();
nimble_port_freertos_deinit();
}

int gatt_svr_init(void)
{
int rc;

ble_svc_gap_init();
ble_svc_gatt_init();
ble_svc_ans_init();

rc = ble_gatts_count_cfg(gatt_svr_svcs);
if (rc != 0) {
return rc;
}

rc = ble_gatts_add_svcs(gatt_svr_svcs);
if (rc != 0) {
return rc;
}

return 0;
}

void app_main()
{
adc_init();
xTaskCreate(read_adc_data, "read_adc_data", 4096, NULL, 1, NULL);
vTaskDelay(1000 / portTICK_PERIOD_MS);


nvs_flash_init(); // 1 - Initialize NVS flash using
esp_nimble_hci_and_controller_init(); // 2 - Initialize ESP controller
nimble_port_init(); // 3 - Initialize the host stack
ble_svc_gap_device_name_set("VOYAGER-ONUR"); // 4 - Initialize NimBLE configuration - server name
gatt_svr_init(); // 4 - Initialize NimBLE configuration - gatt service
ble_hs_cfg.sync_cb = ble_app_on_sync; // 5 - Initialize application
nimble_port_freertos_init(host_task); // 6 - Run the thread



}

Who is online

Users browsing this forum: Baidu [Spider] and 74 guests