Advertisement
frowin

Untitled

May 29th, 2020
2,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  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.     };
  44.  
  45.     void onDisconnect(BLEServer* pServer) {
  46.       deviceConnected = false;
  47.     }
  48. };
  49.  
  50.  
  51.  
  52. void setup() {
  53.   Serial.begin(115200);
  54.  
  55.   // Create the BLE Device
  56.   BLEDevice::init("ESP32");
  57.  
  58.   // Create the BLE Server
  59.   pServer = BLEDevice::createServer();
  60.   pServer->setCallbacks(new MyServerCallbacks());
  61.  
  62.   // Create the BLE Service
  63.   BLEService *pService = pServer->createService(SERVICE_UUID);
  64.  
  65.   // Create a BLE Characteristic
  66.   pCharacteristic = pService->createCharacteristic(
  67.                       CHARACTERISTIC_UUID,
  68.                       BLECharacteristic::PROPERTY_READ   |
  69.                       BLECharacteristic::PROPERTY_WRITE  |
  70.                       BLECharacteristic::PROPERTY_NOTIFY |
  71.                       BLECharacteristic::PROPERTY_INDICATE
  72.                     );
  73.  
  74.   // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
  75.   // Create a BLE Descriptor
  76.   pCharacteristic->addDescriptor(new BLE2902());
  77.  
  78.   // Start the service
  79.   pService->start();
  80.  
  81.   // Start advertising
  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();
  87.   Serial.println("Waiting a client connection to notify...");
  88. }
  89.  
  90. void loop() {
  91.     // notify changed value
  92.     if (deviceConnected) {
  93.         pCharacteristic->setValue((uint8_t*)&value, 4);
  94.         pCharacteristic->notify();
  95.         value++;
  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
  97.     }
  98.     // disconnecting
  99.     if (!deviceConnected && oldDeviceConnected) {
  100.         delay(500); // give the bluetooth stack the chance to get things ready
  101.         pServer->startAdvertising(); // restart advertising
  102.         Serial.println("start advertising");
  103.         oldDeviceConnected = deviceConnected;
  104.     }
  105.     // connecting
  106.     if (deviceConnected && !oldDeviceConnected) {
  107.         // do stuff here on connecting
  108.         oldDeviceConnected = deviceConnected;
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement