[已解决](esp32c3)eddystone url 用 coded phy 可以吗?

User avatar
666hjk
Posts: 47
Joined: Wed Jan 12, 2022 3:09 am
Location: 小红点

[已解决](esp32c3)eddystone url 用 coded phy 可以吗?

Postby 666hjk » Thu Oct 06, 2022 1:02 am

有那位可以帮帮忙? 谢谢。

1) 构建后的输出 build and flash giving output :
E (356) EDDYSTONE_DEMO: app_main set prefered default phy failed: ESP_ERR_INVALID_STATE
问题在哪里?

2) 把 esp_ble_gap_set_prefered_default_phy( ESP_BLE_GAP_PHY_CODED_PREF_MASK, ESP_BLE_GAP_PHY_1M_PREF_MASK) 这样用法对吗? 有案例我可以参考吗?


=============================================================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_gatt_common_api.h"

#include "sdkconfig.h"

#define LOG_TAG "EDDYSTONE_DEMO"
#define NAME "test4"

#define ESP_BLE_GAP_PHY_CODED_PREF_MASK (1 << 2)
#define ESP_BLE_GAP_PHY_1M_PREF_MASK (1 << 0)

uint8_t addr_coded_eddystone[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x01};

static esp_ble_adv_params_t adv_params_coded_eddystone = {
.adv_int_min = 0x30,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_NONCONN_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};


static uint8_t eddystone_data[] = {
/* flags */
0x02, 0x01, 0x06,
/* service type*/
0x03, 0x03, 0xAA, 0xFE,
/* service data */
0x13, 0x16, 0xAA, 0xFE, 0x10, 0xF8, 0x03,'g','o','o','.','g','l','/','a','0','m','n','s','S'
};


static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
switch (event) {
case ESP_GAP_BLE_SET_STATIC_RAND_ADDR_EVT:
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_SET_STATIC_RAND_ADDR_EVT, status %d", param->set_rand_addr_cmpl.status);
break;


case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT, status %d", param->adv_data_raw_cmpl.status);
esp_ble_gap_start_advertising(&adv_params_coded_eddystone);
break;

case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_ADV_START_COMPLETE_EVT, status %d", param->adv_start_cmpl.status);
break;

case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT, status %d", param->adv_stop_cmpl.status);
break;
default:
break;
}
}




void app_main(void)
{
esp_err_t ret;

ret = esp_ble_gap_set_prefered_default_phy( ESP_BLE_GAP_PHY_CODED_PREF_MASK, ESP_BLE_GAP_PHY_1M_PREF_MASK);
if (ret) {
ESP_LOGE(LOG_TAG, "%s set prefered default phy failed: %s\n", __func__, esp_err_to_name(ret));
return;
}


// Initialize 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 );

/*

*/

ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));

esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(LOG_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
return;
}

ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret) {
ESP_LOGE(LOG_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
return;
}



ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(LOG_TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(LOG_TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_ble_gap_set_device_name(NAME);
if (ret) {
ESP_LOGE(LOG_TAG, "%s name device failed: %s\n", __func__, esp_err_to_name(ret));
return;
}

ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret){
ESP_LOGE(LOG_TAG, "gap register error, error code = %x", ret);
return;
}

vTaskDelay(200 / portTICK_PERIOD_MS);

ret = esp_ble_gap_set_rand_addr(addr_coded_eddystone);
if (ret) {
ESP_LOGE(LOG_TAG, "%s set random address failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_ble_gap_config_adv_data_raw(eddystone_data, sizeof(eddystone_data));
if (ret) {
ESP_LOGE(LOG_TAG, "%s config raw adv data failed: %s\n", __func__, esp_err_to_name(ret));
return;
}

return;
}


==============================================================================================================

构建后的输出 after build & flash, (see below) :

E (356) EDDYSTONE_DEMO: app_main set prefered default phy failed: ESP_ERR_INVALID_STATE
Last edited by 666hjk on Sat Nov 12, 2022 1:00 pm, edited 2 times in total.
学习中找战友。

ESP_XuLZ
Posts: 173
Joined: Fri Mar 26, 2021 6:04 am

Re: (esp32c3)eddystone url 用 coded phy 可以吗?

Postby ESP_XuLZ » Sun Oct 09, 2022 6:12 am

coded phy 的使用示例可以参考这个 demo: https://github.com/espressif/esp-idf/tr ... /multi-adv

esp_ble_gap_set_prefered_default_phy API 的使用位置也不对,应当在 controller init 和 enable 之后; 使用 coded phy 广播可以不用这个 API,参考 ble_50/multi-adv 例程进行修改

User avatar
666hjk
Posts: 47
Joined: Wed Jan 12, 2022 3:09 am
Location: 小红点

Re: (esp32c3)eddystone url 用 coded phy 可以吗?

Postby 666hjk » Sun Oct 09, 2022 5:42 pm

谢谢你的提议。

1)我是想, 如果只有一个adv的话, 可用另一个方法来广播而不需要用multi-adv.
2) ble_50/multi-adv 里,.tx_power = EXT_ADV_TX_PWR_NO_PREFERENCE , 他的type 是 uint8_t . 如果用 +126dBm , 就是 0x7E ?

让我做修正,才让你知道。 :D

1x  .png
1x .png (125.58 KiB) Viewed 5642 times
学习中找战友。

User avatar
666hjk
Posts: 47
Joined: Wed Jan 12, 2022 3:09 am
Location: 小红点

Re: (esp32c3)eddystone url 用 coded phy 可以吗?

Postby 666hjk » Mon Oct 10, 2022 7:34 pm

ESP_XuLZ wrote:
Sun Oct 09, 2022 6:12 am
coded phy 的使用示例可以参考这个 demo: https://github.com/espressif/esp-idf/tr ... /multi-adv

esp_ble_gap_set_prefered_default_phy API 的使用位置也不对,应当在 controller init 和 enable 之后; 使用 coded phy 广播可以不用这个 API,参考 ble_50/multi-adv 例程进行修改
ESP_XuLZ ,
你提议的demo 是用 ESP-IDF Release v4.3.4 。 我的version ESP-IDF v4.4.2-dirty

1) documentation 里, 用以下方法做跟换对吗? (我第一次换version, 不怎么会)
cd $IDF_PATH
git fetch
git checkout release/v4.3.4
git pull
git submodule update --init --recursive


2) 如果,每个使用示例 都用不同的version 不就很麻烦?有更好的方法吗?


谢谢。
学习中找战友。

ESP_XuLZ
Posts: 173
Joined: Fri Mar 26, 2021 6:04 am

Re: (esp32c3)eddystone url 用 coded phy 可以吗?

Postby ESP_XuLZ » Tue Oct 11, 2022 2:06 am

666hjk wrote:
Mon Oct 10, 2022 7:34 pm
ESP_XuLZ wrote:
Sun Oct 09, 2022 6:12 am
coded phy 的使用示例可以参考这个 demo: https://github.com/espressif/esp-idf/tr ... /multi-adv

esp_ble_gap_set_prefered_default_phy API 的使用位置也不对,应当在 controller init 和 enable 之后; 使用 coded phy 广播可以不用这个 API,参考 ble_50/multi-adv 例程进行修改
ESP_XuLZ ,
你提议的demo 是用 ESP-IDF Release v4.3.4 。 我的version ESP-IDF v4.4.2-dirty

1) documentation 里, 用以下方法做跟换对吗? (我第一次换version, 不怎么会)
cd $IDF_PATH
git fetch
git checkout release/v4.3.4
git pull
git submodule update --init --recursive


2) 如果,每个使用示例 都用不同的version 不就很麻烦?有更好的方法吗?


谢谢。
https://github.com/espressif/esp-idf/tr ... /multi-adv
multi-adv demo 在很多分支中和 tag 版本都有的, 建议你目前使用稳定的 release/v4.4 分支进行开发

关于版本分支切换后,可以在 esp-idf 目录下如下操作
git submodule update --init --recursive
./install.sh
. ./export.sh

User avatar
666hjk
Posts: 47
Joined: Wed Jan 12, 2022 3:09 am
Location: 小红点

Re: (esp32c3)eddystone url 用 coded phy 可以吗?

Postby 666hjk » Tue Oct 11, 2022 8:53 pm

