summaryrefslogtreecommitdiff
path: root/examples/BLE_notify
diff options
context:
space:
mode:
authorNeil Kolban <kolban1@kolban.com>2018-04-03 19:34:25 -0500
committerNeil Kolban <kolban1@kolban.com>2018-04-03 19:34:25 -0500
commitb5d960bacc2c92770a42b4b4565f0e82c797bbc5 (patch)
tree4dfa590a744a7f282698cae709173391ebbe9011 /examples/BLE_notify
parentSync for 0.4.9 (diff)
downloadthermostat-b5d960bacc2c92770a42b4b4565f0e82c797bbc5.tar.gz
thermostat-b5d960bacc2c92770a42b4b4565f0e82c797bbc5.tar.bz2
thermostat-b5d960bacc2c92770a42b4b4565f0e82c797bbc5.zip
Commit for 0.4.10
Diffstat (limited to 'examples/BLE_notify')
-rw-r--r--examples/BLE_notify/BLE_notify.ino35
1 files changed, 23 insertions, 12 deletions
diff --git a/examples/BLE_notify/BLE_notify.ino b/examples/BLE_notify/BLE_notify.ino
index 8d329c2..57ad7a7 100644
--- a/examples/BLE_notify/BLE_notify.ino
+++ b/examples/BLE_notify/BLE_notify.ino
@@ -23,8 +23,9 @@
#include <BLEUtils.h>
#include <BLE2902.h>
-BLECharacteristic *pCharacteristic;
+BLEServer *pServer = NULL;
bool deviceConnected = false;
+bool oldDeviceConnected = false;
uint8_t value = 0;
// See the following for generating UUIDs:
@@ -53,14 +54,14 @@ void setup() {
BLEDevice::init("MyESP32");
// Create the BLE Server
- BLEServer *pServer = BLEDevice::createServer();
+ pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
- pCharacteristic = pService->createCharacteristic(
+ BLECharacteristic * pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
@@ -81,13 +82,23 @@ void setup() {
}
void loop() {
-
- if (deviceConnected) {
- Serial.printf("*** NOTIFY: %d ***\n", value);
- pCharacteristic->setValue(&value, 1);
- pCharacteristic->notify();
- //pCharacteristic->indicate();
- value++;
- }
- delay(2000);
+ // notify changed value
+ if (deviceConnected) {
+ pCharacteristic->setValue(&value, 1);
+ pCharacteristic->notify();
+ value++;
+ delay(10); // bluetooth stack will go into congestion, if too many packets are sent
+ }
+ // disconnecting
+ if (!deviceConnected && oldDeviceConnected) {
+ delay(500); // give the bluetooth stack the chance to get things ready
+ pServer->startAdvertising(); // restart advertising
+ Serial.println("start advertising");
+ oldDeviceConnected = deviceConnected;
+ }
+ // connecting
+ if (deviceConnected && !oldDeviceConnected) {
+ // do stuff here on connecting
+ oldDeviceConnected = deviceConnected;
+ }
} \ No newline at end of file