Skip to content

Commit b5d960b

Browse files
committed
Commit for 0.4.10
1 parent 6bad7b4 commit b5d960b

13 files changed

+204
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ESP32 BLE for Arduino
2-
The Arduino IDE provides an excellent library package managed where versions of libraries can be downloaded and installed. This Github project provides the repository for the ESP32 BLE support for Arduino.
2+
The Arduino IDE provides an excellent library package manager where versions of libraries can be downloaded and installed. This Github project provides the repository for the ESP32 BLE support for Arduino.
33

44
The actual source of the project which is being maintained can be found here:
55

examples/BLE_iBeacon/BLE_iBeacon.ino

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
3+
Ported to Arduino ESP32 by pcbreflux
4+
*/
5+
6+
7+
/*
8+
Create a BLE server that will send periodic iBeacon frames.
9+
The design of creating the BLE server is:
10+
1. Create a BLE Server
11+
2. Create advertising data
12+
3. Start advertising.
13+
4. wait
14+
5. Stop advertising.
15+
6. deep sleep
16+
17+
*/
18+
#include "sys/time.h"
19+
20+
#include "BLEDevice.h"
21+
#include "BLEServer.h"
22+
#include "BLEUtils.h"
23+
#include "BLEBeacon.h"
24+
#include "esp_sleep.h"
25+
26+
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up
27+
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
28+
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
uint8_t temprature_sens_read();
35+
//uint8_t g_phyFuns;
36+
37+
#ifdef __cplusplus
38+
}
39+
#endif
40+
41+
// See the following for generating UUIDs:
42+
// https://www.uuidgenerator.net/
43+
BLEAdvertising *pAdvertising;
44+
struct timeval now;
45+
46+
#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d" // UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/)
47+
48+
void setBeacon() {
49+
50+
BLEBeacon oBeacon = BLEBeacon();
51+
oBeacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!)
52+
oBeacon.setProximityUUID(BLEUUID(BEACON_UUID));
53+
oBeacon.setMajor((bootcount & 0xFFFF0000) >> 16);
54+
oBeacon.setMinor(bootcount&0xFFFF);
55+
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
56+
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
57+
58+
oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04
59+
60+
std::string strServiceData = "";
61+
62+
strServiceData += (char)26; // Len
63+
strServiceData += (char)0xFF; // Type
64+
strServiceData += oBeacon.getData();
65+
oAdvertisementData.addData(strServiceData);
66+
67+
pAdvertising->setAdvertisementData(oAdvertisementData);
68+
pAdvertising->setScanResponseData(oScanResponseData);
69+
70+
}
71+
72+
void setup() {
73+
74+
75+
Serial.begin(115200);
76+
gettimeofday(&now, NULL);
77+
78+
Serial.printf("start ESP32 %d\n",bootcount++);
79+
80+
Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n",now.tv_sec,now.tv_sec-last);
81+
82+
last = now.tv_sec;
83+
84+
// Create the BLE Device
85+
BLEDevice::init("");
86+
87+
// Create the BLE Server
88+
BLEServer *pServer = BLEDevice::createServer();
89+
90+
pAdvertising = pServer->getAdvertising();
91+
92+
setBeacon();
93+
// Start advertising
94+
pAdvertising->start();
95+
Serial.println("Advertizing started...");
96+
delay(100);
97+
pAdvertising->stop();
98+
Serial.printf("enter deep sleep\n");
99+
esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);
100+
Serial.printf("in deep sleep\n");
101+
}
102+
103+
void loop() {
104+
}

examples/BLE_notify/BLE_notify.ino

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
#include <BLEUtils.h>
2424
#include <BLE2902.h>
2525

26-
BLECharacteristic *pCharacteristic;
26+
BLEServer *pServer = NULL;
2727
bool deviceConnected = false;
28+
bool oldDeviceConnected = false;
2829
uint8_t value = 0;
2930

