Skip to content

Commit c7fa75a

Browse files
committed
Add periodic advertising with example,
add periodic sync with example
1 parent 2ada5ce commit c7fa75a

File tree

11 files changed

+378
-55
lines changed

11 files changed

+378
-55
lines changed

libraries/BLE/examples/BLE5_multi_advertising/BLE5_multi_advertising.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,26 @@ void setup() {
112112

113113
BLEDevice::init("");
114114

115-
advert.setAdvParams(0, &ext_adv_params_1M);
115+
advert.setAdvertisingParams(0, &ext_adv_params_1M);
116116
advert.setAdvertisingData(0, sizeof(raw_adv_data_1m), &raw_adv_data_1m[0]);
117-
advert.setPrivateAddress(0, addr_1m);
117+
advert.setInstanceAddress(0, addr_1m);
118118
advert.setDuration(0);
119119

120-
advert.setAdvParams(1, &ext_adv_params_2M);
120+
advert.setAdvertisingParams(1, &ext_adv_params_2M);
121121
advert.setScanRspData(1, sizeof(raw_scan_rsp_data_2m), &raw_scan_rsp_data_2m[0]);
122-
advert.setPrivateAddress(1, addr_2m);
122+
advert.setInstanceAddress(1, addr_2m);
123123
advert.setDuration(1);
124124

125-
advert.setAdvParams(2, &legacy_adv_params);
125+
advert.setAdvertisingParams(2, &legacy_adv_params);
126126
advert.setAdvertisingData(2, sizeof(legacy_adv_data), &legacy_adv_data[0]);
127127
advert.setScanRspData(2, sizeof(legacy_scan_rsp_data), &legacy_scan_rsp_data[0]);
128-
advert.setPrivateAddress(2, addr_legacy);
128+
advert.setInstanceAddress(2, addr_legacy);
129129
advert.setDuration(2);
130130

131-
advert.setAdvParams(3, &ext_adv_params_coded);
131+
advert.setAdvertisingParams(3, &ext_adv_params_coded);
132132
advert.setDuration(3);
133133
advert.setScanRspData(3, sizeof(raw_scan_rsp_data_coded), &raw_scan_rsp_data_coded[0]);
134-
advert.setPrivateAddress(3, addr_coded);
134+
advert.setInstanceAddress(3, addr_coded);
135135

136136
delay(1000);
137137
advert.start(4, 0);

libraries/BLE/examples/BLE5_periodic_advertising/.skip.esp32

Whitespace-only changes.

libraries/BLE/examples/BLE5_periodic_advertising/.skip.esp32s2

Whitespace-only changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Simple BLE5 multi advertising example on esp32 C3/S3
3+
only ESP_BLE_GAP_SET_EXT_ADV_PROP_NONCONN_NONSCANNABLE_UNDIRECTED can be used for periodic advertising
4+
5+
author: chegewara
6+
*/
7+
8+
#include <BLEDevice.h>
9+
#include <BLEAdvertising.h>
10+
11+
12+
esp_ble_gap_ext_adv_params_t ext_adv_params_2M = {
13+
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_NONCONN_NONSCANNABLE_UNDIRECTED,
14+
.interval_min = 0x40,
15+
.interval_max = 0x40,
16+
.channel_map = ADV_CHNL_ALL,
17+
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
18+
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
19+
.primary_phy = ESP_BLE_GAP_PHY_1M,
20+
.max_skip = 0,
21+
.secondary_phy = ESP_BLE_GAP_PHY_2M,
22+
.sid = 1,
23+
.scan_req_notif = false,
24+
};
25+
26+
static uint8_t raw_scan_rsp_data_2m[] = {
27+
0x02, 0x01, 0x06,
28+
0x02, 0x0a, 0xeb,
29+
0x12, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
30+
'D', 'V', '_', '2', 'M', 0X0
31+
};
32+
33+
static esp_ble_gap_periodic_adv_params_t periodic_adv_params = {
34+
.interval_min = 0x320, // 1000 ms interval
35+
.interval_max = 0x640,
36+
.properties = 0, // Do not include TX power
37+
};
38+
39+
static uint8_t periodic_adv_raw_data[] = {
40+
0x02, 0x01, 0x06,
41+
0x02, 0x0a, 0xeb,
42+
0x03, 0x03, 0xab, 0xcd,
43+
0x11, 0x09, 'E', 'S', 'P', '_', 'P', 'E', 'R', 'I', 'O', 'D', 'I',
44+
'C', '_', 'A', 'D', 'V',
45+
0x18, 0x24, 'h', 't', 't', 'p', ':', '/', '/', 'd', 'c', '4', '.', 'e', 's', 'p', '3', '2', '.', 'e', 'u', '.', 'o', 'r', 'g'
46+
};
47+
48+
49+
uint8_t addr_2m[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x02};
50+
51+
BLEMultiAdvertising advert(1); // max number of advertisement data
52+
53+
void setup() {
54+
Serial.begin(115200);
55+
Serial.println("Multi-Advertising...");
56+
57+
BLEDevice::init("");
58+
59+
advert.setAdvertisingParams(0, &ext_adv_params_2M);
60+
advert.setAdvertisingData(0, sizeof(raw_scan_rsp_data_2m), &raw_scan_rsp_data_2m[0]);
61+
advert.setPrivateAddress(0, addr_2m);
62+
advert.setDuration(0, 0, 0);
63+
64+
delay(100);
65+
advert.start();
66+
advert.setPeriodicAdvertisingParams(0, &periodic_adv_params);
67+
advert.setPeriodicAdvertisingData(0, sizeof(periodic_adv_raw_data), &periodic_adv_raw_data[0]);
68+
advert.startPeriodicAdvertising(0);
69+
}
70+
71+
void loop() {
72+
delay(2000);
73+
}

libraries/BLE/examples/BLE5_periodic_sync/.skip.esp32

Whitespace-only changes.

libraries/BLE/examples/BLE5_periodic_sync/.skip.esp32s2

Whitespace-only changes.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
BLE5 extended scan example for esp32 C3 and S3
3+
with this code it is simple to scan legacy (BLE4) compatible advertising,
4+
and BLE5 extended advertising. New coded added in BLEScan is not changing old behavior,
5+
which can be used with old esp32, but is adding functionality to use on C3/S3.
6+
With this new API advertised device wont be stored in API, it is now user responsibility
7+
8+
author: chegewara
9+
*/
10+
#ifndef CONFIG_BT_BLE_50_FEATURES_SUPPORTED
11+
#warning "Not compatible hardware"
12+
#else
13+
#include <BLEDevice.h>
14+
#include <BLEUtils.h>
15+
#include <BLEScan.h>
16+
17+
BLEScan *pBLEScan;
18+
static bool periodic_sync = false;
19+
20+
static esp_ble_gap_periodic_adv_sync_params_t periodic_adv_sync_params = {
21+
.filter_policy = 0,
22+
.sid = 0,
23+
.addr_type = BLE_ADDR_TYPE_RANDOM,
24+
.skip = 10,
25+
.sync_timeout = 1000, // timeout: 1000 * 10ms
26+
};
27+
28+
/**
29+
* @brief extend adv report parameters
30+
*/
31+
//typedef struct {
32+
// esp_ble_gap_adv_type_t event_type; /*!< extend advertising type */
33+
// uint8_t addr_type; /*!< extend advertising address type */
34+
// esp_bd_addr_t addr; /*!< extend advertising address */
35+
// esp_ble_gap_pri_phy_t primary_phy; /*!< extend advertising primary phy */
36+
// esp_ble_gap_phy_t secondly_phy; /*!< extend advertising secondary phy */
37+
// uint8_t sid; /*!< extend advertising sid */
38+
// uint8_t tx_power; /*!< extend advertising tx power */
39+
// int8_t rssi; /*!< extend advertising rssi */
40+
// uint16_t per_adv_interval; /*!< periodic advertising interval */
41+
// uint8_t dir_addr_type; /*!< direct address type */
42+
// esp_bd_addr_t dir_addr; /*!< direct address */
43+
// esp_ble_gap_ext_adv_data_status_t data_status; /*!< data type */
44+
// uint8_t adv_data_len; /*!< extend advertising data length */
45+
// uint8_t adv_data[251]; /*!< extend advertising data */
46+
//} esp_ble_gap_ext_adv_reprot_t;
47+
48+
class MyBLEExtAdvertisingCallbacks : public BLEExtAdvertisingCallbacks
49+
{
50+
void onResult(esp_ble_gap_ext_adv_reprot_t params)
51+
{
52+
uint8_t *adv_name = NULL;
53+
uint8_t adv_name_len = 0;
54+
adv_name = esp_ble_resolve_adv_data(params.adv_data, ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
55+
if ((adv_name != NULL) && (memcmp(adv_name, "ESP_MULTI_ADV_2M", adv_name_len) == 0) && !periodic_sync)
56+
{
57+
periodic_sync = true;
58+
char adv_temp_name[60] = {'0'};
59+
memcpy(adv_temp_name, adv_name, adv_name_len);
60+
log_i("Start create sync with the peer device %s", adv_temp_name);
61+
periodic_adv_sync_params.sid = params.sid;
62+
// periodic_adv_sync_params.addr_type = params.addr_type;
63+
memcpy(periodic_adv_sync_params.addr, params.addr, sizeof(esp_bd_addr_t));
64+
esp_ble_gap_periodic_adv_create_sync(&periodic_adv_sync_params);
65+
}
66+
}
67+
};
68+
69+
class MyPeriodicScan : public BLEPeriodicScanCallbacks
70+
{
71+
// void onCreateSync(esp_bt_status_t status){}
72+
// void onCancelSync(esp_bt_status_t status){}
73+
// void onTerminateSync(esp_bt_status_t status){}
74+
75+
void onStop(esp_bt_status_t status)
76+
{
77+
log_i("ESP_GAP_BLE_EXT_SCAN_STOP_COMPLETE_EVT");
78+
periodic_sync = false;
79+
pBLEScan->startExtScan(0, 0); // scan duration in n * 10ms, period - repeat after n seconds (period >= duration)
80+
}
81+
82+
void onLostSync(uint16_t sync_handle)
83+
{
84+
log_i("ESP_GAP_BLE_PERIODIC_ADV_SYNC_LOST_EVT");
85+
esp_ble_gap_stop_ext_scan();
86+
}
87+
88+
void onSync(esp_ble_periodic_adv_sync_estab_param_t params)
89+
{
90+
log_i("ESP_GAP_BLE_PERIODIC_ADV_SYNC_ESTAB_EVT, status %d", params.status);
91+
// esp_log_buffer_hex("sync addr", param->periodic_adv_sync_estab.adv_addr, 6);
92+
log_i("sync handle %d sid %d perioic adv interval %d adv phy %d", params.sync_handle,
93+
params.sid,
94+
params.period_adv_interval,
95+
params.adv_phy);
96+
}
97+
98+
void onReport(esp_ble_gap_periodic_adv_report_t params)
99+
{
100+
log_i("periodic adv report, sync handle %d data status %d data len %d rssi %d", params.sync_handle,
101+
params.data_status,
102+
params.data_length,
103+
params.rssi);
104+
}
105+
};
106+
107+
void setup()
108+
{
109+
Serial.begin(115200);
110+
Serial.println("Periodic scan...");
111+
112+
BLEDevice::init("");
113+
pBLEScan = BLEDevice::getScan(); //create new scan
114+
pBLEScan->setExtendedScanCallback(new MyBLEExtAdvertisingCallbacks());
115+
pBLEScan->setExtScanParams(); // use with pre-defined/default values, overloaded function allows to pass parameters
116+
pBLEScan->setPeriodicScanCallback(new MyPeriodicScan());
117+
delay(100); // it is just for simplicity this example, to let ble stack to set extended scan params
118+
pBLEScan->startExtScan(0, 0);
119+
120+
}
121+
122+
void loop()
123+
{
124+
delay(2000);
125+
}
126+
127+
#endif // CONFIG_BT_BLE_50_FEATURES_SUPPORTED

0 commit comments

Comments
 (0)