Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@elktros
Created March 24, 2021 13:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elktros/e598ee2d7989dac511665020ba1ca49f to your computer and use it in GitHub Desktop.
Save elktros/e598ee2d7989dac511665020ba1ca49f to your computer and use it in GitHub Desktop.
ESP32 BLE Server
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
/* BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
); */
BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristic;
void setup()
{
Serial.begin(115200);
Serial.println("Starting BLE Server!");
BLEDevice::init("ESP32-BLE-Server");
pServer = BLEDevice::createServer();
pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
/* BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);*/
pCharacteristic->setValue("Hello, World!");
pService->start();
//BLEAdvertising *pAdvertising = pServer->getAdvertising();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
//pAdvertising->start();
Serial.println("Characteristic defined! Now you can read it in the Client!");
}
void loop()
{
std::string value = pCharacteristic->getValue();
Serial.print("The new characteristic value is: ");
Serial.println(value.c_str());
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment