Skip to content

Commit 4401dc0

Browse files
committed
Code changes for #30
1 parent 5cec383 commit 4401dc0

File tree

7 files changed

+77
-52
lines changed

7 files changed

+77
-52
lines changed

cpp_utils/BLEAdvertisedDevice.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class BLEAdvertisedDeviceCallbacks {
9898
* As we are scanning, we will find new devices. When found, this call back is invoked with a reference to the
9999
* device that was found. During any individual scan, a device will only be detected one time.
100100
*/
101-
virtual void onResult(BLEAdvertisedDevice* pAdvertisedDevice) = 0;
101+
virtual void onResult(BLEAdvertisedDevice advertisedDevice) = 0;
102102
};
103103

104104
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLEAdvertisedDeviceCallbacks.cpp

-11
This file was deleted.

cpp_utils/BLEScan.cpp

+48-20
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,19 @@ BLEScan::BLEScan() {
3232
} // BLEScan
3333

3434

35-
BLEScan::~BLEScan() {
36-
clearAdvertisedDevices();
37-
} // ~BLEScan
38-
3935

4036
/**
4137
* @brief Clear the history of previously detected advertised devices.
4238
* @return N/A
4339
*/
40+
/*
4441
void BLEScan::clearAdvertisedDevices() {
4542
for (int i=0; i<m_vectorAvdertisedDevices.size(); i++) {
4643
delete m_vectorAvdertisedDevices[i];
4744
}
4845
m_vectorAvdertisedDevices.clear();
4946
} // clearAdvertisedDevices
47+
*/
5048

5149

5250
/**
@@ -92,31 +90,41 @@ void BLEScan::gapEventHandler(
9290
// ignore it.
9391
BLEAddress advertisedAddress(param->scan_rst.bda);
9492
bool found = false;
93+
/*
9594
for (int i=0; i<m_vectorAvdertisedDevices.size(); i++) {
9695
if (m_vectorAvdertisedDevices[i]->getAddress().equals(advertisedAddress)) {
9796
found = true;
9897
break;
9998
}
10099
}
100+
*/
101+
for (int i=0; i<m_scanResults.getCount(); i++) {
102+
if (m_scanResults.getDevice(i).getAddress().equals(advertisedAddress)) {
103+
found = true;
104+
break;
105+
}
106+
}
101107
if (found) {
102108
ESP_LOGD(LOG_TAG, "Ignoring %s, already seen it.", advertisedAddress.toString().c_str());
103109
break;
104110
}
105111

106112
// We now construct a model of the advertised device that we have just found for the first
107113
// time.
108-
BLEAdvertisedDevice* pAdvertisedDevice = new BLEAdvertisedDevice();
109-
pAdvertisedDevice->setAddress(advertisedAddress);
110-
pAdvertisedDevice->setRSSI(param->scan_rst.rssi);
111-
pAdvertisedDevice->setAdFlag(param->scan_rst.flag);
112-
pAdvertisedDevice->parseAdvertisement((uint8_t*)param->scan_rst.ble_adv);
113-
pAdvertisedDevice->setScan(this);
114-
115-
m_vectorAvdertisedDevices.push_back(pAdvertisedDevice);
114+
BLEAdvertisedDevice advertisedDevice;
115+
advertisedDevice.setAddress(advertisedAddress);
116+
advertisedDevice.setRSSI(param->scan_rst.rssi);
117+
advertisedDevice.setAdFlag(param->scan_rst.flag);
118+
advertisedDevice.parseAdvertisement((uint8_t*)param->scan_rst.ble_adv);
119+
advertisedDevice.setScan(this);
120+
121+
//m_vectorAvdertisedDevices.push_back(pAdvertisedDevice);
116122
if (m_pAdvertisedDeviceCallbacks) {
117-
m_pAdvertisedDeviceCallbacks->onResult(pAdvertisedDevice);
123+
m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice);
118124
}
119125

126+
m_scanResults.m_vectorAdvertisedDevices.push_back(advertisedDevice);
127+
120128
break;
121129
} // ESP_GAP_SEARCH_INQ_RES_EVT
122130

