Skip to content

Commit 943df90

Browse files
committed
[BREAKING] - Refactor NimBLEScan
* General code cleanup * `NimBLEScan::start` will no longer clear cache or results if scanning is already in progress. * `NimBLEScan::clearResults` will now reset the vector capacity to 0. * `NimBLEScan::stop` will no longer call the `onScanEnd` callback as the caller should know its been stopped when this is called. * `NimBLEScan::clearDuplicateCache` has been removed as it was problematic and only for the esp32. Stop and start the scanner for the same effect. * `NimBLEScan::start` takes a new bool parameter `restart`, default `true`, that will restart an already in progress scan and clear the duplicate filter so all devices will be discovered again. * Scan response data that is received without advertisement first will now create the device and send a callback. * Added new method: `NimBLEAdvertisedDevice::isScannable()` that returns true if the device is scannable. * Added default callbacks for `NimBLEScanCallbacks` * `NimBLEScanCallbacks` function signatures updated: * - `onDiscovered` now takes a `const NimBLEAdvertisedDevice*` * - `onResult` now takes a `const NimBLEAdvertisedDevice*` * - `onScanEnd` now takes a `const NimBLEScanResults&` and `int reason` * Added new erase overload: `NimBLEScan::erase(const NimBLEAdvertisedDevice* device)` * `NimBLEScanResults::getDevice` methods now return `const NimBLEAdvertisedDevice*` * `NimBLEScanResults` iterators are now `const_iterator`
1 parent 5f2730d commit 943df90

File tree

9 files changed

+252
-287
lines changed

9 files changed

+252
-287
lines changed

examples/Advanced/NimBLE_Client/main/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
extern "C" {void app_main(void);}
1313

14-
static NimBLEAdvertisedDevice* advDevice;
14+
static const NimBLEAdvertisedDevice* advDevice;
1515

1616
static bool doConnect = false;
1717
static uint32_t scanTime = 0; /** scan time in milliseconds, 0 = scan forever */
@@ -67,7 +67,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
6767

6868
/** Define a class to handle the callbacks when advertisments are received */
6969
class scanCallbacks: public NimBLEScanCallbacks {
70-
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
70+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
7171
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
7272
if(advertisedDevice->isAdvertisingService(NimBLEUUID("DEAD")))
7373
{
@@ -82,8 +82,8 @@ class scanCallbacks: public NimBLEScanCallbacks {
8282
}
8383

8484
/** Callback to process the results of the completed scan or restart it */
85-
void onScanEnd(NimBLEScanResults results) {
86-
printf("Scan Ended\n");
85+
void onScanEnd(const NimBLEScanResults& results, int reason) {
86+
printf("Scan Ended, reason: %d, device count: %d\n", reason, results.getCount());
8787
}
8888
};
8989

examples/NimBLE_Async_Client/main/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
2525
} clientCB;
2626

2727
class scanCallbacks : public NimBLEScanCallbacks {
28-
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
28+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
2929
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
3030
if (advertisedDevice->haveName() && advertisedDevice->getName() == "NimBLE-Server") {
3131
printf("Found Our Device\n");

examples/basic/BLE_client/main/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static bool doConnect = false;
2424
static bool connected = false;
2525
static bool doScan = false;
2626
static BLERemoteCharacteristic* pRemoteCharacteristic;
27-
static BLEAdvertisedDevice* myDevice;
27+
static const BLEAdvertisedDevice* myDevice;
2828

2929
static void notifyCallback(
3030
BLERemoteCharacteristic* pBLERemoteCharacteristic,
@@ -137,7 +137,7 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
137137

138138
/*** Only a reference to the advertised device is passed now
139139
void onResult(BLEAdvertisedDevice advertisedDevice) { **/
140-
void onResult(BLEAdvertisedDevice* advertisedDevice) {
140+
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
141141
printf("BLE Advertised Device found: %s\n", advertisedDevice->toString().c_str());
142142

143143
// We have found a device, let us now see if it contains the service we are looking for.

examples/basic/BLE_scan/main/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever
2121
BLEScan* pBLEScan;
2222

2323
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
24-
void onResult(BLEAdvertisedDevice* advertisedDevice) {
24+
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
2525
printf("Advertised Device: %s \n", advertisedDevice->toString().c_str());
2626
}
2727
};
@@ -35,7 +35,7 @@ void scanTask (void * parameter){
3535
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
3636
vTaskDelay(2000/portTICK_PERIOD_MS); // Delay a second between loops.
3737
}
38-
38+
3939
vTaskDelete(NULL);
4040
}
4141

src/NimBLEAdvertisedDevice.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,14 @@ bool NimBLEAdvertisedDevice::isConnectable() const {
757757
return (m_advType & BLE_HCI_ADV_CONN_MASK) || (m_advType & BLE_HCI_ADV_DIRECT_MASK);
758758
} // isConnectable
759759

760+
/**
761+
* @brief Check if this device is advertising as scannable.
762+
* @return True if the device is scannable.
763+
*/
764+
bool NimBLEAdvertisedDevice::isScannable() const {
765+
return isLegacyAdvertisement() && (m_advType == BLE_HCI_ADV_TYPE_ADV_IND || m_advType == BLE_HCI_ADV_TYPE_ADV_SCAN_IND);
766+
} // isScannable
767+
760768
/**
761769
* @brief Check if this advertisement is a legacy or extended type
762770
* @return True if legacy (Bluetooth 4.x), false if extended (bluetooth 5.x).

src/NimBLEAdvertisedDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class NimBLEAdvertisedDevice {
8282
bool haveType(uint16_t type) const;
8383
std::string toString() const;
8484
bool isConnectable() const;
85+
bool isScannable() const;
8586
bool isLegacyAdvertisement() const;
8687
# if CONFIG_BT_NIMBLE_EXT_ADV
8788
uint8_t getSetId() const;

src/NimBLEDevice.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,15 +723,7 @@ void NimBLEDevice::onReset(int reason) {
723723

724724
m_synced = false;
725725

726-
NIMBLE_LOGE(LOG_TAG, "Resetting state; reason=%d, %s", reason, NimBLEUtils::returnCodeToString(reason));
727-
728-
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
729-
if (m_initialized) {
730-
if (m_pScan != nullptr) {
731-
m_pScan->onHostReset();
732-
}
733-
}
734-
# endif
726+
NIMBLE_LOGE(LOG_TAG, "Host reset; reason=%d, %s", reason, NimBLEUtils::returnCodeToString(reason));
735727
} // onReset
736728

737729
/**

0 commit comments

Comments
 (0)