|
| 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