ESP32 BLE : Dynamic data advertisment

Swagger
Posts: 28
Joined: Tue May 19, 2020 3:06 am

ESP32 BLE : Dynamic data advertisment

Postby Swagger » Tue May 19, 2020 3:29 am

Hi,
I'm trying to use ESP32 BLE GATT server code to advertise a data with my specific manufacture data. So I need to update manufacture data every seconds using a timer.

This is my manuafacture data looks like.
uint8_t advtimer_value = 0x22;
"uint8_t manufacture_specific_data[8] = {0x0b, advtimer_value, 0x02, 0x00};"
Till her it works and if i change the value in between then iam not able to update this advtimer_value in esp32. It says not a const . How should overcome this. Or is there any other way which i can achieve my goal.
Please help.

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 BLE : Dynamic data advertisment

Postby chegewara » Tue May 19, 2020 4:15 am

Code: Select all

uint8_t advtimer_value = 0x22;
uint8_t manufacture_specific_data[8] = {0x0b, 0x0, 0x02, 0x00};
manufacture_specific_data[1] = advtimer_value;

Swagger
Posts: 28
Joined: Tue May 19, 2020 3:06 am

Re: ESP32 BLE : Dynamic data advertisment

Postby Swagger » Tue May 19, 2020 4:56 am

chegewara wrote:
Tue May 19, 2020 4:15 am

Code: Select all

uint8_t advtimer_value = 0x22;
uint8_t manufacture_specific_data[8] = {0x0b, 0x0, 0x02, 0x00};
manufacture_specific_data[1] = advtimer_value;

Hi , do i need to do anything on the below functions to get the changes reflected in my advertising frames.
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
adv_config_done &= (~ADV_CONFIG_FLAG);
manufacture_specific_data[1] = advtimer_value;
if (adv_config_done == 0)
{
esp_ble_gap_start_advertising(&adv_params);
}
break;
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
adv_config_done &= (~SCAN_RSP_CONFIG_FLAG);
if (adv_config_done == 0)
{
esp_ble_gap_start_advertising(&adv_params);
}
break;

And my "advtimer_value" will be inside a timer, so do i just need to change the value there or do i need to make any changes in the BLE code.
Last edited by Swagger on Wed May 20, 2020 8:30 am, edited 1 time in total.

Swagger
Posts: 28
Joined: Tue May 19, 2020 3:06 am

Re: ESP32 BLE : Dynamic data advertisment

Postby Swagger » Tue May 19, 2020 5:00 am

chegewara wrote:
Tue May 19, 2020 4:15 am

Code: Select all

uint8_t advtimer_value = 0x22;
uint8_t manufacture_specific_data[8] = {0x0b, 0x0, 0x02, 0x00};
manufacture_specific_data[1] = advtimer_value;

Hi thanks for the quick reply,

Do i need to make any changes in the belwo functions to reflect the changes in my advertsing frames,

case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
adv_config_done &= (~ADV_CONFIG_FLAG);
manufacture_specific_data[1] = advtimer_value;
if (adv_config_done == 0)
{
esp_ble_gap_start_advertising(&adv_params);
}
break;
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
adv_config_done &= (~SCAN_RSP_CONFIG_FLAG);
if (adv_config_done == 0)
{
esp_ble_gap_start_advertising(&adv_params);
}
break;

My "advtimer_value" will be updated inside a timer functions, so do i just neeed to update the advtimer_value value ther or do i need to make any changes in the BLE functions.

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 BLE : Dynamic data advertisment

Postby chegewara » Tue May 19, 2020 8:40 am

When advtimer_val is updated somewhere else, then you have to stop advertising, update manufacturer data and start advertising again.

Swagger
Posts: 28
Joined: Tue May 19, 2020 3:06 am

Re: ESP32 BLE : Dynamic data advertisment

Postby Swagger » Wed May 20, 2020 3:17 am

hi , thanks for your response.

uint8_t manufacture_specific_data[4] = {0x0b, 0xFF, 0x02, 0x00};

// scan response data
static esp_ble_adv_data_t scan_rsp_data = {
.set_scan_rsp = true,
.include_name = true,
.include_txpower = false,
.min_interval = 0x0006,
.max_interval = 0x0010,
.appearance = 0x00,
.manufacturer_len = sizeof(manufacture_specific_data),
.p_manufacturer_data = manufacture_specific_data,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = ESP_UUID_LEN_128,
.p_service_uuid = service_uuid,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};