ESP_XuLZ wrote:
Tue Oct 11, 2022 2:06 am
666hjk wrote:
Mon Oct 10, 2022 7:34 pm
ESP_XuLZ wrote:
Sun Oct 09, 2022 6:12 am
coded phy 的使用示例可以参考这个 demo: https://github.com/espressif/esp-idf/tr ... /multi-adv

esp_ble_gap_set_prefered_default_phy API 的使用位置也不对,应当在 controller init 和 enable 之后; 使用 coded phy 广播可以不用这个 API,参考 ble_50/multi-adv 例程进行修改
ESP_XuLZ ,
你提议的demo 是用 ESP-IDF Release v4.3.4 。 我的version ESP-IDF v4.4.2-dirty

1) documentation 里, 用以下方法做跟换对吗? (我第一次换version, 不怎么会)
cd $IDF_PATH
git fetch
git checkout release/v4.3.4
git pull
git submodule update --init --recursive


2) 如果,每个使用示例 都用不同的version 不就很麻烦?有更好的方法吗?


谢谢。
https://github.com/espressif/esp-idf/tr ... /multi-adv
multi-adv demo 在很多分支中和 tag 版本都有的, 建议你目前使用稳定的 release/v4.4 分支进行开发

关于版本分支切换后,可以在 esp-idf 目录下如下操作
git submodule update --init --recursive
./install.sh
. ./export.sh
1) 做了以下的步骤,但 version 还是 ESP-IDF v4.4.2-dirty. 需要先清除esp-idf folder理的文件,才跟步骤吗?
~/esp/esp-idf $ git submodule update --init --recursive
~/esp/esp-idf$ ./install.sh
~/esp/esp-idf$ . ./export.sh

2) 电脑里的 components/bt/host/bluedroid/api/include/api/esp_gap_ble_api.h 也找不到修改后的
#define EXT_ADV_TX_PWR_NO_PREFERENCE (127)
- // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD , 还是2016

我有忽略其他的程序吗? 谢谢。
学习中找战友。

ESP_XuLZ
Posts: 173
Joined: Fri Mar 26, 2021 6:04 am

Re: (esp32c3)eddystone url 用 coded phy 可以吗?

Postby ESP_XuLZ » Wed Oct 12, 2022 1:49 am

漏了一条,先切换到 release/v4.4 分支后,再执行后面的操作
cd $IDF_PATH
git fetch
git checkout release/v4.4
git pull
git submodule update --init --recursive
./install.sh
. ./export.sh

User avatar
666hjk
Posts: 47
Joined: Wed Jan 12, 2022 3:09 am
Location: 小红点

Re: (esp32c3)eddystone url 用 coded phy 可以吗?

Postby 666hjk » Wed Oct 12, 2022 10:27 am

ESP_XuLZ wrote:
Wed Oct 12, 2022 1:49 am
漏了一条,先切换到 release/v4.4 分支后,再执行后面的操作
cd $IDF_PATH
git fetch
git checkout release/v4.4
git pull
git submodule update --init --recursive
./install.sh
. ./export.sh


1) version 的问题已经搞定。 >> ESP-IDF v4.4.2-378-g9269a536ac-dirty 。 谢谢。
2) build and flash 后的输出也一切ok(看下方), 但手机scan 不到 eddystone url ?? 问题在哪里?


在 ble_50/multi-adv 例程里,我外加上eddystone url 的代码做测试 (如以下)

uint8_t eddystone_coded[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x05};

esp_ble_gap_ext_adv_params_t ext_eddystone_coded = {
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY_NONCONN, // parameter 用的对吗?
.interval_min = 0x60,
.interval_max = 0x60,
.channel_map = ADV_CHNL_ALL,
// .filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
.primary_phy = ESP_BLE_GAP_PHY_1M,
.max_skip = 0,
.secondary_phy = ESP_BLE_GAP_PHY_CODED,
.sid = 4,
.scan_req_notif = false,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.tx_power = EXT_ADV_TX_PWR_NO_PREFERENCE,
};


static uint8_t raw_data_coded_eddystone[] = {
0x02, 0x01, 0x06, // ( type of flag )
0x03, 0x03, 0xAA,0xFE, // (16-bit uuid type)
0x13, 0x16, 0xAA,0xFE, // service data , eddystone ID , (type of service data)
0x10, 0xF8, 0x03,'r','b','.','g','y','/','p','z','g','j','7','r' // with url shortener
// 0x10 = eddystone frame type , 0xF8 = tx power , 0x03 = https:// , site url
};