@@ -160,8 +168,8 @@ void BLEScan::setActiveScan(bool active) {
160168

161169

162170
/**
163-
* @brief Set the callbacks to be invoked.
164-
* @param [in] pAdvertisedDeviceCallbacks Callbacks to be invoked.
171+
* @brief Set the call backs to be invoked.
172+
* @param [in] pAdvertisedDeviceCallbacks Call backs to be invoked.
165173
*/
166174
void BLEScan::setAdvertisedDeviceCallbacks(BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks) {
167175
m_pAdvertisedDeviceCallbacks = pAdvertisedDeviceCallbacks;
@@ -191,27 +199,27 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
191199
* @param [in] duration The duration in seconds for which to scan.
192200
* @return N/A.
193201
*/
194-
std::vector<BLEAdvertisedDevice*> BLEScan::start(uint32_t duration) {
202+
BLEScanResults BLEScan::start(uint32_t duration) {
195203
ESP_LOGD(LOG_TAG, ">> start(%d)", duration);
196204

197205
m_semaphoreScanEnd.take("start");
198206

199-
clearAdvertisedDevices();
207+
m_scanResults.m_vectorAdvertisedDevices.empty();
200208

201209
esp_err_t errRc = ::esp_ble_gap_set_scan_params(&m_scan_params);
202210

203211
if (errRc != ESP_OK) {
204212
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc));
205213
m_semaphoreScanEnd.give();
206-
return m_vectorAvdertisedDevices;
214+
return m_scanResults;
207215
}
208216

209217
errRc = ::esp_ble_gap_start_scanning(duration);
210218

211219
if (errRc != ESP_OK) {
212220
ESP_LOGE(LOG_TAG, "esp_ble_gap_start_scanning: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc));
213221
m_semaphoreScanEnd.give();
214-
return m_vectorAvdertisedDevices;
222+
return m_scanResults;
215223
}
216224

217225
m_stopped = false;
@@ -220,7 +228,7 @@ std::vector<BLEAdvertisedDevice*> BLEScan::start(uint32_t duration) {
220228
m_semaphoreScanEnd.give();
221229

222230
ESP_LOGD(LOG_TAG, "<< start()");
223-
return m_vectorAvdertisedDevices;
231+
return m_scanResults;
224232
} // start
225233

226234

@@ -245,4 +253,24 @@ void BLEScan::stop() {
245253
ESP_LOGD(LOG_TAG, "<< stop()");
246254
} // stop
247255

256+
257+
/**
258+
* @brief Return the count of devices found in the last scan.
259+
* @return The number of devices found in the last scan.
260+
*/
261+
int BLEScanResults::getCount() {
262+
return m_vectorAdvertisedDevices.size();
263+
} // getCount
264+
265+
266+
/**
267+
* @brief Return the specified device at the given index.
268+
* The index should be between 0 and getCount()-1.
269+
* @param [in] i The index of the device.
270+
* @return The device at the specified index.
271+
*/
272+
BLEAdvertisedDevice BLEScanResults::getDevice(uint32_t i) {
273+
return m_vectorAdvertisedDevices.at(i);
274+
}
275+
248276
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLEScan.h

+18-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
class BLEAdvertisedDevice;
2020
class BLEAdvertisedDeviceCallbacks;
2121
class BLEClient;
22+
class BLEScan;
23+
24+
class BLEScanResults {
25+
public:
26+
int getCount();
27+
BLEAdvertisedDevice getDevice(uint32_t i);
28+
private:
29+
friend BLEScan;
30+
std::vector<BLEAdvertisedDevice> m_vectorAdvertisedDevices;
31+
};
2232

2333
/**
2434
* @brief Perform and manage %BLE scans.
@@ -28,30 +38,29 @@ class BLEClient;
2838
class BLEScan {
2939
public:
3040
BLEScan();
31-
virtual ~BLEScan();
3241

3342
//virtual void onResults();
34-
void setActiveScan(bool active);
35-
void setAdvertisedDeviceCallbacks(BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks);
36-
void setInterval(uint16_t intervalMSecs);
37-
void setWindow(uint16_t windowMSecs);
38-
std::vector<BLEAdvertisedDevice*> start(uint32_t duration);
39-
void stop();
43+
void setActiveScan(bool active);
44+
void setAdvertisedDeviceCallbacks(BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks);
45+
void setInterval(uint16_t intervalMSecs);
46+
void setWindow(uint16_t windowMSecs);
47+
BLEScanResults start(uint32_t duration);
48+
void stop();
4049

4150
private:
4251
friend class BLE;
4352
void gapEventHandler(
4453
esp_gap_ble_cb_event_t event,
4554
esp_ble_gap_cb_param_t* param);
46-
void clearAdvertisedDevices();
4755
void parseAdvertisement(BLEClient* pRemoteDevice, uint8_t *payload);
4856

4957

5058
esp_ble_scan_params_t m_scan_params;
5159
BLEAdvertisedDeviceCallbacks* m_pAdvertisedDeviceCallbacks;
5260
bool m_stopped;
5361
FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd");
54-
std::vector<BLEAdvertisedDevice*> m_vectorAvdertisedDevices;
62+
//std::vector<BLEAdvertisedDevice*> m_vectorAvdertisedDevices;
63+
BLEScanResults m_scanResults;
5564
}; // BLEScan
5665

5766
#endif /* CONFIG_BT_ENABLED */

cpp_utils/Makefile.arduino

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ BLE_FILES= \
1616
BLEAddress.h \
1717
BLEAdvertisedDevice.cpp \
1818
BLEAdvertisedDevice.h \
19-
BLEAdvertisedDeviceCallbacks.cpp \
2019
BLEAdvertising.cpp \
2120
BLEAdvertising.h \
2221
BLECharacteristic.cpp \

cpp_utils/tests/BLE Tests/SampleClient.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
8080
/**
8181
* Called for each advertising BLE server.
8282
*/
83-
void onResult(BLEAdvertisedDevice *pAdvertisedDevice) {
84-
ESP_LOGD(LOG_TAG, "Advertised Device: %s", pAdvertisedDevice->toString().c_str());
83+
void onResult(BLEAdvertisedDevice advertisedDevice) {
84+
ESP_LOGD(LOG_TAG, "Advertised Device: %s", advertisedDevice.toString().c_str());
8585

86-
if (pAdvertisedDevice->haveServiceUUID() && pAdvertisedDevice->getServiceUUID().equals(serviceUUID)) {
87-
pAdvertisedDevice->getScan()->stop();
86+
if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
87+
advertisedDevice.getScan()->stop();
8888

89-
ESP_LOGD(LOG_TAG, "Found our device! address: %s", pAdvertisedDevice->getAddress().toString().c_str());
89+
ESP_LOGD(LOG_TAG, "Found our device! address: %s", advertisedDevice.getAddress().toString().c_str());
9090
MyClient* pMyClient = new MyClient();
9191
pMyClient->setStackSize(18000);
92-
pMyClient->start(new BLEAddress(*pAdvertisedDevice->getAddress().getNative()));
92+
pMyClient->start(new BLEAddress(*advertisedDevice.getAddress().getNative()));
9393
} // Found our server
9494
} // onResult
9595
}; // MyAdvertisedDeviceCallbacks

cpp_utils/tests/BLE Tests/SampleScan.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
static const char LOG_TAG[] = "SampleScan";
1111

1212
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
13-
void onResult(BLEAdvertisedDevice *pAdvertisedDevice) {
14-
ESP_LOGD(LOG_TAG, "Advertised Device: %s", pAdvertisedDevice->toString().c_str());
13+
void onResult(BLEAdvertisedDevice advertisedDevice) {
14+
ESP_LOGD(LOG_TAG, "Advertised Device: %s", advertisedDevice.toString().c_str());
1515
}
1616
};
1717

@@ -21,8 +21,8 @@ static void run() {
2121
BLEScan* pBLEScan = BLE::getScan();
2222
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
2323
pBLEScan->setActiveScan(true);
24-
std::vector<BLEAdvertisedDevice*> foundDevices = pBLEScan->start(30);
25-
ESP_LOGD(LOG_TAG, "We found %d devices", foundDevices.size());
24+
BLEScanResults scanResults = pBLEScan->start(30);
25+
ESP_LOGD(LOG_TAG, "We found %d devices", scanResults.getCount());
2626
ESP_LOGD(LOG_TAG, "Scanning sample ended");
2727
}
2828

0 commit comments

Comments
 (0)