ESP32c6 Nimble-BLE Only send data
Posted: Thu Aug 22, 2024 12:19 am
Hi All,
I had many problems developing with esp32c6 module and many BLE examples does not work for esp32c6, mainly if I select "Bluedroid-DualMode" in menuconfig. I did a functional section of my code bellow but I burning my head and I do not know how send data from esp32c6 BT module to android application. I receive the data (buffRxBLE) comming from application but I cannot reply any data as far (buffTxBLE). Unfortunately I could not find any usefully example or tips about that! I think the esp32c6 is under development yet!
Could anyone help me?
I had many problems developing with esp32c6 module and many BLE examples does not work for esp32c6, mainly if I select "Bluedroid-DualMode" in menuconfig. I did a functional section of my code bellow but I burning my head and I do not know how send data from esp32c6 BT module to android application. I receive the data (buffRxBLE) comming from application but I cannot reply any data as far (buffTxBLE). Unfortunately I could not find any usefully example or tips about that! I think the esp32c6 is under development yet!
Could anyone help me?
Code: Select all
[Codebox=cpp file=Untitled.cpp][/Codebox]#include <assert.h>
#include <string.h>
#include <esp_err.h>
#include "ble_handler.h"
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "host/ble_hs.h"
#include "services/gap/ble_svc_gap.h"
#include "services/gatt/ble_svc_gatt.h"
#include "sdkconfig.h"
#define BLE_DEBUG
#define BLE_TAG "ble_handler"
#ifdef BLE_DEBUG
#define BLE_DEBUGF(BLE_TAG, format, ...) ESP_LOGI(BLE_TAG, format, ##__VA_ARGS__)
#else
#define BLE_DEBUGF(BLE_TAG, format, ...)
#endif /* BLE_DEBUG */
#define DEVICE_NAME "ESP32-C6"
char *TAG = "BLE-Server";
uint8_t ble_addr_type;
void ble_app_advertise(void);
extern char * buffRxBLE; // buffer reception
extern char *buffTxBLE; // pointer for tx buffer
// data sent from remote partner/app to ESP32
static int device_write(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg){
buffRxBLE=0;
buffRxBLE = (char *)ctxt->om->om_data;
printf("Data sent from bufferRx: %.*s\n", ctxt->om->om_len, ctxt->om->om_data);
vTaskDelay(5);
return 0;
}
// data sent from ESP32 to remote partner/app
static int device_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg){
printf("Data receipt bufferTx.........: %s\n", buffTxBLE);
os_mbuf_append(ctxt->om, buffTxBLE, strlen(buffTxBLE));
vTaskDelay(10);
return 0;
}
// Array of pointers to other service definitions
// UUID - Universal Unique Identifier
static const struct ble_gatt_svc_def gatt_svcs[] = {
{.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(0xFFE0), // Define UUID for device type
.characteristics = (struct ble_gatt_chr_def[]){
{.uuid = BLE_UUID16_DECLARE(0xFFE1), // Define UUID for writing
// .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_WRITE_NO_RSP,
.flags = BLE_GATT_CHR_F_WRITE, // | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_WRITE_NO_RSP,
.access_cb = device_write},
{.uuid = BLE_UUID16_DECLARE(0xFFE2), // Define UUID for reading
.flags = BLE_GATT_CHR_F_READ,
// .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_WRITE_NO_RSP,
.access_cb = ble_svc_gatt_srv_sup_feat_access},
{0}}},
{0}};
// BLE event handling
static int ble_gap_event(struct ble_gap_event *event, void *arg){
switch (event->type){
// If connected
case BLE_GAP_EVENT_CONNECT:
ESP_LOGI("GAP", "BLE GAP APP CONNECTION %s", event->connect.status == 0 ? "OK!" : "FAIL!");
if (event->connect.status != 0)
ble_app_advertise();
break;
// Advertise again after completion of the event
case BLE_GAP_EVENT_DISCONNECT:
ESP_LOGI("GAP", "BLE GAP CLIENT DISC");
ble_app_advertise();
break;
case BLE_GAP_EVENT_ADV_COMPLETE:
ESP_LOGI("GAP", "BLE GAP EVENT OK");
ble_app_advertise();
break;
default:
break;
}
return 0;
}
// BLE
void ble_app_advertise(void){
// GAP (Generic Access Profile)
struct ble_hs_adv_fields fields;
const char *device_name;
memset(&fields, 0, sizeof(fields));
fields.flags = BLE_HS_ADV_F_DISC_GEN | // show BLE
BLE_HS_ADV_F_BREDR_UNSUP; // show in mobile devices
device_name = ble_svc_gap_device_name();
fields.name = (uint8_t *)device_name;
fields.name_len = strlen(device_name);
fields.name_is_complete = 1;
ble_gap_adv_set_fields(&fields);
// GAP conectivity
struct ble_gap_adv_params adv_params;
memset(&adv_params, 0, sizeof(adv_params));
// NON: non 3.C.9.3.2 DIR: directed 3.C.9.3.3) UND: undirected 3.C.9.3.4
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
//NON: non 3.C.9.2.2 LTD: limited 3.C.9.2.3 GEN: general 3.C.9.2.4)
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
ble_gap_adv_start(ble_addr_type, NULL, BLE_HS_FOREVER, &adv_params, ble_gap_event, NULL);
}
// Application
void ble_app_on_sync(void){
ble_hs_id_infer_auto(0, &ble_addr_type); // Determines the best address type automatically
ble_app_advertise(); // Define the BLE connection
}
// Task
void host_task(void *param){
nimble_port_run(); // This function will return only when nimble_port_stop() is executed
}
//
esp_err_t esp32_ble_initialize(){
nvs_flash_init(); // 1 - NVS flash
nimble_port_init(); // 2 - host stack
ble_svc_gap_device_name_set(DEVICE_NAME); // 3 - NimBLE server
ble_svc_gap_init(); // 4 - NimBLE gap service
ble_svc_gatt_init(); // 5 - NimBLE gatt service
ble_gatts_count_cfg(gatt_svcs); // 6 - NimBLE config gatt services
ble_gatts_add_svcs(gatt_svcs); // 7 - NimBLE queues gatt services
ble_hs_cfg.sync_cb = ble_app_on_sync; // 8 - Ini app
nimble_port_freertos_init(host_task); // 9 - Run thread
return ESP_OK;
}