Guest User

Untitled

a guest
Nov 16th, 2025
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLE2902.h>
  5.  
  6.  
  7. BLECharacteristic *pCharacteristic;
  8.  
  9.  
  10. bool deviceConnected = false;
  11.  
  12.  
  13. char value[50] = "Default";
  14.  
  15.  
  16. #define customService BLEUUID((uint16_t)0x1700)
  17. BLECharacteristic customCharacteristic(BLEUUID((uint16_t)0x1A00), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
  18.  
  19.  
  20. class MyCharacteristicCallbacks: public BLECharacteristicCallbacks {
  21. void onWrite(BLECharacteristic *customCharacteristic){
  22. String rcvString = customCharacteristic->getValue();
  23. if(rcvString.length()>0){
  24. Serial.println("Value Received from BLE: ");
  25. for (int i = 0; i < rcvString.length(); ++i)
  26. {
  27. Serial.print(rcvString[i]);
  28. value[i]=rcvString[i];
  29. }
  30. for (int i = rcvString.length(); i < 50; ++i)
  31. {
  32. value[i]=NULL;
  33. }
  34. customCharacteristic->setValue((char*)&value);
  35. }
  36. else{
  37. Serial.println("Empty Value Received!");
  38. }
  39. }
  40. };
  41.  
  42.  
  43. void setup() {
  44. Serial.begin(115200);
  45.  
  46.  
  47. // Create the BLE Device
  48. BLEDevice::init("MyESP32");
  49.  
  50.  
  51. // Create the BLE Server
  52. BLEServer *pServer = BLEDevice::createServer();
  53.  
  54.  
  55. // Create the BLE Service
  56. BLEService *pService = pServer->createService(customService);
  57.  
  58.  
  59. // Create a BLE Characteristic
  60. pService->addCharacteristic(&customCharacteristic);
  61.  
  62.  
  63. customCharacteristic.setCallbacks(new MyCharacteristicCallbacks());
  64.  
  65.  
  66. pServer->getAdvertising()->addServiceUUID(customService);
  67.  
  68.  
  69. // Start the service
  70. pService->start();
  71.  
  72.  
  73. // Start advertising
  74. pServer->getAdvertising()->start();
  75.  
  76.  
  77. Serial.println(value);
  78. customCharacteristic.setValue((char*)&value);
  79. }
  80.  
  81.  
  82. void loop() {
  83. delay(100);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment