Added extended advertising scan support (#69)

This commit is contained in:
jvrobert 2022-02-06 16:52:42 -07:00 committed by GitHub
parent 2e498cef2b
commit 288ee92d39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -61,13 +61,22 @@ NimBLEScan::~NimBLEScan() {
switch(event->type) { switch(event->type) {
case BLE_GAP_EVENT_EXT_DISC:
case BLE_GAP_EVENT_DISC: { case BLE_GAP_EVENT_DISC: {
if(pScan->m_ignoreResults) { if(pScan->m_ignoreResults) {
NIMBLE_LOGI(LOG_TAG, "Scan op in progress - ignoring results"); NIMBLE_LOGI(LOG_TAG, "Scan op in progress - ignoring results");
return 0; return 0;
} }
#if MYNEWT_VAL(BLE_EXT_ADV)
const auto& disc = event->ext_disc;
const auto event_type = disc.legacy_event_type;
#else
const auto& disc = event->disc;
const auto event_type = disc.event_type;
#endif
NimBLEAddress advertisedAddress(event->disc.addr); NimBLEAddress advertisedAddress(disc.addr);
// Examine our list of ignored addresses and stop processing if we don't want to see it or are already connected // Examine our list of ignored addresses and stop processing if we don't want to see it or are already connected
if(NimBLEDevice::isIgnored(advertisedAddress)) { if(NimBLEDevice::isIgnored(advertisedAddress)) {
@ -87,7 +96,7 @@ NimBLEScan::~NimBLEScan() {
// If we haven't seen this device before; create a new instance and insert it in the vector. // If we haven't seen this device before; create a new instance and insert it in the vector.
// Otherwise just update the relevant parameters of the already known device. // Otherwise just update the relevant parameters of the already known device.
if(advertisedDevice == nullptr && event->disc.event_type != BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP){ if(advertisedDevice == nullptr && event_type != BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP){
// Check if we have reach the scan results limit, ignore this one if so. // Check if we have reach the scan results limit, ignore this one if so.
// We still need to store each device when maxResults is 0 to be able to append the scan results // We still need to store each device when maxResults is 0 to be able to append the scan results
if(pScan->m_maxResults > 0 && pScan->m_maxResults < 0xFF && if(pScan->m_maxResults > 0 && pScan->m_maxResults < 0xFF &&
@ -97,7 +106,7 @@ NimBLEScan::~NimBLEScan() {
} }
advertisedDevice = new NimBLEAdvertisedDevice(); advertisedDevice = new NimBLEAdvertisedDevice();
advertisedDevice->setAddress(advertisedAddress); advertisedDevice->setAddress(advertisedAddress);
advertisedDevice->setAdvType(event->disc.event_type); advertisedDevice->setAdvType(event_type);
pScan->m_scanResults.m_advertisedDevicesVector.push_back(advertisedDevice); pScan->m_scanResults.m_advertisedDevicesVector.push_back(advertisedDevice);
NIMBLE_LOGI(LOG_TAG, "New advertiser: %s", advertisedAddress.toString().c_str()); NIMBLE_LOGI(LOG_TAG, "New advertiser: %s", advertisedAddress.toString().c_str());
} else if(advertisedDevice != nullptr) { } else if(advertisedDevice != nullptr) {
@ -108,9 +117,9 @@ NimBLEScan::~NimBLEScan() {
} }
advertisedDevice->m_timestamp = time(nullptr); advertisedDevice->m_timestamp = time(nullptr);
advertisedDevice->setRSSI(event->disc.rssi); advertisedDevice->setRSSI(disc.rssi);
advertisedDevice->setPayload(event->disc.data, event->disc.length_data, advertisedDevice->setPayload(disc.data, disc.length_data,
event->disc.event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP); event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP);
if (pScan->m_pAdvertisedDeviceCallbacks) { if (pScan->m_pAdvertisedDeviceCallbacks) {
// If not active scanning or scan response is not available // If not active scanning or scan response is not available
@ -123,7 +132,7 @@ NimBLEScan::~NimBLEScan() {
pScan->m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice); pScan->m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice);
// Otherwise, wait for the scan response so we can report the complete data. // Otherwise, wait for the scan response so we can report the complete data.
} else if (event->disc.event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) { } else if (event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
advertisedDevice->m_callbackSent = true; advertisedDevice->m_callbackSent = true;
pScan->m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice); pScan->m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice);
} }
@ -304,9 +313,18 @@ bool NimBLEScan::start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResul
m_ignoreResults = true; m_ignoreResults = true;
} }
#if MYNEWT_VAL(BLE_EXT_ADV)
ble_gap_ext_disc_params scan_params;
scan_params.passive = m_scan_params.passive;
scan_params.itvl = m_scan_params.itvl;
scan_params.window = m_scan_params.window;
int rc = ble_gap_ext_disc(NimBLEDevice::m_own_addr_type, duration/10, 0, m_scan_params.filter_duplicates, m_scan_params.filter_policy, m_scan_params.limited, &scan_params, &scan_params,
NimBLEScan::handleGapEvent, this);
#else
int rc = ble_gap_disc(NimBLEDevice::m_own_addr_type, duration, &m_scan_params, int rc = ble_gap_disc(NimBLEDevice::m_own_addr_type, duration, &m_scan_params,
NimBLEScan::handleGapEvent, this); NimBLEScan::handleGapEvent, this);
#endif
switch(rc) { switch(rc) {
case 0: case 0:
if(!is_continue) { if(!is_continue) {