Page 1 of 1

How to send a new characteristic value from the ESP32 client to the esp32 BLE server?

Posted: Fri Mar 05, 2021 6:52 am
by alexpe28
I can connect to the server through the BLE scanner application and send the value to the ELEM_UUID characteristic.
And I don’t understand how to write a sketch on the ESP32 client so that he would do the same without a smartphone.
Who understands please write how to do it.
Thank you very much in advance!

Code ESP32 Server:
  1. #include <BLEDevice.h>
  2. #include <BLEUtils.h>
  3. #include <BLEServer.h>
  4.  
  5. // See the following for generating UUIDs:
  6. // https://www.uuidgenerator.net/
  7.  
  8. #define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
  9. #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
  10.  
  11. #define ELEM_UUID              "cc46b944-003e-42b6-b836-c4246b8f19a0" // ID
  12.  
  13. void setup() {
  14.   Serial.begin(115200);
  15.   Serial.println("Starting BLE work!");
  16.  
  17.   BLEDevice::init("Long name works now");
  18.   BLEServer *pServer = BLEDevice::createServer();
  19.   BLEService *pService = pServer->createService(SERVICE_UUID);
  20.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  21.                                          CHARACTERISTIC_UUID,
  22.                                          BLECharacteristic::PROPERTY_READ |
  23.                                          BLECharacteristic::PROPERTY_WRITE
  24.                                        );
  25.  
  26.   BLECharacteristic *pElem;
  27.  
  28.   class ElemCallbacs : public BLECharacteristicCallbacks {
  29.   void onWrite(BLECharacteristic *pCharacteristic) {
  30.       std::string value1 = pCharacteristic->getValue();
  31.       Serial.println(value1.c_str());
  32.   }
  33. };
  34.   pElem = pService->createCharacteristic(ELEM_UUID,BLECharacteristic::PROPERTY_READ| BLECharacteristic::PROPERTY_WRITE);
  35.   pElem->setCallbacks(new ElemCallbacs());
  36.  
  37.   pCharacteristic->setValue("Hello World says Neil");
  38.   pService->start();
  39.   // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  40.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  41.   pAdvertising->addServiceUUID(SERVICE_UUID);
  42.   pAdvertising->setScanResponse(true);
  43.   pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  44.   pAdvertising->setMinPreferred(0x12);
  45.   BLEDevice::startAdvertising();
  46.   Serial.println("Characteristic defined! Now you can read it in your phone!");
  47. }
  48.  
  49. void loop() {
  50.   // put your main code here, to run repeatedly:
  51.  
  52.   delay(2000);
  53.  
  54. }
Image