Advertisement
pleasedontcode

BLE code

Aug 11th, 2024
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.29 KB | Source Code | 0 0
  1. #include <BLEDevice.h>
  2. #include <BLEUtils.h>
  3. #include <BLEServer.h>
  4.  
  5. // Define the BLE Service and Characteristic UUIDs
  6. #define SERVICE_UUID        "12345678-1234-5678-1234-56789abcdef0"
  7. #define CHARACTERISTIC_UUID "12345678-1234-5678-1234-56789abcdef1"
  8.  
  9. // Create a BLE Server and Characteristic
  10. BLEServer *pServer = nullptr;
  11. BLECharacteristic *pCharacteristic = nullptr;
  12.  
  13. void setup() {
  14.   Serial.begin(115200);
  15.   BLEDevice::init("ESP32_BLE_Server"); // Initialize BLE with a name
  16.  
  17.   // Create the BLE Server
  18.   pServer = BLEDevice::createServer();
  19.  
  20.   // Create the BLE Service
  21.   BLEService *pService = pServer->createService(SERVICE_UUID);
  22.  
  23.   // Create the BLE Characteristic
  24.   pCharacteristic = pService->createCharacteristic(
  25.                       CHARACTERISTIC_UUID,
  26.                       BLECharacteristic::PROPERTY_READ
  27.                     );
  28.  
  29.   // Set the initial value of the characteristic
  30.   pCharacteristic->setValue("Hello from ESP32");
  31.  
  32.   // Start the service
  33.   pService->start();
  34.  
  35.   // Start advertising
  36.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  37.   pAdvertising->addServiceUUID(SERVICE_UUID);
  38.   pAdvertising->start();
  39.  
  40.   Serial.println("BLE Server is running, waiting for clients to connect...");
  41. }
  42.  
  43. void loop() {
  44.   // Nothing to do here
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement