|
| 1 | +/* |
| 2 | + 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/BLE%20Tests/SampleNotify.cpp |
| 4 | + Ported to Arduino ESP32 by Evandro Copercini |
| 5 | + updated by chegewara |
| 6 | +
|
| 7 | + Create a BLE server that, once we receive a connection, will send periodic notifications. |
| 8 | + The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b |
| 9 | + And has a characteristic of: beb5483e-36e1-4688-b7f5-ea07361b26a8 |
| 10 | +
|
| 11 | + The design of creating the BLE server is: |
| 12 | + 1. Create a BLE Server |
| 13 | + 2. Create a BLE Service |
| 14 | + 3. Create a BLE Characteristic on the Service |
| 15 | + 4. Create a BLE Descriptor on the characteristic |
| 16 | + 5. Start the service. |
| 17 | + 6. Start advertising. |
| 18 | +
|
| 19 | + A connect hander associated with the server starts a background task that performs notification |
| 20 | + every couple of seconds. |
| 21 | +*/ |
| 22 | +#include <BLEDevice.h> |
| 23 | +#include <BLEServer.h> |
| 24 | +#include <BLEUtils.h> |
| 25 | +#include <BLE2902.h> |
| 26 | + |
| 27 | +BLEServer* pServer = NULL; |
| 28 | +BLECharacteristic* pCharacteristic = NULL; |
| 29 | +bool deviceConnected = false; |
| 30 | +bool oldDeviceConnected = false; |
| 31 | +uint32_t value = 0; |
| 32 | + |
| 33 | +// See the following for generating UUIDs: |
| 34 | +// https://www.uuidgenerator.net/ |
| 35 | + |
| 36 | +#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" |
| 37 | +#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" |
| 38 | + |
| 39 | + |
| 40 | +class MyServerCallbacks: public BLEServerCallbacks { |
| 41 | + void onConnect(BLEServer* pServer) { |
| 42 | + deviceConnected = true; |
| 43 | + BLEDevice::startAdvertising(); |
| 44 | + }; |
| 45 | + |
| 46 | + void onDisconnect(BLEServer* pServer) { |
| 47 | + deviceConnected = false; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +void setup() { |
| 54 | + Serial.begin(115200); |
| 55 | + |
| 56 | + // Create the BLE Device |
| 57 | + BLEDevice::init("ESP32 THAT PROJECT"); |
| 58 | + |
| 59 | + // Create the BLE Server |
| 60 | + pServer = BLEDevice::createServer(); |
| 61 | + pServer->setCallbacks(new MyServerCallbacks()); |
| 62 | + |
| 63 | + // Create the BLE Service |
| 64 | + BLEService *pService = pServer->createService(SERVICE_UUID); |
| 65 | + |
| 66 | + // Create a BLE Characteristic |
| 67 | + pCharacteristic = pService->createCharacteristic( |
| 68 | + CHARACTERISTIC_UUID, |
| 69 | + BLECharacteristic::PROPERTY_READ | |
| 70 | + BLECharacteristic::PROPERTY_WRITE | |
| 71 | + BLECharacteristic::PROPERTY_NOTIFY | |
| 72 | + BLECharacteristic::PROPERTY_INDICATE |
| 73 | + ); |
| 74 | + |
| 75 | + // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml |
| 76 | + // Create a BLE Descriptor |
| 77 | + pCharacteristic->addDescriptor(new BLE2902()); |
| 78 | + |
| 79 | + // Start the service |
| 80 | + pService->start(); |
| 81 | + |
| 82 | + // Start advertising |
| 83 | + BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); |
| 84 | + pAdvertising->addServiceUUID(SERVICE_UUID); |
| 85 | + pAdvertising->setScanResponse(false); |
| 86 | + pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter |
| 87 | + BLEDevice::startAdvertising(); |
| 88 | + Serial.println("Waiting a client connection to notify..."); |
| 89 | +} |
| 90 | + |
| 91 | +void loop() { |
| 92 | + // notify changed value |
| 93 | + if (deviceConnected) { |
| 94 | + pCharacteristic->setValue((uint8_t*)&value, 4); |
| 95 | + pCharacteristic->notify(); |
| 96 | + value++; |
| 97 | + delay(500); // 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 |
| 98 | + } |
| 99 | + // disconnecting |
| 100 | + if (!deviceConnected && oldDeviceConnected) { |
| 101 | + delay(500); // give the bluetooth stack the chance to get things ready |
| 102 | + pServer->startAdvertising(); // restart advertising |
| 103 | + Serial.println("start advertising"); |
| 104 | + oldDeviceConnected = deviceConnected; |
| 105 | + } |
| 106 | + // connecting |
| 107 | + if (deviceConnected && !oldDeviceConnected) { |
| 108 | + // do stuff here on connecting |
| 109 | + oldDeviceConnected = deviceConnected; |
| 110 | + } |
| 111 | +} |
0 commit comments