Skip to content

Commit 934702b

Browse files
committed
Changes, bugfixes and updgrades. Library is ready to work with arduino-esp32 v1.0.1.
1 parent 7951347 commit 934702b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2204
-1478
lines changed

examples/BLE_client/BLE_client.ino

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
/**
22
* A BLE client example that is rich in capabilities.
3+
* There is a lot new capabilities implemented.
4+
* author unknown
5+
* updated by chegewara
36
*/
47

58
#include "BLEDevice.h"
69
//#include "BLEScan.h"
710

811
// The remote service we wish to connect to.
9-
static BLEUUID serviceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");
12+
static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
1013
// The characteristic of the remote service we are interested in.
11-
static BLEUUID charUUID("0d563a58-196a-48ce-ace2-dfec78acc814");
14+
static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");
1215

13-
static BLEAddress *pServerAddress;
1416
static boolean doConnect = false;
1517
static boolean connected = false;
18+
static doScan = false;
1619
static BLERemoteCharacteristic* pRemoteCharacteristic;
20+
static BLEAdvertisedDevice* myDevice;
1721

1822
static void notifyCallback(
1923
BLERemoteCharacteristic* pBLERemoteCharacteristic,
@@ -24,24 +28,39 @@ static void notifyCallback(
2428
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
2529
Serial.print(" of data length ");
2630
Serial.println(length);
31+
Serial.print("data: ");
32+
Serial.println((char*)pData);
2733
}
2834

29-
bool connectToServer(BLEAddress pAddress) {
35+
class MyClientCallback : public BLEClientCallbacks {
36+
void onConnect(BLEClient* pclient) {
37+
}
38+
39+
void onDisconnect(BLEClient* pclient) {
40+
connected = false;
41+
Serial.println("onDisconnect");
42+
}
43+
};
44+
45+
bool connectToServer() {
3046
Serial.print("Forming a connection to ");
31-
Serial.println(pAddress.toString().c_str());
47+
Serial.println(myDevice->getAddress().toString().c_str());
3248

3349
BLEClient* pClient = BLEDevice::createClient();
3450
Serial.println(" - Created client");
3551

52+
pClient->setClientCallbacks(new MyClientCallback());
53+
3654
// Connect to the remove BLE Server.
37-
pClient->connect(pAddress);
55+
pClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
3856
Serial.println(" - Connected to server");
3957

4058
// Obtain a reference to the service we are after in the remote BLE server.
4159
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
4260
if (pRemoteService == nullptr) {
4361
Serial.print("Failed to find our service UUID: ");
4462
Serial.println(serviceUUID.toString().c_str());
63+
pClient->disconnect();
4564
return false;
4665
}
4766
Serial.println(" - Found our service");
@@ -52,16 +71,22 @@ bool connectToServer(BLEAddress pAddress) {
5271
if (pRemoteCharacteristic == nullptr) {
5372
Serial.print("Failed to find our characteristic UUID: ");
5473
Serial.println(charUUID.toString().c_str());
74+
pClient->disconnect();
5575
return false;
5676
}
5777
Serial.println(" - Found our characteristic");
5878

5979
// Read the value of the characteristic.
60-
std::string value = pRemoteCharacteristic->readValue();
61-
Serial.print("The characteristic value was: ");
62-
Serial.println(value.c_str());
80+
if(pRemoteCharacteristic->canRead()) {
81+
std::string value = pRemoteCharacteristic->readValue();
82+
Serial.print("The characteristic value was: ");
83+
Serial.println(value.c_str());
84+
}
6385

64-
pRemoteCharacteristic->registerForNotify(notifyCallback);
86+
if(pRemoteCharacteristic->canNotify())
87+
pRemoteCharacteristic->registerForNotify(notifyCallback);
88+
89+
connected = true;
6590
}
6691
/**
6792
* Scan for BLE servers and find the first one that advertises the service we are looking for.
@@ -75,14 +100,12 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
75100
Serial.println(advertisedDevice.toString().c_str());
76101

77102
// We have found a device, let us now see if it contains the service we are looking for.
78-
if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
79-
80-
//
81-
Serial.print("Found our device! address: ");
82-
advertisedDevice.getScan()->stop();
103+
if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {
83104

84-
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
105+
BLEDevice::getScan()->stop();
106+
myDevice = new BLEAdvertisedDevice(advertisedDevice);
85107
doConnect = true;
108+
doScan = true;
86109

87110
} // Found our server
88111
} // onResult
@@ -96,11 +119,13 @@ void setup() {
96119

97120
// Retrieve a Scanner and set the callback we want to use to be informed when we
98121
// have detected a new device. Specify that we want active scanning and start the
99-
// scan to run for 30 seconds.
122+
// scan to run for 5 seconds.
100123
BLEScan* pBLEScan = BLEDevice::getScan();
101124
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
125+
pBLEScan->setInterval(1349);
126+
pBLEScan->setWindow(449);
102127
pBLEScan->setActiveScan(true);
103-
pBLEScan->start(30);
128+
pBLEScan->start(5, false);
104129
} // End of setup.
105130

106131

@@ -111,9 +136,8 @@ void loop() {
111136
// BLE Server with which we wish to connect. Now we connect to it. Once we are
112137
// connected we set the connected flag to be true.
113138
if (doConnect == true) {
114-
if (connectToServer(*pServerAddress)) {
139+
if (connectToServer()) {
115140
Serial.println("We are now connected to the BLE Server.");
116-
connected = true;
117141
} else {
118142
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
119143
}
@@ -128,7 +152,9 @@ void loop() {
128152

129153
// Set the characteristic's value to be the array of bytes that is actually a string.
130154
pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
155+
}else if(doScan){
156+
BLEDevice::getScan()->start(0); // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
131157
}
132158

133159
delay(1000); // Delay a second between loops.
134-
} // End of loop
160+
} // End of loop

examples/BLE_iBeacon/BLE_iBeacon.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "sys/time.h"
1919

2020
#include "BLEDevice.h"
21-
#include "BLEServer.h"
2221
#include "BLEUtils.h"
2322
#include "BLEBeacon.h"
2423
#include "esp_sleep.h"
@@ -85,9 +84,9 @@ void setup() {
8584
BLEDevice::init("");
8685

8786
// Create the BLE Server
88-
BLEServer *pServer = BLEDevice::createServer();
87+
// BLEServer *pServer = BLEDevice::createServer(); // <-- no longer required to instantiate BLEServer, less flash and ram usage
8988

90-
pAdvertising = pServer->getAdvertising();
89+
pAdvertising = BLEDevice::getAdvertising();
9190

9291
setBeacon();
9392
// Start advertising

examples/BLE_notify/BLE_notify.ino

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Video: https://www.youtube.com/watch?v=oCMOYS71NIU
33
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
5+
updated by chegewara
56
67
Create a BLE server that, once we receive a connection, will send periodic notifications.
78
The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b
@@ -27,7 +28,7 @@ BLEServer* pServer = NULL;
2728
BLECharacteristic* pCharacteristic = NULL;
2829
bool deviceConnected = false;
2930
bool oldDeviceConnected = false;
30-
uint8_t value = 0;
31+
uint32_t value = 0;
3132

3233
// See the following for generating UUIDs:
3334
// https://www.uuidgenerator.net/
@@ -52,7 +53,7 @@ void setup() {
5253
Serial.begin(115200);
5354

5455
// Create the BLE Device
55-
BLEDevice::init("MyESP32");
56+
BLEDevice::init("ESP32");
5657

5758
// Create the BLE Server
5859
pServer = BLEDevice::createServer();
@@ -78,17 +79,21 @@ void setup() {
7879
pService->start();
7980

8081
// Start advertising
81-
pServer->getAdvertising()->start();
82+
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
83+
pAdvertising->addServiceUUID(SERVICE_UUID);
84+
pAdvertising->setScanResponse(false);
85+
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
86+
BLEDevice::startAdvertising();
8287
Serial.println("Waiting a client connection to notify...");
8388
}
8489

8590
void loop() {
8691
// notify changed value
8792
if (deviceConnected) {
88-
pCharacteristic->setValue(&value, 1);
93+
pCharacteristic->setValue((uint8_t*)&value, 4);
8994
pCharacteristic->notify();
9095
value++;
91-
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
96+
delay(3); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
9297
}
9398
// disconnecting
9499
if (!deviceConnected && oldDeviceConnected) {

examples/BLE_scan/BLE_scan.ino

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <BLEScan.h>
99
#include <BLEAdvertisedDevice.h>
1010

11-
int scanTime = 30; //In seconds
11+
int scanTime = 5; //In seconds
12+
BLEScan* pBLEScan;
1213

1314
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
1415
void onResult(BLEAdvertisedDevice advertisedDevice) {
@@ -21,16 +22,19 @@ void setup() {
2122
Serial.println("Scanning...");
2223

2324
BLEDevice::init("");
24-
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
25+
pBLEScan = BLEDevice::getScan(); //create new scan
2526
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
2627
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
27-
BLEScanResults foundDevices = pBLEScan->start(scanTime);
28-
Serial.print("Devices found: ");
29-
Serial.println(foundDevices.getCount());
30-
Serial.println("Scan done!");
28+
pBLEScan->setInterval(100);
29+
pBLEScan->setWindow(99); // less or equal setInterval value
3130
}
3231

3332
void loop() {
3433
// put your main code here, to run repeatedly:
34+
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
35+
Serial.print("Devices found: ");
36+
Serial.println(foundDevices.getCount());
37+
Serial.println("Scan done!");
38+
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
3539
delay(2000);
3640
}

examples/BLE_server/BLE_server.ino

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
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
4+
updates by chegewara
45
*/
56

67
#include <BLEDevice.h>
@@ -17,7 +18,7 @@ void setup() {
1718
Serial.begin(115200);
1819
Serial.println("Starting BLE work!");
1920

20-
BLEDevice::init("MyESP32");
21+
BLEDevice::init("Long name works now");
2122
BLEServer *pServer = BLEDevice::createServer();
2223
BLEService *pService = pServer->createService(SERVICE_UUID);
2324
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
@@ -28,8 +29,13 @@ void setup() {
2829

2930
pCharacteristic->setValue("Hello World says Neil");
3031
pService->start();
31-
BLEAdvertising *pAdvertising = pServer->getAdvertising();
32-
pAdvertising->start();
32+
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
33+
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
34+
pAdvertising->addServiceUUID(SERVICE_UUID);
35+
pAdvertising->setScanResponse(true);
36+
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
37+
pAdvertising->setMinPreferred(0x12);
38+
BLEDevice::startAdvertising();
3339
Serial.println("Characteristic defined! Now you can read it in your phone!");
3440
}
3541

0 commit comments

Comments
 (0)