static esp_ble_gap_ext_adv_t ext_adv[5] = {
// instance, duration, period
[0] = {0, 0, 0},
[1] = {1, 0, 0},
[2] = {2, 0, 0},
[3] = {3, 0, 0},
[4] = {4, 0, 0}, // 为eddystone url 加上的。
};


// coded phy extend adv , eddystone url
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_params(4, &ext_eddystone_coded), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_rand_addr(4, eddystone_coded), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_adv_data_raw(4, sizeof(raw_data_coded_eddystone), &raw_data_coded_eddystone[0]), test_sem);

==============================================================================================
修整后的code如以下,


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"

#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_gatt_common_api.h"

#include "sdkconfig.h"

#include "freertos/semphr.h"

#define LOG_TAG "MULTI_ADV_DEMO"

#define FUNC_SEND_WAIT_SEM(func, sem) do {\
esp_err_t __err_rc = (func);\
if (__err_rc != ESP_OK) { \
ESP_LOGE(LOG_TAG, "%s, message send fail, error = %d", __func__, __err_rc); \
} \
xSemaphoreTake(sem, portMAX_DELAY); \
} while(0);

static SemaphoreHandle_t test_sem = NULL;

uint8_t addr_1m[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x01};
uint8_t addr_2m[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x02};
uint8_t addr_legacy[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x03};
uint8_t addr_coded[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x04};
uint8_t eddystone_coded[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x05};


esp_ble_gap_ext_adv_params_t ext_adv_params_1M = {
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_CONNECTABLE,
.interval_min = 0x30,
.interval_max = 0x30,
.channel_map = ADV_CHNL_ALL,
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
.primary_phy = ESP_BLE_GAP_PHY_1M,
.max_skip = 0,
.secondary_phy = ESP_BLE_GAP_PHY_1M,
.sid = 0,
.scan_req_notif = false,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.tx_power = EXT_ADV_TX_PWR_NO_PREFERENCE,
};

esp_ble_gap_ext_adv_params_t ext_adv_params_2M = {
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_SCANNABLE,
.interval_min = 0x40,
.interval_max = 0x40,
.channel_map = ADV_CHNL_ALL,
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
.primary_phy = ESP_BLE_GAP_PHY_1M,
.max_skip = 0,
.secondary_phy = ESP_BLE_GAP_PHY_2M,
.sid = 1,
.scan_req_notif = false,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.tx_power = EXT_ADV_TX_PWR_NO_PREFERENCE,
};

esp_ble_gap_ext_adv_params_t legacy_adv_params = {
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY_IND,
.interval_min = 0x45,
.interval_max = 0x45,
.channel_map = ADV_CHNL_ALL,
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
.primary_phy = ESP_BLE_GAP_PHY_1M,
.max_skip = 0,
.secondary_phy = ESP_BLE_GAP_PHY_1M,
.sid = 2,
.scan_req_notif = false,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.tx_power = EXT_ADV_TX_PWR_NO_PREFERENCE,
};

esp_ble_gap_ext_adv_params_t ext_adv_params_coded = {
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_SCANNABLE,
.interval_min = 0x50,
.interval_max = 0x50,
.channel_map = ADV_CHNL_ALL,
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
.primary_phy = ESP_BLE_GAP_PHY_1M,
.max_skip = 0,
.secondary_phy = ESP_BLE_GAP_PHY_CODED,
.sid = 3,
.scan_req_notif = false,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.tx_power = EXT_ADV_TX_PWR_NO_PREFERENCE,
};


esp_ble_gap_ext_adv_params_t ext_eddystone_coded = {
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY_NONCONN,
.interval_min = 0x60,
.interval_max = 0x60,
.channel_map = ADV_CHNL_ALL,
// .filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
.primary_phy = ESP_BLE_GAP_PHY_1M,
.max_skip = 0,
.secondary_phy = ESP_BLE_GAP_PHY_CODED,
.sid = 4,
.scan_req_notif = false,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.tx_power = EXT_ADV_TX_PWR_NO_PREFERENCE,
};