I want to change the manufacture_specific_data[2]= advtimer_value ;

advtimer_value will be updated every second based on a timer.

But the advertising value is not getting any changes even if the " advtimer_value " in the time is changing every second. every time I change the value I am stopping the advertisement as you said and after changing the value restarting the advertisement.

What I am doing wrong.

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 BLE : Dynamic data advertisment

Postby chegewara » Wed May 20, 2020 4:06 am

I dont have IDF example now, but here is simplest arduino example to show you idea:

Code: Select all

#include <BLEDevice.h>
#include <BLEServer.h>

BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
uint8_t val;

void setup() {
  BLEDevice::init("esp32");
  BLEServer *pServer = BLEDevice::createServer();
}

void loop() {
  String _val = String(val++);
  BLEAdvertisementData data;
  data.setFlags(0x6);
  data.setManufacturerData(_val.c_str());
  pAdvertising->setAdvertisementData(data);
  pAdvertising->start();

  delay(1000);
}

Swagger
Posts: 28
Joined: Tue May 19, 2020 3:06 am

Re: ESP32 BLE : Dynamic data advertisment

Postby Swagger » Wed May 20, 2020 7:29 am

Iam using esp gatt server demo code
:https://github.com/espressif/esp-idf/bl ... tts_demo.c


uint8_t manufacture_specific_data[8] = { 0x0b, 0xFF, 0x02, 0x00};
uint8_t checkNum = 0;


/* The length of adv data must be less than 31 bytes */
static esp_ble_adv_data_t adv_data = {
.set_scan_rsp = false,
.include_name = false,
.include_txpower = false,
.min_interval = 0x20,
.max_interval = 0x40, //slave connection max interval, Time = max_interval * 1.25 msec
.appearance = 0x00,
// .manufacturer_len = sizeof(manufacture_specific_data),
// .p_manufacturer_data = &manufacture_specific_data,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = ESP_UUID_LEN_128,
.p_service_uuid = Huilan_service_uuid,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};

// scan response data
static esp_ble_adv_data_t scan_rsp_data = {
.set_scan_rsp = true,
.include_name = true,
.include_txpower = false,
.min_interval = 0x0006,
.max_interval = 0x0010,
.appearance = 0x00,
.manufacturer_len = sizeof(manufacture_specific_data),
.p_manufacturer_data = manufacture_specific_data,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = ESP_UUID_LEN_128,
.p_service_uuid = Huilan_service_uuid,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
static 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,
};



///////////////////////////////////////////////////////////////////////////////////////////////////////
This is the timer call back where I updated the manufacture data

void periodic_timer_callback(void *arg)
{
checkNum++;
int64_t time_since_boot = esp_timer_get_time();
ESP_LOGI(timer_tag, "Periodic timer called, time since boot: %lld us", time_since_boot);
if (checkNum != 250)
{
dyn_adv_update(false);
manufacture_specific_data[2] = checkNum;
dyn_adv_update(true);
}
else
{
ESP_ERROR_CHECK(esp_timer_stop(periodic_timer));
ESP_ERROR_CHECK(esp_timer_delete(periodic_timer));
ESP_LOGI(timer_tag, "Stopped and deleted timers");
}
}

void dyn_adv_update(bool param)
{
if (param == false)
esp_ble_gap_stop_advertising();
if (param == true)
esp_ble_gap_start_advertising(&adv_params);
}


so that each time the periodic timer is called the manufacture data needs to be updated. The timer works fine. But the data is not getting updated when advertising.

kubera
Posts: 23
Joined: Wed May 20, 2020 10:21 am

Re: ESP32 BLE : Dynamic data advertisment

Postby kubera » Wed May 20, 2020 10:29 am

Hi ,
I am also trying the same code from esp32 gatt server demo.

is there anything to do with the initialization of manufacture specifi data.

chegewara
Posts: 2207
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 BLE : Dynamic data advertisment

Postby chegewara » Wed May 20, 2020 11:11 am

You need to call this function only, and code you already have will take care of rest (will start advertising thru events):

Code: Select all

esp_ble_gap_config_adv_data()
https://github.com/espressif/esp-idf/bl ... emo.c#L208
https://github.com/espressif/esp-idf/bl ... emo.c#L202

Who is online

Users browsing this forum: No registered users and 95 guests