├── README.md └── Arduino └── BLE_server └── BLE_server.ino /README.md: -------------------------------------------------------------------------------- 1 | # Esp32BlePart1 2 | Simple example of an ESP32 based Bluetooth Low Energy (BLE) Server. Server sends out a counter every second with Notify. 3 | 4 | # How to use 5 | https://youtu.be/0Yvd_k0hbVs 6 | -------------------------------------------------------------------------------- /Arduino/BLE_server/BLE_server.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp 3 | Ported to Arduino ESP32 by Evandro Copercini 4 | updated by chegewara and MoThunderz 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | BLEServer* pServer = NULL; 12 | BLECharacteristic* pCharacteristic = NULL; 13 | BLEDescriptor *pDescr; 14 | BLE2902 *pBLE2902; 15 | 16 | bool deviceConnected = false; 17 | bool oldDeviceConnected = false; 18 | uint32_t value = 0; 19 | 20 | // See the following for generating UUIDs: 21 | // https://www.uuidgenerator.net/ 22 | 23 | #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" 24 | #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" 25 | 26 | class MyServerCallbacks: public BLEServerCallbacks { 27 | void onConnect(BLEServer* pServer) { 28 | deviceConnected = true; 29 | }; 30 | 31 | void onDisconnect(BLEServer* pServer) { 32 | deviceConnected = false; 33 | } 34 | }; 35 | 36 | void setup() { 37 | Serial.begin(115200); 38 | 39 | // Create the BLE Device 40 | BLEDevice::init("ESP32"); 41 | 42 | // Create the BLE Server 43 | pServer = BLEDevice::createServer(); 44 | pServer->setCallbacks(new MyServerCallbacks()); 45 | 46 | // Create the BLE Service 47 | BLEService *pService = pServer->createService(SERVICE_UUID); 48 | 49 | // Create a BLE Characteristic 50 | pCharacteristic = pService->createCharacteristic( 51 | CHARACTERISTIC_UUID, 52 | BLECharacteristic::PROPERTY_NOTIFY 53 | ); 54 | 55 | // Create a BLE Descriptor 56 | 57 | pDescr = new BLEDescriptor((uint16_t)0x2901); 58 | pDescr->setValue("A very interesting variable"); 59 | pCharacteristic->addDescriptor(pDescr); 60 | 61 | pBLE2902 = new BLE2902(); 62 | pBLE2902->setNotifications(true); 63 | pCharacteristic->addDescriptor(pBLE2902); 64 | 65 | // Start the service 66 | pService->start(); 67 | 68 | // Start advertising 69 | BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); 70 | pAdvertising->addServiceUUID(SERVICE_UUID); 71 | pAdvertising->setScanResponse(false); 72 | pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter 73 | BLEDevice::startAdvertising(); 74 | Serial.println("Waiting a client connection to notify..."); 75 | } 76 | 77 | void loop() { 78 | // notify changed value 79 | if (deviceConnected) { 80 | pCharacteristic->setValue(value); 81 | pCharacteristic->notify(); 82 | value++; 83 | delay(1000); 84 | } 85 | // disconnecting 86 | if (!deviceConnected && oldDeviceConnected) { 87 | delay(500); // give the bluetooth stack the chance to get things ready 88 | pServer->startAdvertising(); // restart advertising 89 | Serial.println("start advertising"); 90 | oldDeviceConnected = deviceConnected; 91 | } 92 | // connecting 93 | if (deviceConnected && !oldDeviceConnected) { 94 | // do stuff here on connecting 95 | oldDeviceConnected = deviceConnected; 96 | } 97 | } 98 | --------------------------------------------------------------------------------