static uint8_t raw_adv_data_1m[] = {
0x02, 0x01, 0x06,
0x02, 0x0a, 0xeb,
0x11, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
'D', 'V', '_', '1', 'M'
};

static uint8_t raw_scan_rsp_data_2m[] = {
0x02, 0x01, 0x06,
0x02, 0x0a, 0xeb,
0x11, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
'D', 'V', '_', '2', 'M'
};

static uint8_t legacy_adv_data[] = {
0x02, 0x01, 0x06,
0x02, 0x0a, 0xeb,
0x14, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
'D', 'V', '_', 'C', 'O', 'D', 'E', 'D'
};

static uint8_t legacy_scan_rsp_data[] = {
0x02, 0x01, 0x06,
0x02, 0x0a, 0xeb,
0x15, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
'D', 'V', '_', 'L', 'E', 'G', 'A', 'C', 'Y'
};

static uint8_t raw_scan_rsp_data_coded[] = {
0x02, 0x01, 0x06,
0x02, 0x0a, 0xeb,
0x14, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
'D', 'V', '_', 'C', 'O', 'D', 'E', 'D'
};


static uint8_t raw_data_coded_eddystone[] = {
0x02, 0x01, 0x06, // ( type of flag )
0x03, 0x03, 0xAA,0xFE, // length of service list , service list , eddystone ID , (16-bit uuid type)
0x13, 0x16, 0xAA,0xFE, // length of service data , service data , eddystone ID , (type of service data)
0x10, 0xF8, 0x03,'r','b','.','g','y','/','p','z','g','j','7','r' // with url shortener
// 0x10 = eddystone frame type , 0xF8 = tx power , 0x03 = https:// , site url
};




static esp_ble_gap_ext_adv_t ext_adv[5] = {
// instance, duration, peroid
[0] = {0, 0, 0},
[1] = {1, 0, 0},
[2] = {2, 0, 0},
[3] = {3, 0, 0},
[4] = {4, 0, 0},
};

static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
switch (event) {
case ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT:
xSemaphoreGive(test_sem);
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, status %d", param->ext_adv_set_rand_addr.status);
break;
case ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT:
xSemaphoreGive(test_sem);
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status %d", param->ext_adv_set_params.status);
break;
case ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT:
xSemaphoreGive(test_sem);
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT, status %d", param->ext_adv_data_set.status);
break;
case ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT:
xSemaphoreGive(test_sem);
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT, status %d", param->scan_rsp_set.status);
break;
case ESP_GAP_BLE_EXT_ADV_START_COMPLETE_EVT:
xSemaphoreGive(test_sem);
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_START_COMPLETE_EVT, status %d", param->ext_adv_start.status);
break;
case ESP_GAP_BLE_EXT_ADV_STOP_COMPLETE_EVT:
xSemaphoreGive(test_sem);
ESP_LOGI(LOG_TAG, "ESP_GAP_BLE_EXT_ADV_STOP_COMPLETE_EVT, status %d", param->ext_adv_stop.status);
break;
default:
break;
}
}

void app_main(void)
{
esp_err_t ret;

// Initialize 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 );

ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));

esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(LOG_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
return;
}

ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret) {
ESP_LOGE(LOG_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(LOG_TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(LOG_TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret));
return;
}
ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret){
ESP_LOGE(LOG_TAG, "gap register error, error code = %x", ret);
return;
}

vTaskDelay(200 / portTICK_PERIOD_MS);

test_sem = xSemaphoreCreateBinary();
// 1M phy extend adv, Connectable advertising
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_params(0, &ext_adv_params_1M), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_rand_addr(0, addr_1m), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_adv_data_raw(0, sizeof(raw_adv_data_1m), &raw_adv_data_1m[0]), test_sem);

// 2M phy extend adv, Scannable advertising
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_params(1, &ext_adv_params_2M), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_rand_addr(1, addr_2m), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_scan_rsp_data_raw(1, sizeof(raw_scan_rsp_data_2m), raw_scan_rsp_data_2m), test_sem);

// 1M phy legacy adv, ADV_IND
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_params(2, &legacy_adv_params), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_rand_addr(2, addr_legacy), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_adv_data_raw(2, sizeof(legacy_adv_data), &legacy_adv_data[0]), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_scan_rsp_data_raw(2, sizeof(legacy_scan_rsp_data), &legacy_scan_rsp_data[0]), test_sem);

