Skip to content

Commit 7856de7

Browse files
authored
BLE5 features to use with C3/S3 (#5085)
Added new BLE5 features to use on C3/S3 family: extended scan, extended/multi advertising New code is not fancy (no feedback from events), but i think it is functional. To get feedback from events i am suggesting to use custom GAP callback, which is already implemented in BLEDevice.
1 parent e62ff6d commit 7856de7

17 files changed

+909
-1
lines changed

libraries/BLE/examples/BLE5_extended_scan/.skip.esp32

Whitespace-only changes.

libraries/BLE/examples/BLE5_extended_scan/.skip.esp32s2

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
#include <BLEAdvertisedDevice.h>
17+
18+
uint32_t scanTime = 100; //In 10ms (1000ms)
19+
BLEScan* pBLEScan;
20+
21+
/**
22+
* @brief extend adv report parameters
23+
*/
24+
//typedef struct {
25+
// esp_ble_gap_adv_type_t event_type; /*!< extend advertising type */
26+
// uint8_t addr_type; /*!< extend advertising address type */
27+
// esp_bd_addr_t addr; /*!< extend advertising address */
28+
// esp_ble_gap_pri_phy_t primary_phy; /*!< extend advertising primary phy */
29+
// esp_ble_gap_phy_t secondly_phy; /*!< extend advertising secondary phy */
30+
// uint8_t sid; /*!< extend advertising sid */
31+
// uint8_t tx_power; /*!< extend advertising tx power */
32+
// int8_t rssi; /*!< extend advertising rssi */
33+
// uint16_t per_adv_interval; /*!< periodic advertising interval */
34+
// uint8_t dir_addr_type; /*!< direct address type */
35+
// esp_bd_addr_t dir_addr; /*!< direct address */
36+
// esp_ble_gap_ext_adv_data_status_t data_status; /*!< data type */
37+
// uint8_t adv_data_len; /*!< extend advertising data length */
38+
// uint8_t adv_data[251]; /*!< extend advertising data */
39+
//} esp_ble_gap_ext_adv_reprot_t;
40+
41+
class MyBLEExtAdvertisingCallbacks: public BLEExtAdvertisingCallbacks {
42+
void onResult(esp_ble_gap_ext_adv_reprot_t report) {
43+
if(report.event_type & ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY){
44+
// here we can receive regular advertising data from BLE4.x devices
45+
Serial.println("BLE4.2");
46+
} else {
47+
// here we will get extended advertising data that are advertised over data channel by BLE5 divices
48+
Serial.printf("Ext advertise: data_le: %d, data_status: %d \n", report.adv_data_len, report.data_status);
49+
}
50+
}
51+
};
52+
53+
void setup() {
54+
Serial.begin(115200);
55+
Serial.println("Scanning...");
56+
57+
BLEDevice::init("");
58+
pBLEScan = BLEDevice::getScan(); //create new scan
59+
pBLEScan->setExtendedScanCallback(new MyBLEExtAdvertisingCallbacks());
60+
pBLEScan->setExtScanParams(); // use with pre-defined/default values, overloaded function allows to pass parameters
61+
delay(1000); // it is just for simplicity this example, to let ble stack to set extended scan params
62+
pBLEScan->startExtScan(scanTime, 3); // scan duration in n * 10ms, period - repeat after n seconds (period >= duration)
63+
}
64+
65+
void loop() {
66+
// put your main code here, to run repeatedly:
67+
delay(2000);
68+
}
69+
#endif // CONFIG_BT_BLE_50_FEATURES_SUPPORTED

libraries/BLE/examples/BLE5_multi_advertising/.skip.esp32

Whitespace-only changes.

libraries/BLE/examples/BLE5_multi_advertising/.skip.esp32s2

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
Simple BLE5 multi advertising example on esp32 C3/S3
3+
only ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY_IND is backward compatible
4+
and can be scanned with BLE4.2 devices
5+
6+
author: chegewara
7+
*/
8+
9+
#include <BLEDevice.h>
10+
#include <BLEAdvertising.h>
11+
12+
esp_ble_gap_ext_adv_params_t ext_adv_params_1M = {
13+
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_CONNECTABLE,
14+
.interval_min = 0x30,
15+
.interval_max = 0x30,
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_CODED,
20+
.max_skip = 0,
21+
.secondary_phy = ESP_BLE_GAP_PHY_1M,
22+
.sid = 0,
23+
.scan_req_notif = false,
24+
};
25+
26+
esp_ble_gap_ext_adv_params_t ext_adv_params_2M = {
27+
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_SCANNABLE,
28+
.interval_min = 0x40,
29+
.interval_max = 0x40,
30+
.channel_map = ADV_CHNL_ALL,
31+
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
32+
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
33+
.primary_phy = ESP_BLE_GAP_PHY_1M,
34+
.max_skip = 0,
35+
.secondary_phy = ESP_BLE_GAP_PHY_2M,
36+
.sid = 1,
37+
.scan_req_notif = false,
38+
};
39+
40+
esp_ble_gap_ext_adv_params_t legacy_adv_params = {
41+
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_LEGACY_IND,
42+
.interval_min = 0x45,
43+
.interval_max = 0x45,
44+
.channel_map = ADV_CHNL_ALL,
45+
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
46+
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
47+
.primary_phy = ESP_BLE_GAP_PHY_1M,
48+
.max_skip = 0,
49+
.secondary_phy = ESP_BLE_GAP_PHY_1M,
50+
.sid = 2,
51+
.scan_req_notif = false,
52+
};
53+
54+
esp_ble_gap_ext_adv_params_t ext_adv_params_coded = {
55+
.type = ESP_BLE_GAP_SET_EXT_ADV_PROP_SCANNABLE,
56+
.interval_min = 0x50,
57+
.interval_max = 0x50,
58+
.channel_map = ADV_CHNL_ALL,
59+
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
60+
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
61+
.primary_phy = ESP_BLE_GAP_PHY_1M,
62+
.max_skip = 0,
63+
.secondary_phy = ESP_BLE_GAP_PHY_CODED,
64+
.sid = 3,
65+
.scan_req_notif = false,
66+
};
67+
68+
static uint8_t raw_adv_data_1m[] = {
69+
0x02, 0x01, 0x06,
70+
0x02, 0x0a, 0xeb,
71+
0x12, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
72+
'D', 'V', '_', '1', 'M', 0X0
73+
};
74+
75+
static uint8_t raw_scan_rsp_data_2m[] = {
76+
0x02, 0x01, 0x06,
77+
0x02, 0x0a, 0xeb,
78+
0x12, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
79+
'D', 'V', '_', '2', 'M', 0X0
80+
};
81+
82+
static uint8_t legacy_adv_data[] = {
83+
0x02, 0x01, 0x06,
84+
0x02, 0x0a, 0xeb,
85+
0x15, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
86+
'D', 'V', '_', 'C', 'O', 'D', 'E', 'D', 0X0
87+
};
88+
89+
static uint8_t legacy_scan_rsp_data[] = {
90+
0x02, 0x01, 0x06,
91+
0x02, 0x0a, 0xeb,
92+
0x16, 0x09, 'E', 'S', 'P', '_', 'M', 'U', 'L', 'T', 'I', '_', 'A',
93+
'D', 'V', '_', 'L', 'E', 'G', 'A', 'C', 'Y', 0X0
94+
};
95+
96+
static uint8_t raw_scan_rsp_data_coded[] = {
97+
0x37, 0x09, 'V', 'E', 'R', 'Y', '_', 'L', 'O', 'N', 'G', '_', 'D', 'E', 'V', 'I', 'C', 'E', '_', 'N', 'A', 'M', 'E', '_',
98+
'S', 'E', 'N', 'T', '_', 'U', 'S', 'I', 'N', 'G', '_', 'E', 'X', 'T', 'E', 'N', 'D', 'E', 'D', '_', 'A', 'D', 'V', 'E', 'R', 'T', 'I', 'S', 'I', 'N', 'G', 0X0
99+
};
100+
101+
102+
uint8_t addr_1m[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x01};
103+
uint8_t addr_2m[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x02};
104+
uint8_t addr_legacy[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x03};
105+
uint8_t addr_coded[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x04};
106+
107+
BLEMultiAdvertising advert(4); // max number of advertisement data
108+
109+
void setup() {
110+
Serial.begin(115200);
111+
Serial.println("Multi-Advertising...");
112+
113+
BLEDevice::init("");
114+
115+
advert.setAdvertisingParams(0, &ext_adv_params_1M);
116+
advert.setAdvertisingData(0, sizeof(raw_adv_data_1m), &raw_adv_data_1m[0]);
117+
advert.setInstanceAddress(0, addr_1m);
118+
advert.setDuration(0);
119+
120+
advert.setAdvertisingParams(1, &ext_adv_params_2M);
121+
advert.setScanRspData(1, sizeof(raw_scan_rsp_data_2m), &raw_scan_rsp_data_2m[0]);
122+
advert.setInstanceAddress(1, addr_2m);
123+
advert.setDuration(1);
124+
125+
advert.setAdvertisingParams(2, &legacy_adv_params);
126+
advert.setAdvertisingData(2, sizeof(legacy_adv_data), &legacy_adv_data[0]);
127+
advert.setScanRspData(2, sizeof(legacy_scan_rsp_data), &legacy_scan_rsp_data[0]);
128+
advert.setInstanceAddress(2, addr_legacy);
129+
advert.setDuration(2);
130+
131+
advert.setAdvertisingParams(3, &ext_adv_params_coded);
132+
advert.setDuration(3);
133+
advert.setScanRspData(3, sizeof(raw_scan_rsp_data_coded), &raw_scan_rsp_data_coded[0]);
134+
advert.setInstanceAddress(3, addr_coded);
135+
136+
delay(1000);
137+
advert.start(4, 0);
138+
}
139+
140+
void loop() {
141+
delay(2000);
142+
}

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

Whitespace-only changes.

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
};
46+
47+
48+
uint8_t addr_2m[6] = {0xc0, 0xde, 0x52, 0x00, 0x00, 0x02};
49+
50+
BLEMultiAdvertising advert(1); // max number of advertisement data
51+
52+
void setup() {
53+
Serial.begin(115200);
54+
Serial.println("Multi-Advertising...");
55+
56+
BLEDevice::init("");
57+
58+
advert.setAdvertisingParams(0, &ext_adv_params_2M);
59+
advert.setAdvertisingData(0, sizeof(raw_scan_rsp_data_2m), &raw_scan_rsp_data_2m[0]);
60+
advert.setInstanceAddress(0, addr_2m);
61+
advert.setDuration(0, 0, 0);
62+
63+
delay(100);
64+
advert.start();
65+
advert.setPeriodicAdvertisingParams(0, &periodic_adv_params);
66+
advert.setPeriodicAdvertisingData(0, sizeof(periodic_adv_raw_data), &periodic_adv_raw_data[0]);
67+
advert.startPeriodicAdvertising(0);
68+
}
69+
70+
void loop() {
71+
delay(2000);
72+
}

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

Whitespace-only changes.

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

Whitespace-only changes.

0 commit comments

Comments
 (0)