ble_beacon.ino
Creates a custom BLE service visible to smartphones.
Uses the native ESP32 BLE stack. The device broadcasts a service UUID. Use an app like 'nRF Connect' on Android/iOS to detect the 'M5PaperDIY_Beacon' device and read the characteristic. BLE mode consumes less power than WiFi.
ble_beacon.ino
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("M5PaperDIY_Beacon");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService("12345678-1234-1234-1234-1234567890ab");
pCharacteristic = pService->createCharacteristic(
"abcdefab-1234-1234-1234-1234567890ab",
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello from ESP32-S3");
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID("12345678-1234-1234-1234-1234567890ab");
pAdvertising->setScanResponse(true);
pAdvertising->start();
Serial.println("BLE Beacon started");
}
void loop() {
delay(1000);
}