// coded phy extend adv, Connectable advertising
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_params(3, &ext_adv_params_coded), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_rand_addr(3, addr_coded), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_scan_rsp_data_raw(3, sizeof(raw_scan_rsp_data_coded), &raw_scan_rsp_data_coded[0]), test_sem);


// coded phy extend adv , eddystone url
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_params(4, &ext_eddystone_coded), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_set_rand_addr(4, eddystone_coded), test_sem);
FUNC_SEND_WAIT_SEM(esp_ble_gap_config_ext_adv_data_raw(4, sizeof(raw_data_coded_eddystone), &raw_data_coded_eddystone[0]), test_sem);


// start all adv
FUNC_SEND_WAIT_SEM(esp_ble_gap_ext_adv_start(5, &ext_adv[0]), test_sem);

return;
}

==================================================================================================
build and flash 后的输出, 一切ok.

I (627) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status 0
I (627) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, status 0
I (627) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT, status 0
I (637) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status 0
I (647) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, status 0
I (657) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT, status 0
I (657) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status 0
I (667) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, status 0
I (677) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT, status 0
I (687) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT, status 0
I (697) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status 0
I (697) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, status 0
I (707) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_SCAN_RSP_DATA_SET_COMPLETE_EVT, status 0
I (717) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_PARAMS_COMPLETE_EVT, status 0
I (727) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_SET_RAND_ADDR_COMPLETE_EVT, status 0
I (737) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_DATA_SET_COMPLETE_EVT, status 0
I (737) MULTI_ADV_DEMO: ESP_GAP_BLE_EXT_ADV_START_COMPLETE_EVT, status 0
学习中找战友。

ESP_XuLZ
Posts: 173
Joined: Fri Mar 26, 2021 6:04 am

Re: (esp32c3)eddystone url 用 coded phy 可以吗?

Postby ESP_XuLZ » Thu Oct 13, 2022 3:59 am

广播结构不对

multi-adv demo 只修改如下内容再试下
static uint8_t raw_scan_rsp_data_coded[] = {
0x02, 0x01, 0x06, // ( type of flag )
0x03, 0x03, 0xAA,0xFE, // length of service list , service list , eddystone ID , (16-bit uuid type)
0x03, 0x16, 0xAA,0xFE, // length of service data , service data , eddystone ID , (type of service data)
0x0e, 0xFf, 0x03,'r','b','.','g','y','/','p','z','g','j','7','r' // with url shortener
};

可以网上找下 BLE 广播结构参考下,如 https://www.cnblogs.com/debugdabiaoge/p/15772546.html ,广播结构不正确,手机会过滤掉这些广播不显示

User avatar
666hjk
Posts: 47
Joined: Wed Jan 12, 2022 3:09 am
Location: 小红点

(搞定了 )Re: (esp32c3)eddystone url 用 coded phy 可以吗?

Postby 666hjk » Thu Oct 13, 2022 10:43 am

ESP_XuLZ wrote:
Thu Oct 13, 2022 3:59 am
广播结构不对

multi-adv demo 只修改如下内容再试下
static uint8_t raw_scan_rsp_data_coded[] = {
0x02, 0x01, 0x06, // ( type of flag )
0x03, 0x03, 0xAA,0xFE, // length of service list , service list , eddystone ID , (16-bit uuid type)
0x03, 0x16, 0xAA,0xFE, // length of service data , service data , eddystone ID , (type of service data)
0x0e, 0xFf, 0x03,'r','b','.','g','y','/','p','z','g','j','7','r' // with url shortener
};

可以网上找下 BLE 广播结构参考下,如 https://www.cnblogs.com/debugdabiaoge/p/15772546.html ,广播结构不正确,手机会过滤掉这些广播不显示


ESP_XuLZ ,

搞定了! 谢谢你。一天都亮了。

问题在 1)用scan response data
2) service data 的 长度 应该是 0x12
3) eddystone url frame 是 0x10

如果这eddystone url 广播要用中文名, scan时能用中文字显示在手机上吗?
学习中找战友。

Who is online

Users browsing this forum: Google [Bot] and 185 guests