#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define SERVICE_UUID "0000180D-0000-1000-8000-00805F9B34FB" // Replace with your service UUID
#define CHARACTERISTIC_UUID "00002A37-0000-1000-8000-00805F9B34FB" // Replace with your characteristic UUID
#define BEACON_NAME "YourBLEBeacon" // Set your beacon name here
BLEServer* pServer = NULL; // Represents the BLE peripheral device in a BLE communication setup. pServer is an instance of the BLEServer
BLECharacteristic* pCharacteristic = NULL; // Is a specific data element within a BLE service. Characteristics are used to represent specific data or features provided by the peripheral device
bool deviceConnected = false;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
// Handle data received from the beacon
}
};
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init(BEACON_NAME);
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));
pCharacteristic = pService->createCharacteristic(
BLEUUID(CHARACTERISTIC_UUID),
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCharacteristicCallbacks());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
// Handle other tasks here
}