@ -15,39 +15,40 @@
# include "nimconfig.h"
# if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
# include "NimBLEScan.h"
# include "NimBLEDevice.h"
# include "NimBLELog.h"
# include "NimBLEScan.h"
# include "NimBLEDevice.h"
# include "NimBLELog.h"
# include <string>
# include <climits>
# if defined(CONFIG_NIMBLE_CPP_IDF)
# include "nimble / nimble_port.h"
# else
# include "nimble / porting / nimble / include / nimble / nimble_port.h"
# endif
# include <string>
# include <climits>
static const char * LOG_TAG = " NimBLEScan " ;
/**
* @brief Scan constuctor.
* @brief Scan constr uctor.
*/
NimBLEScan : : NimBLEScan ( ) {
m_s can_params . filter_policy = BLE_HCI_SCAN_FILT_NO_WL ;
m_scan_p arams . passive = 1 ; // If set, don’ t send scan requests to advertisers (i.e., don’ t request additional advertising data).
m_scan_params . itvl = 0 ; // This is defined as the time interval from when the Controller started its last LE scan until it begins the subsequent LE scan. (units=0.625 msec)
m_scan_params . window = 0 ; // The duration of the LE scan. LE_Scan_Window shall be less than or equal to LE_Scan_Interval (units=0.625 msec)
m_scan_params . limited = 0 ; // If set, only discover devices in limited discoverable mode.
m_scan_params . filter_duplicates = 1 ; // If set, the controller ignores all but the first advertisement from each device.
m_pScanCallbacks = nullptr ;
m_ignoreResults = false ;
m_pTaskData = nullptr ;
m_duration = BLE_HS_FOREVER ; // make sure this is non-zero in the event of a host reset
m_maxResults = 0xFF ;
NimBLEScan : : NimBLEScan ( )
: m_pS canCallbacks { nullptr } ,
m_scanP arams {
. itvl { 0 } , . window { 0 } , . filter_policy { BLE_HCI_SCAN_FILT_NO_WL } , . limited { 0 } , . passive { 1 } , . filter_duplicates { 1 } } ,
m_duration { BLE_HS_FOREVER } ,
m_pTaskData { nullptr } ,
m_maxResults { 0xFF } {
ble_npl_callout_init ( & m_srTimer , nimble_port_get_dflt_eventq ( ) , NimBLEScan : : srTimerCb , nullptr ) ;
}
/**
* @brief Scan destructor, release any allocated resources.
*/
NimBLEScan : : ~ NimBLEScan ( ) {
clearResults ( ) ;
clearResults ( ) ;
ble_npl_callout_deinit ( & m_srTimer ) ;
}
/**
@ -55,32 +56,26 @@ NimBLEScan::~NimBLEScan() {
* @param [in] event The event type for this event.
* @param [in] param Parameter data for this event.
*/
/*STATIC*/
int NimBLEScan : : handleGapEvent ( ble_gap_event * event , void * arg ) {
( void ) arg ;
NimBLEScan * pScan = NimBLEDevice : : getScan ( ) ;
switch ( event - > type ) {
switch ( event - > type ) {
case BLE_GAP_EVENT_EXT_DISC :
case BLE_GAP_EVENT_DISC : {
if ( pScan - > m_ignoreResults ) {
NIMBLE_LOGI ( LOG_TAG , " Scan op in progress - ignoring results " ) ;
return 0 ;
}
# if CONFIG_BT_NIMBLE_EXT_ADV
const auto & disc = event - > ext_ disc;
const bool isLegacyAdv = disc . props & BLE_HCI_ADV_LEGACY_MASK ;
const auto event_type = isLegacyAdv ? disc . legacy_event_type : disc . props ;
# else
const auto & disc = event - > disc ;
const bool isLegacyAdv = true ;
const auto event_type = disc . event_type ;
# endif
# if CONFIG_BT_NIMBLE_EXT_ADV
const auto & disc = event - > ext_disc ;
const bool isLegacyAdv = disc . props & BLE_HCI_ADV_LEGACY_MASK ;
const auto event_type = isLegacyAdv ? disc . legacy_event_type : disc . props ;
# else
const auto & disc = event - > disc ;
const bool isLegacyAdv = true ;
const auto event_type = disc . event_type ;
# endif
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
if ( NimBLEDevice : : isIgnored ( advertisedAddress ) ) {
// stop processing if we don't want to see it or are already connected
if ( NimBLEDevice : : isIgnored ( advertisedAddress ) ) {
NIMBLE_LOGI ( LOG_TAG , " Ignoring device: address: %s " , advertisedAddress . toString ( ) . c_str ( ) ) ;
return 0 ;
}
@ -88,77 +83,87 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
NimBLEAdvertisedDevice * advertisedDevice = nullptr ;
// If we've seen this device before get a pointer to it from the vector
for ( auto & it : pScan - > m_scanResults . m_advertisedD evices Vector ) {
# if CONFIG_BT_NIMBLE_EXT_ADV
for ( auto & ad : pScan - > m_scanResults . m_d eviceVec ) {
# if CONFIG_BT_NIMBLE_EXT_ADV
// Same address but different set ID should create a new advertised device.
if ( it - > getAddress ( ) = = advertisedAddress & & it - > getSetId ( ) = = disc . sid ) {
# else
if ( it - > getAddress ( ) = = advertisedAddress ) {
# endif
advertisedDevice = it ;
if ( ad - > getAddress ( ) = = advertisedAddress & & ad - > getSetId ( ) = = disc . sid ) {
# else
if ( ad - > getAddress ( ) = = advertisedAddress ) {
# endif
advertisedDevice = ad ;
break ;
}
}
// 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.
if ( advertisedDevice = = nullptr & &
( ! isLegacyAdv | | event_type ! = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP ) ) {
if ( advertisedDevice = = nullptr ) {
if ( event_type = = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP ) {
NIMBLE_LOGW ( LOG_TAG , " Scan response without advertisement: %s " , advertisedAddress . toString ( ) . c_str ( ) ) ;
}
// 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
if ( pScan - > m_maxResults > 0 & & pScan - > m_maxResults < 0xFF & &
( pScan - > m_scanResults . m_advertisedD evices Vector . size ( ) > = pScan - > m_maxResults ) ) {
( pScan - > m_scanResults . m_d eviceVec . size ( ) > = pScan - > m_maxResults ) ) {
return 0 ;
}
advertisedDevice = new NimBLEAdvertisedDevice ( event , event_type ) ;
pScan - > m_scanResults . m_advertisedD evices Vector . push_back ( advertisedDevice ) ;
pScan - > m_scanResults . m_d eviceVec . push_back ( advertisedDevice ) ;
NIMBLE_LOGI ( LOG_TAG , " New advertiser: %s " , advertisedAddress . toString ( ) . c_str ( ) ) ;
} else if ( advertisedDevice ! = nullptr ) {
advertisedDevice - > update ( event , event_type ) ;
NIMBLE_LOGI ( LOG_TAG , " Updated advertiser: %s " , advertisedAddress . toString ( ) . c_str ( ) ) ;
} else {
// Scan response from unknown device
// unknown device
return 0 ;
}
if ( pScan - > m_pScanCallbacks ) {
if ( advertisedDevice - > m_callbackSent = = 0 | | ! pScan - > m_scan_p arams . filter_duplicates ) {
if ( advertisedDevice - > m_callbackSent = = 0 | | ! pScan - > m_scanP arams . filter_duplicates ) {
advertisedDevice - > m_callbackSent = 1 ;
pScan - > m_pScanCallbacks - > onDiscovered ( advertisedDevice ) ;
}
if ( pScan - > m_scan_p arams . filter_duplicates & & advertisedDevice - > m_callbackSent > = 2 ) {
if ( pScan - > m_scanP arams . filter_duplicates & & advertisedDevice - > m_callbackSent > = 2 ) {
return 0 ;
}
// If not active scanning or scan response is not available
// or extended advertisement scanning, report the result to the callback now.
if ( pScan - > m_scan_p arams . passive | | ! isLegacyAdv | |
( advertisedDevice - > getAdvType ( ) ! = BLE_HCI_ADV_TYPE_ADV_IND & &
advertisedDevice - > getAdvType ( ) ! = BLE_HCI_ADV_TYPE_ADV_SCAN_IND ) )
{
if ( pScan - > m_scanP arams . passive | | ! isLegacyAdv | |
( advertisedDevice - > getAdvType ( ) ! = BLE_HCI_ADV_TYPE_ADV_IND & &
advertisedDevice - > getAdvType ( ) ! = BLE_HCI_ADV_TYPE_ADV_SCAN_IND ) ) {
advertisedDevice - > m_callbackSent = 2 ;
pScan - > m_pScanCallbacks - > 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 ( isLegacyAdv & & event_type = = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP ) {
advertisedDevice - > m_callbackSent = 2 ;
pScan - > m_pScanCallbacks - > onResult ( advertisedDevice ) ;
} else if ( isLegacyAdv ) {
ble_npl_time_t ticks ;
ble_npl_time_ms_to_ticks ( 500 , & ticks ) ; // more than 500ms for scan response = not coming
advertisedDevice - > m_srTimeout = ble_npl_time_get ( ) + ticks ;
if ( ! ble_npl_callout_is_active ( & pScan - > m_srTimer ) ) {
ble_npl_callout_reset ( & pScan - > m_srTimer , ticks ) ;
}
}
// If not storing results and we have invoked the callback, delete the device.
if ( pScan - > m_maxResults = = 0 & & advertisedDevice - > m_callbackSent > = 2 ) {
if ( pScan - > m_maxResults = = 0 & & advertisedDevice - > m_callbackSent > = 2 ) {
pScan - > erase ( advertisedAddress ) ;
}
}
return 0 ;
}
case BLE_GAP_EVENT_DISC_COMPLETE : {
NIMBLE_LOGD ( LOG_TAG , " discovery complete; reason=%d " ,
event - > disc_complete . reason ) ;
if ( pScan - > m_maxResults = = 0 ) {
case BLE_GAP_EVENT_DISC_COMPLETE : {
NIMBLE_LOGD ( LOG_TAG , " discovery complete; reason=%d " , event - > disc_complete . reason ) ;
if ( pScan - > m_maxResults = = 0 ) {
pScan - > clearResults ( ) ;
}
@ -166,7 +171,7 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
pScan - > m_pScanCallbacks - > onScanEnd ( pScan - > m_scanResults ) ;
}
if ( pScan - > m_pTaskData ! = nullptr ) {
if ( pScan - > m_pTaskData ! = nullptr ) {
NimBLEUtils : : taskRelease ( * pScan - > m_pTaskData , event - > disc_complete . reason ) ;
}
@ -176,8 +181,42 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
default :
return 0 ;
}
} // gapEventHandler
} // handleGapEvent
/**
* @brief This will call the scan result callback when a scan response is not received
* and will delete the device from the vector when maxResults is 0.
*/
void NimBLEScan : : srTimerCb ( ble_npl_event * event ) {
NimBLEScan * pScan = NimBLEDevice : : getScan ( ) ;
ble_npl_time_t now = ble_npl_time_get ( ) ;
ble_npl_time_t nextTimeout = BLE_NPL_TIME_FOREVER ;
for ( auto & ad : pScan - > m_scanResults . m_deviceVec ) {
if ( ! ad - > m_srTimeout ) {
continue ;
}
if ( ad - > m_callbackSent = = 1 & & now > ad - > m_srTimeout ) {
ad - > m_callbackSent = 2 ;
pScan - > m_pScanCallbacks - > onResult ( ad ) ;
if ( pScan - > m_maxResults = = 0 ) {
pScan - > erase ( ad - > getAddress ( ) ) ;
}
continue ;
}
if ( ad - > m_callbackSent = = 1 & & ad - > m_srTimeout < nextTimeout ) {
nextTimeout = ad - > m_srTimeout ;
}
}
if ( nextTimeout ! = BLE_NPL_TIME_FOREVER ) {
ble_npl_callout_reset ( & pScan - > m_srTimer , nextTimeout ) ;
}
} // srTimerCb
/**
* @brief Should we perform an active or passive scan?
@ -185,10 +224,9 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
* @param [in] active If true, we perform an active scan otherwise a passive scan.
*/
void NimBLEScan : : setActiveScan ( bool active ) {
m_scan_p arams . passive = ! active ;
m_scanP arams . passive = ! active ;
} // setActiveScan
/**
* @brief Set whether or not the BLE controller should only report results
* from devices it has not already seen.
@ -197,20 +235,18 @@ void NimBLEScan::setActiveScan(bool active) {
* duplicate devices once the limit is reached.
*/
void NimBLEScan : : setDuplicateFilter ( bool enabled ) {
m_scan_p arams . filter_duplicates = enabled ;
m_scanP arams . filter_duplicates = enabled ;
} // setDuplicateFilter
/**
* @brief Set whether or not the BLE controller only report scan results
* from devices advertising in limited discovery mode, i.e. directed advertising.
* @param [in] enabled If true, only limited discovery devices will be in scan results.
*/
void NimBLEScan : : setLimitedOnly ( bool enabled ) {
m_scan_p arams . limited = enabled ;
m_scanP arams . limited = enabled ;
} // setLimited
/**
* @brief Sets the scan filter policy.
* @param [in] filter Can be one of:
@ -230,10 +266,9 @@ void NimBLEScan::setLimitedOnly(bool enabled) {
* resolvable private address.
*/
void NimBLEScan : : setFilterPolicy ( uint8_t filter ) {
m_scan_p arams . filter_policy = filter ;
m_scanP arams . filter_policy = filter ;
} // setFilterPolicy
/**
* @brief Sets the max number of results to store.
* @param [in] maxResults The number of results to limit storage to\n
@ -243,7 +278,6 @@ void NimBLEScan::setMaxResults(uint8_t maxResults) {
m_maxResults = maxResults ;
}
/**
* @brief Set the call backs to be invoked.
* @param [in] pScanCallbacks Call backs to be invoked.
@ -254,25 +288,22 @@ void NimBLEScan::setScanCallbacks(NimBLEScanCallbacks* pScanCallbacks, bool want
m_pScanCallbacks = pScanCallbacks ;
} // setScanCallbacks
/**
* @brief Set the interval to scan.
* @param [in] intervalMSecs The scan interval (how often) in milliseconds.
*/
void NimBLEScan : : setInterval ( uint16_t intervalMSecs ) {
m_scan_p arams . itvl = intervalMSecs / 0.625 ;
m_scanP arams . itvl = intervalMSecs / 0.625 ;
} // setInterval
/**
* @brief Set the window to actively scan.
* @param [in] windowMSecs How long to actively scan.
*/
void NimBLEScan : : setWindow ( uint16_t windowMSecs ) {
m_scan_p arams . window = windowMSecs / 0.625 ;
m_scanP arams . window = windowMSecs / 0.625 ;
} // setWindow
/**
* @brief Get the status of the scanner.
* @return true if scanning or scan starting.
@ -281,14 +312,15 @@ bool NimBLEScan::isScanning() {
return ble_gap_disc_active ( ) ;
}
/**
* @brief Start scanning.
* @param [in] duration The duration in milliseconds for which to scan. 0 == scan forever.
* @param [in] is_continue Set to true to save previous scan results, false to clear them.
* @param [in] restart Set to true to restart the scan if already in progress.
* this is useful to clear the duplicate filter so all devices are reported again.
* @return True if scan started or false if there was an error.
*/
bool NimBLEScan : : start ( uint32_t duration , bool is_continue ) {
bool NimBLEScan : : start ( uint32_t duration , bool is_continue , bool restart ) {
NIMBLE_LOGD ( LOG_TAG , " >> start: duration=% " PRIu32 , duration ) ;
if ( ble_gap_conn_active ( ) ) {
@ -296,51 +328,52 @@ bool NimBLEScan::start(uint32_t duration, bool is_continue) {
return false ;
}
if ( isScanning ( ) ) {
if ( restart ) {
// NIMBLE_LOGI(LOG_TAG, "Scan already in progress, stopping it");
if ( ! stop ( ) ) {
return false ;
}
} else {
NIMBLE_LOGI ( LOG_TAG , " Scan already in progress " ) ;
return true ;
}
}
if ( ! is_continue ) {
clearResults ( ) ;
}
// Save the duration in the case that the host is reset so we can reuse it.
m_duration = duration ;
// If 0 duration specified then we assume a continuous scan is desired.
if ( duration = = 0 ) {
if ( duration = = 0 ) {
duration = BLE_HS_FOREVER ;
}
// Set the flag to ignore the results while we are deleting the vector
if ( ! is_continue ) {
m_ignoreResults = true ;
}
# if CONFIG_BT_NIMBLE_EXT_ADV
ble_gap_ext_disc_params scan_params ;
scan_params . passive = m_scan_p arams . passive ;
scan_params . itvl = m_scan_p arams . itvl ;
scan_params . window = m_scan_p arams . window ;
int rc = ble_gap_ext_disc ( NimBLEDevice : : m_ownAddrType ,
duration / 10 ,
scan_params . passive = m_scanP arams . passive ;
scan_params . itvl = m_scanP arams . itvl ;
scan_params . window = m_scanP arams . window ;
int rc = ble_gap_ext_disc ( NimBLEDevice : : m_ownAddrType ,
duration / 10 ,
0 ,
m_scan_p arams . filter_duplicates ,
m_scan_p arams . filter_policy ,
m_scan_p arams . limited ,
m_scanP arams . filter_duplicates ,
m_scanP arams . filter_policy ,
m_scanP arams . limited ,
& scan_params ,
& scan_params ,
NimBLEScan : : handleGapEvent ,
NULL ) ;
# else
int rc = ble_gap_disc ( NimBLEDevice : : m_ownAddrType ,
duration ,
& m_scan_params ,
NimBLEScan : : handleGapEvent ,
NULL ) ;
# endif
switch ( rc ) {
# else
int rc = ble_gap_disc ( NimBLEDevice : : m_ownAddrType , duration , & m_scanParams , NimBLEScan : : handleGapEvent , NULL ) ;
# endif
switch ( rc ) {
case 0 :
if ( ! is_continue ) {
clearResults ( ) ;
}
break ;
case BLE_HS_EALREADY :
// Clear the cache if already scanning in case an advertiser was missed.
clearDuplicateCache ( ) ;
NIMBLE_LOGD ( LOG_TAG , " Scan started " ) ;
break ;
case BLE_HS_EBUSY :
@ -355,21 +388,14 @@ bool NimBLEScan::start(uint32_t duration, bool is_continue) {
break ;
default :
NIMBLE_LOGE ( LOG_TAG , " Error initiating GAP discovery procedure; rc=%d, %s " ,
rc , NimBLEUtils : : returnCodeToString ( rc ) ) ;
NIMBLE_LOGE ( LOG_TAG , " Error starting scan; rc=%d, %s " , rc , NimBLEUtils : : returnCodeToString ( rc ) ) ;
break ;
}
m_ignoreResults = false ;
NIMBLE_LOGD ( LOG_TAG , " << start() " ) ;
if ( rc ! = 0 & & rc ! = BLE_HS_EALREADY ) {
return false ;
}
return true ;
return rc = = 0 | | rc = = BLE_HS_EALREADY ;
} // start
/**
* @brief Stop an in progress scan.
* @return True if successful.
@ -383,15 +409,15 @@ bool NimBLEScan::stop() {
return false ;
}
if ( m_maxResults = = 0 ) {
if ( m_pScanCallbacks ) {
ble_npl_callout_stop ( & m_srTimer ) ;
}
if ( m_maxResults = = 0 ) {
clearResults ( ) ;
}
if ( rc ! = BLE_HS_EALREADY & & m_pScanCallbacks ! = nullptr ) {
m_pScanCallbacks - > onScanEnd ( m_scanResults ) ;
}
if ( m_pTaskData ! = nullptr ) {
if ( m_pTaskData ! = nullptr ) {
NimBLEUtils : : taskRelease ( * m_pTaskData ) ;
}
@ -399,56 +425,32 @@ bool NimBLEScan::stop() {
return true ;
} // stop
/**
* @brief Clears the duplicate scan filter cache.
*/
void NimBLEScan : : clearDuplicateCache ( ) {
# ifdef CONFIG_IDF_TARGET_ESP32 // Not available for ESP32C3
esp_ble_scan_dupilcate_list_flush ( ) ;
# endif
}
/**
* @brief Delete peer device from the scan results vector.
* @param [in] address The address of the device to delete from the results.
* @details After disconnecting, it may be required in the case we were connected to a device without a public address.
*/
void NimBLEScan : : erase ( const NimBLEAddress & address ) {
void NimBLEScan : : erase ( const NimBLEAddress & address ) {
NIMBLE_LOGD ( LOG_TAG , " erase device: %s " , address . toString ( ) . c_str ( ) ) ;
for ( auto it = m_scanResults . m_advertisedDevicesVector . begin ( ) ; it ! = m_scanResults . m_advertisedDevicesVector . end ( ) ; + + it ) {
if ( ( * it ) - > getAddress ( ) = = address ) {
for ( auto it = m_scanResults . m_deviceVec . begin ( ) ; it ! = m_scanResults . m_deviceVec . end ( ) ; + + it ) {
if ( ( * it ) - > getAddress ( ) = = address ) {
delete * it ;
m_scanResults . m_advertisedD evices Vector . erase ( it ) ;
m_scanResults . m_d eviceVec . erase ( it ) ;
break ;
}
}
}
/**
* @brief Called when host reset, we set a flag to stop scanning until synced.
*/
void NimBLEScan : : onHostReset ( ) {
m_ignoreResults = true ;
}
/**
* @brief If the host reset and re-synced this is called.
* If the application was scanning indefinitely with a callback, restart it.
*/
void NimBLEScan : : onHostSync ( ) {
m_ignoreResults = false ;
if ( m_duration = = 0 & & m_pScanCallbacks ! = nullptr ) {
if ( m_duration = = 0 & & m_pScanCallbacks ! = nullptr ) {
start ( 0 , false ) ;
}
}
/**
* @brief Start scanning and block until scanning has been completed.
* @param [in] duration The duration in milliseconds for which to scan.
@ -456,14 +458,19 @@ void NimBLEScan::onHostSync() {
* @return The scan results.
*/
NimBLEScanResults NimBLEScan : : getResults ( uint32_t duration , bool is_continue ) {
if ( duration = = 0 ) {
if ( duration = = 0 ) {
NIMBLE_LOGW ( LOG_TAG , " Blocking scan called with duration = forever " ) ;
}
if ( m_pTaskData ! = nullptr ) {
NIMBLE_LOGE ( LOG_TAG , " Scan already in progress " ) ;
return m_scanResults ;
}
NimBLETaskData taskData ;
m_pTaskData = & taskData ;
if ( start ( duration , is_continue ) ) {
if ( start ( duration , is_continue ) ) {
NimBLEUtils : : taskWait ( taskData , BLE_NPL_TIME_FOREVER ) ;
}
@ -471,7 +478,6 @@ NimBLEScanResults NimBLEScan::getResults(uint32_t duration, bool is_continue) {
return m_scanResults ;
} // getResults
/**
* @brief Get the results of the scan.
* @return NimBLEScanResults object.
@ -480,39 +486,34 @@ NimBLEScanResults NimBLEScan::getResults() {
return m_scanResults ;
}
/**
* @brief Clear the results of the scan.
*/
void NimBLEScan : : clearResults ( ) {
for ( auto & it : m_scanResults . m_advertisedD evices Vector ) {
for ( auto & it : m_scanResults . m_d eviceVec ) {
delete it ;
}
m_scanResults . m_advertisedDevicesVector . clear ( ) ;
clearDuplicateCache ( ) ;
}
std : : vector < NimBLEAdvertisedDevice * > ( ) . swap ( m_scanResults . m_deviceVec ) ;
} // clearResults
/**
* @brief Dump the scan results to the log.
*/
void NimBLEScanResults : : dump ( ) {
NIMBLE_LOGD ( LOG_TAG , " >> Dump scan results: " ) ;
for ( int i = 0 ; i < getCount ( ) ; i + + ) {
for ( int i = 0 ; i < getCount ( ) ; i + + ) {
NIMBLE_LOGI ( LOG_TAG , " - %s " , getDevice ( i ) . toString ( ) . c_str ( ) ) ;
}
} // dump
/**
* @brief Get the count of devices found in the last scan.
* @return The number of devices found in the last scan.
*/
int NimBLEScanResults : : getCount ( ) {
return m_advertisedD evices Vector . size ( ) ;
return m_d eviceVec . size ( ) ;
} // getCount
/**
* @brief Return the specified device at the given index.
* The index should be between 0 and getCount()-1.
@ -520,38 +521,35 @@ int NimBLEScanResults::getCount() {
* @return The device at the specified index.
*/
NimBLEAdvertisedDevice NimBLEScanResults : : getDevice ( uint32_t i ) {
return * m_advertisedD evices Vector [ i ] ;
return * m_d eviceVec [ i ] ;
}
/**
* @brief Get iterator to the beginning of the vector of advertised device pointers.
* @return An iterator to the beginning of the vector of advertised device pointers.
*/
std : : vector < NimBLEAdvertisedDevice * > : : iterator NimBLEScanResults : : begin ( ) {
return m_advertisedD evices Vector . begin ( ) ;
return m_d eviceVec . begin ( ) ;
}
/**
* @brief Get iterator to the end of the vector of advertised device pointers.
* @return An iterator to the end of the vector of advertised device pointers.
*/
std : : vector < NimBLEAdvertisedDevice * > : : iterator NimBLEScanResults : : end ( ) {
return m_advertisedD evices Vector . end ( ) ;
return m_d eviceVec . end ( ) ;
}
/**
* @brief Get a pointer to the specified device at the given address.
* If the address is not found a nullptr is returned.
* @param [in] address The address of the device.
* @return A pointer to the device at the specified address.
*/
NimBLEAdvertisedDevice * NimBLEScanResults : : getDevice ( const NimBLEAddress & address ) {
for ( size_t index = 0 ; index < m_advertisedD evices Vector . size ( ) ; index + + ) {
if ( m_advertisedD evices Vector [ index ] - > getAddress ( ) = = address ) {
return m_advertisedD evices Vector [ index ] ;
NimBLEAdvertisedDevice * NimBLEScanResults : : getDevice ( const NimBLEAddress & address ) {
for ( size_t index = 0 ; index < m_d eviceVec . size ( ) ; index + + ) {
if ( m_d eviceVec [ index ] - > getAddress ( ) = = address ) {
return m_d eviceVec [ index ] ;
}
}