3031
// See the following for generating UUIDs:
@@ -53,14 +54,14 @@ void setup() {
5354
BLEDevice::init("MyESP32");
5455

5556
// Create the BLE Server
56-
BLEServer *pServer = BLEDevice::createServer();
57+
pServer = BLEDevice::createServer();
5758
pServer->setCallbacks(new MyServerCallbacks());
5859

5960
// Create the BLE Service
6061
BLEService *pService = pServer->createService(SERVICE_UUID);
6162

6263
// Create a BLE Characteristic
63-
pCharacteristic = pService->createCharacteristic(
64+
BLECharacteristic * pCharacteristic = pService->createCharacteristic(
6465
CHARACTERISTIC_UUID,
6566
BLECharacteristic::PROPERTY_READ |
6667
BLECharacteristic::PROPERTY_WRITE |
@@ -81,13 +82,23 @@ void setup() {
8182
}
8283

8384
void loop() {
84-
85-
if (deviceConnected) {
86-
Serial.printf("*** NOTIFY: %d ***\n", value);
87-
pCharacteristic->setValue(&value, 1);
88-
pCharacteristic->notify();
89-
//pCharacteristic->indicate();
90-
value++;
91-
}
92-
delay(2000);
85+
// notify changed value
86+
if (deviceConnected) {
87+
pCharacteristic->setValue(&value, 1);
88+
pCharacteristic->notify();
89+
value++;
90+
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
91+
}
92+
// disconnecting
93+
if (!deviceConnected && oldDeviceConnected) {
94+
delay(500); // give the bluetooth stack the chance to get things ready
95+
pServer->startAdvertising(); // restart advertising
96+
Serial.println("start advertising");
97+
oldDeviceConnected = deviceConnected;
98+
}
99+
// connecting
100+
if (deviceConnected && !oldDeviceConnected) {
101+
// do stuff here on connecting
102+
oldDeviceConnected = deviceConnected;
103+
}
93104
}

examples/BLE_uart/BLE_uart.ino

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
#include <BLEUtils.h>
2525
#include <BLE2902.h>
2626

27-
BLECharacteristic *pCharacteristic;
27+
BLEServer *pServer = NULL;
28+
BLECharacteristic * pTxCharacteristic;
2829
bool deviceConnected = false;
30+
bool oldDeviceConnected = false;
2931
uint8_t txValue = 0;
3032

3133
// See the following for generating UUIDs:
@@ -70,26 +72,26 @@ void setup() {
7072
BLEDevice::init("UART Service");
7173

7274
// Create the BLE Server
73-
BLEServer *pServer = BLEDevice::createServer();
75+
pServer = BLEDevice::createServer();
7476
pServer->setCallbacks(new MyServerCallbacks());
7577

7678
// Create the BLE Service
7779
BLEService *pService = pServer->createService(SERVICE_UUID);
7880

7981
// Create a BLE Characteristic
80-
pCharacteristic = pService->createCharacteristic(
81-
CHARACTERISTIC_UUID_TX,
82-
BLECharacteristic::PROPERTY_NOTIFY
83-
);
82+
pTxCharacteristic = pService->createCharacteristic(
83+
CHARACTERISTIC_UUID_TX,
84+
BLECharacteristic::PROPERTY_NOTIFY
85+
);
8486

85-
pCharacteristic->addDescriptor(new BLE2902());
87+
pTxCharacteristic->addDescriptor(new BLE2902());
8688

87-
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
88-
CHARACTERISTIC_UUID_RX,
89-
BLECharacteristic::PROPERTY_WRITE
90-
);
89+
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
90+
CHARACTERISTIC_UUID_RX,
91+
BLECharacteristic::PROPERTY_WRITE
92+
);
9193

92-
pCharacteristic->setCallbacks(new MyCallbacks());
94+
pRxCharacteristic->setCallbacks(new MyCallbacks());
9395

9496
// Start the service
9597
pService->start();
@@ -101,11 +103,23 @@ void setup() {
101103

102104
void loop() {
103105

104-
if (deviceConnected) {
105-
Serial.printf("*** Sent Value: %d ***\n", txValue);
106-
pCharacteristic->setValue(&txValue, 1);
107-
pCharacteristic->notify();
108-
txValue++;
109-
}
110-
delay(1000);
106+
if (deviceConnected) {
107+
pTxCharacteristic->setValue(&txValue, 1);
108+
pTxCharacteristic->notify();
109+
txValue++;
110+
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
111+
}
112+
113+
// disconnecting
114+
if (!deviceConnected && oldDeviceConnected) {
115+
delay(500); // give the bluetooth stack the chance to get things ready
116+
pServer->startAdvertising(); // restart advertising
117+
Serial.println("start advertising");
118+
oldDeviceConnected = deviceConnected;
119+
}
120+
// connecting
121+
if (deviceConnected && !oldDeviceConnected) {
122+
// do stuff here on connecting
123+
oldDeviceConnected = deviceConnected;
124+
}
111125
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP32 BLE Arduino
2-
version=0.4.9
2+
version=0.4.10
33
author=Neil Kolban <[email protected]>
44
maintainer=Neil Kolban <[email protected]>
55
sentence=BLE functions for ESP32

src/BLEAdvertising.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
9191
m_advData.appearance = appearance;
9292
} // setAppearance
9393

94+
void BLEAdvertising::setMinInterval(uint16_t mininterval) {
95+
m_advData.min_interval = mininterval;
96+
m_advParams.adv_int_min = mininterval;
97+
} // setMinInterval
98+
99+
void BLEAdvertising::setMaxInterval(uint16_t maxinterval) {
100+
m_advData.max_interval = maxinterval;
101+
m_advParams.adv_int_max = maxinterval;
102+
} // setMaxInterval
103+
94104

95105
/**
96106
* @brief Set the filtering for the scan filter.

src/BLEAdvertising.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class BLEAdvertising {
5252
void start();
5353
void stop();
5454
void setAppearance(uint16_t appearance);
55+
void setMaxInterval(uint16_t maxinterval);
56+
void setMinInterval(uint16_t mininterval);
5557
void setAdvertisementData(BLEAdvertisementData& advertisementData);
5658
void setScanFilter(bool scanRequertWhitelistOnly, bool connectWhitelistOnly);
5759
void setScanResponseData(BLEAdvertisementData& advertisementData);

src/BLEClient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ bool BLEClient::connect(BLEAddress address) {
110110
errRc = ::esp_ble_gattc_open(
111111
getGattcIf(),
112112
*getPeerAddress().getNative(), // address
113+
BLE_ADDR_TYPE_PUBLIC, // Note: This was added on 2018-04-03 when the latest ESP-IDF was detected to have changed the signature.
113114
1 // direct connection
114115
);
115116
if (errRc != ESP_OK) {

src/BLEDescriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void BLEDescriptor::handleGATTServerEvent(
155155
(uint32_t)m_pCharacteristic->getService()->getLastCreatedCharacteristic());
156156
*/
157157
if (m_pCharacteristic != nullptr &&
158-
m_bleUUID.equals(BLEUUID(param->add_char_descr.char_uuid)) &&
158+
m_bleUUID.equals(BLEUUID(param->add_char_descr.descr_uuid)) &&
159159
m_pCharacteristic->getService()->getHandle() == param->add_char_descr.service_handle &&
160160
m_pCharacteristic == m_pCharacteristic->getService()->getLastCreatedCharacteristic()) {
161161
setHandle(param->add_char_descr.attr_handle);

0 commit comments

Comments
 (0)