Skip to content

Commit 34902f4

Browse files
committed
Sync update for 0.4.3 take 2
1 parent c8e11bc commit 34902f4

39 files changed

+1986
-618
lines changed

examples/BLE_client/BLE_client.ino

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static void notifyCallback(
2626
Serial.println(length);
2727
}
2828

29-
void connectToServer(BLEAddress pAddress) {
29+
bool connectToServer(BLEAddress pAddress) {
3030
Serial.print("Forming a connection to ");
3131
Serial.println(pAddress.toString().c_str());
3232

@@ -42,17 +42,19 @@ void connectToServer(BLEAddress pAddress) {
4242
if (pRemoteService == nullptr) {
4343
Serial.print("Failed to find our service UUID: ");
4444
Serial.println(serviceUUID.toString().c_str());
45-
return;
45+
return false;
4646
}
47+
Serial.println(" - Found our service");
4748

4849

4950
// Obtain a reference to the characteristic in the service of the remote BLE server.
5051
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
5152
if (pRemoteCharacteristic == nullptr) {
5253
Serial.print("Failed to find our characteristic UUID: ");
5354
Serial.println(charUUID.toString().c_str());
54-
return;
55+
return false;
5556
}
57+
Serial.println(" - Found our characteristic");
5658

5759
// Read the value of the characteristic.
5860
std::string value = pRemoteCharacteristic->readValue();
@@ -109,9 +111,13 @@ void loop() {
109111
// BLE Server with which we wish to connect. Now we connect to it. Once we are
110112
// connected we set the connected flag to be true.
111113
if (doConnect == true) {
112-
connectToServer(*pServerAddress);
114+
if (connectToServer(*pServerAddress)) {
115+
Serial.println("We are now connected to the BLE Server.");
116+
connected = true;
117+
} else {
118+
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
119+
}
113120
doConnect = false;
114-
connected = true;
115121
}
116122

117123
// If we are connected to a peer BLE Server, update the characteristic each time we are reached

examples/BLE_notify/BLE_notify.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Video: https://www.youtube.com/watch?v=oCMOYS71NIU
3-
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleNotify.cpp
3+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
44
Ported to Arduino ESP32 by Evandro Copercini
55
66
Create a BLE server that, once we receive a connection, will send periodic notifications.

examples/BLE_scan/BLE_scan.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleScan.cpp
2+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
33
Ported to Arduino ESP32 by Evandro Copercini
44
*/
55

examples/BLE_server/BLE_server.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleServer.cpp
2+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
33
Ported to Arduino ESP32 by Evandro Copercini
44
*/
55

examples/BLE_uart/BLE_uart.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Video: https://www.youtube.com/watch?v=oCMOYS71NIU
3-
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleNotify.cpp
3+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
44
Ported to Arduino ESP32 by Evandro Copercini
55
66
Create a BLE server that, once we receive a connection, will send periodic notifications.

examples/BLE_write/BLE_write.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/SampleWrite.cpp
2+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
33
Ported to Arduino ESP32 by Evandro Copercini
44
*/
55

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=ESP32 BLE Arduino
2-
version=0.4.2
2+
version=0.4.3
33
author=Neil Kolban <kolban1@kolban.com>
44
maintainer=Neil Kolban <kolban1@kolban.com>
55
sentence=BLE functions for ESP32
66
paragraph=This library provides an implementation Bluetooth Low Energy support for the ESP32 using the Arduino platform.
77
category=Communication
88
url=https://github.com/nkolban/ESP32_BLE_Arduino
99
architectures=esp32
10-
includes=BLEDevice.h, BLEUtils.h, BLEScan.h, BLEAdvertisedDevice.h
10+
includes=BLE.h, BLEUtils.h, BLEScan.h, BLEAdvertisedDevice.h

src/BLEAdvertisedDevice.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ BLEAddress BLEAdvertisedDevice::getAddress() {
6060
*
6161
* @return The appearance of the advertised device.
6262
*/
63-
uint16_t BLEAdvertisedDevice::getApperance() {
63+
uint16_t BLEAdvertisedDevice::getAppearance() {
6464
return m_appearance;
6565
}
6666

@@ -265,7 +265,7 @@ void BLEAdvertisedDevice::parseAdvertisement(uint8_t* payload) {
265265
} // ESP_BLE_AD_MANUFACTURER_SPECIFIC_TYPE
266266

267267
default: {
268-
ESP_LOGD(LOG_TAG, "Unhandled type");
268+
ESP_LOGD(LOG_TAG, "Unhandled type: adType: %d - 0x%.2x", ad_type, ad_type);
269269
break;
270270
}
271271
} // switch
@@ -329,7 +329,7 @@ void BLEAdvertisedDevice::setManufacturerData(std::string manufacturerData) {
329329
void BLEAdvertisedDevice::setName(std::string name) {
330330
m_name = name;
331331
m_haveName = true;
332-
ESP_LOGD(LOG_TAG, "- name: %s", m_name.c_str());
332+
ESP_LOGD(LOG_TAG, "- setName(): name: %s", m_name.c_str());
333333
} // setName
334334

335335

@@ -340,7 +340,7 @@ void BLEAdvertisedDevice::setName(std::string name) {
340340
void BLEAdvertisedDevice::setRSSI(int rssi) {
341341
m_rssi = rssi;
342342
m_haveRSSI = true;
343-
ESP_LOGD(LOG_TAG, "- rssi: %d", m_rssi);
343+
ESP_LOGD(LOG_TAG, "- setRSSI(): rssi: %d", m_rssi);
344344
} // setRSSI
345345

346346

@@ -367,7 +367,7 @@ void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
367367
void BLEAdvertisedDevice::setServiceUUID(BLEUUID serviceUUID) {
368368
m_serviceUUID = serviceUUID;
369369
m_haveServiceUUID = true;
370-
ESP_LOGD(LOG_TAG, "- serviceUUID: %s", serviceUUID.toString().c_str());
370+
ESP_LOGD(LOG_TAG, "- setServiceUUID(): serviceUUID: %s", serviceUUID.toString().c_str());
371371
} // setRSSI
372372

373373

@@ -390,7 +390,7 @@ std::string BLEAdvertisedDevice::toString() {
390390
std::stringstream ss;
391391
ss << "Name: " << getName() << ", Address: " << getAddress().toString();
392392
if (haveAppearance()) {
393-
ss << ", appearance: " << getApperance();
393+
ss << ", appearance: " << getAppearance();
394394
}
395395
if (haveManufacturerData()) {
396396
char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());

src/BLEAdvertisedDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BLEAdvertisedDevice {
3030
BLEAdvertisedDevice();
3131

3232
BLEAddress getAddress();
33-
uint16_t getApperance();
33+
uint16_t getAppearance();
3434
std::string getManufacturerData();
3535
std::string getName();
3636
int getRSSI();

src/BLEAdvertising.cpp

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ BLEAdvertising::BLEAdvertising() {
4444
} // BLEAdvertising
4545

4646

47+
/**
48+
* @brief Add a service uuid to exposed list of services.
49+
* @param [in] serviceUUID The UUID of the service to expose.
50+
*/
51+
void BLEAdvertising::addServiceUUID(BLEUUID serviceUUID) {
52+
m_serviceUUIDs.push_back(serviceUUID);
53+
} // addServiceUUID
54+
55+
56+
/**
57+
* @brief Add a service uuid to exposed list of services.
58+
* @param [in] serviceUUID The string representation of the service to expose.
59+
*/
60+
void BLEAdvertising::addServiceUUID(const char* serviceUUID) {
61+
addServiceUUID(BLEUUID(serviceUUID));
62+
} // addServiceUUID
63+
64+
4765
/**
4866
* @brief Set the device appearance in the advertising data.
4967
* The appearance attribute is of type 0x19. The codes for distinct appearances can be found here:
@@ -56,52 +74,6 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
5674
} // setAppearance
5775

5876

59-
/**
60-
* @brief Set the service UUID.
61-
* We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the
62-
* ESP-IDF advertising functions. In this method, we see two fields within that structure
63-
* namely service_uuid_len and p_service_uuid to be the information supplied in the passed
64-
* in service uuid.
65-
* @param [in] uuid The UUID of the service.
66-
* @return N/A.
67-
*/
68-
void BLEAdvertising::setServiceUUID(const char* serviceUUID) {
69-
return setServiceUUID(BLEUUID(serviceUUID));
70-
}
71-
/**
72-
* @brief Set the service UUID.
73-
* We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the
74-
* ESP-IDF advertising functions. In this method, we see two fields within that structure
75-
* namely service_uuid_len and p_service_uuid to be the information supplied in the passed
76-
* in service uuid.
77-
* @param [in] uuid The UUID of the service.
78-
* @return N/A.
79-
*/
80-
void BLEAdvertising::setServiceUUID(BLEUUID serviceUUID) {
81-
ESP_LOGD(LOG_TAG, ">> setServiceUUID - %s", serviceUUID.toString().c_str());
82-
m_serviceUUID = serviceUUID; // Save the new service UUID
83-
esp_bt_uuid_t* espUUID = m_serviceUUID.getNative();
84-
switch(espUUID->len) {
85-
case ESP_UUID_LEN_16: {
86-
m_advData.service_uuid_len = 2;
87-
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID->uuid.uuid16);
88-
break;
89-
}
90-
case ESP_UUID_LEN_32: {
91-
m_advData.service_uuid_len = 4;
92-
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID->uuid.uuid32);
93-
break;
94-
}
95-
case ESP_UUID_LEN_128: {
96-
m_advData.service_uuid_len = 16;
97-
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID->uuid.uuid128);
98-
break;
99-
}
100-
} // switch
101-
ESP_LOGD(LOG_TAG, "<< setServiceUUID");
102-
} // setServiceUUID
103-
104-
10577
/**
10678
* @brief Start advertising.
10779
* Start advertising.
@@ -110,24 +82,49 @@ void BLEAdvertising::setServiceUUID(BLEUUID serviceUUID) {
11082
void BLEAdvertising::start() {
11183
ESP_LOGD(LOG_TAG, ">> start");
11284

113-
if (m_advData.service_uuid_len > 0) {
114-
uint8_t hexData[16*2+1];
115-
BLEUtils::buildHexData(hexData, m_advData.p_service_uuid, m_advData.service_uuid_len);
116-
ESP_LOGD(LOG_TAG, " - Service: service_uuid_len=%d, p_service_uuid=0x%x (data=%s)",
117-
m_advData.service_uuid_len,
118-
(uint32_t)m_advData.p_service_uuid,
119-
(m_advData.service_uuid_len > 0?(char *)hexData:"N/A")
120-
);
121-
} // We have a service to advertise
85+
// We have a vector of service UUIDs that we wish to advertise. In order to use the
86+
// ESP-IDF framework, these must be supplied in a contiguous array of their 128bit (16 byte)
87+
// representations. If we have 1 or more services to advertise then we allocate enough
88+
// storage to host them and then copy them in one at a time into the contiguous storage.
89+
int numServices = m_serviceUUIDs.size();
90+
if (numServices > 0) {
91+
m_advData.service_uuid_len = 16*numServices;
92+
m_advData.p_service_uuid = new uint8_t[m_advData.service_uuid_len];
93+
uint8_t* p = m_advData.p_service_uuid;
94+
for (int i=0; i<numServices; i++) {
95+
ESP_LOGD(LOG_TAG, "- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
96+
BLEUUID serviceUUID128 = m_serviceUUIDs[i].to128();
97+
memcpy(p, serviceUUID128.getNative()->uuid.uuid128, 16);
98+
p+=16;
99+
}
100+
} else {
101+
m_advData.service_uuid_len = 0;
102+
ESP_LOGD(LOG_TAG, "- no services advertised");
103+
}
122104

123105

124106
// Set the configuration for advertising.
107+
m_advData.set_scan_rsp = false;
125108
esp_err_t errRc = ::esp_ble_gap_config_adv_data(&m_advData);
126109
if (errRc != ESP_OK) {
127110
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
128111
return;
129112
}
130113

114+
m_advData.set_scan_rsp = true;
115+
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
116+
if (errRc != ESP_OK) {
117+
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
118+
return;
119+
}
120+
121+
// If we had services to advertise then we previously allocated some storage for them.
122+
// Here we release that storage.
123+
if (m_advData.service_uuid_len > 0) {
124+
delete[] m_advData.p_service_uuid;
125+
m_advData.p_service_uuid = nullptr;
126+
}
127+
131128
// Start advertising.
132129
errRc = ::esp_ble_gap_start_advertising(&m_advParams);
133130
if (errRc != ESP_OK) {
@@ -152,4 +149,6 @@ void BLEAdvertising::stop() {
152149
}
153150
ESP_LOGD(LOG_TAG, "<< stop");
154151
} // stop
152+
153+
155154
#endif /* CONFIG_BT_ENABLED */

0 commit comments

Comments
 (0)