new characteristicen

This commit is contained in:
lash404 committed 2026-11-03 15:07:08 +01:00
1 parent be742caa46
commit 1114564e2b
1 file changed
+68 -8
+68 -8
View File
@@ -30,15 +30,22 @@ bool timeChanged = false;
Arduino_DataBus *bus = new Arduino_HWSPI(TFT_DC, -1);
Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 0, true, 240, 240, 0, 0);
#define SERVICE_UUID "791cadd7-de87-49f9-8986-94b7ae11940b"
#define SERVICE_UUID "791cadd7-de87-49f9-8986-94b7ae11940b"
#define CHAR_MESSAGE_UUID "3e4eac5b-eaa2-4e0c-a705-dcac4f606e01"
#define CHAR_TIME_UUID "3e4eac5b-eaa2-4e0c-a705-dcac4f606e02"
#define CHAR_DATE_UUID "3e4eac5b-eaa2-4e0c-a705-dcac4f606e03"
#define SERVICE_NOTIFICATION_UUID "791cadd7-de87-49f9-8986-94b7ae11941b"
#define CHAR_NOTIF_APP_UUID "9ff3d4a9-2082-424d-bf34-8dfaf752ff90"
#define CHAR_NOTIF_TITLE_UUID "9ff3d4a9-2082-424d-bf34-8dfaf752ff91"
#define CHAR_NOTIF_TEXT_UUID "9ff3d4a9-2082-424d-bf34-8dfaf752ff92"
#define deviceName "ESP32-Watch"
class CharacteristicCallbacks: public BLECharacteristicCallbacks {
class CharacteristicCallbacksService: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) override {
String value = pCharacteristic->getValue();
@@ -67,10 +74,36 @@ class CharacteristicCallbacks: public BLECharacteristicCallbacks {
}
};
class CharacteristicCallbacksNotification: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) override {
String value = pCharacteristic->getValue();
if (value.length() == 0) return;
//Notification characteristic check
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_NOTIF_APP_UUID))) {
Serial.print("Notification App: ");
Serial.println(value);
message = value;
}
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_NOTIF_TITLE_UUID))) {
Serial.print("Notification Title: ");
Serial.println(value);
}
if (pCharacteristic->getUUID().equals(BLEUUID(CHAR_NOTIF_TEXT_UUID))) {
Serial.print("Notification Text: ");
Serial.println(value);
}
}
};
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
Serial.println("Conecction with client");
connected = true;
BLEDevice::stopAdvertising();
};
void onDisconnect(BLEServer* pServer) {
@@ -94,6 +127,10 @@ void setup() {
}
void loop() {
mainScreen();
}
void mainScreen(){
gfx->setTextSize(2);
gfx->setCursor(20, 20);
if (connected){
@@ -115,7 +152,6 @@ void loop() {
timeChanged = false;
}
updateClock();
}
@@ -123,6 +159,7 @@ void initDevice(){
BLEDevice::init(deviceName);
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
BLESecurity *pSecurity = new BLESecurity();
@@ -130,12 +167,34 @@ void initDevice(){
pSecurity->setCapability(ESP_IO_CAP_NONE);
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
//Message
//---------------ServiceNotificationChar---------------------------------
BLECharacteristic *pCharacteristicApp = pService->createCharacteristic(
CHAR_NOTIF_APP_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE);
pCharacteristicApp->setCallbacks(new CharacteristicCallbacksService());
pCharacteristicApp->setValue("Hello World");
BLECharacteristic *pCharacteristicTitle = pService->createCharacteristic(
CHAR_NOTIF_TITLE_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE);
pCharacteristicTitle->setCallbacks(new CharacteristicCallbacksService());
pCharacteristicTitle->setValue("Hello World");
BLECharacteristic *pCharacteristicText = pService->createCharacteristic(
CHAR_NOTIF_TEXT_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE);
pCharacteristicText->setCallbacks(new CharacteristicCallbacksService());
pCharacteristicText->setValue("Hello World");
//---------------ServiceChar--------------------------
BLECharacteristic *pCharacteristicMessage = pService->createCharacteristic(
CHAR_MESSAGE_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE);
pCharacteristicMessage->setCallbacks(new CharacteristicCallbacks());
pCharacteristicMessage->setCallbacks(new CharacteristicCallbacksService());
pCharacteristicMessage->setValue("Hello World");
//Time
@@ -143,18 +202,19 @@ void initDevice(){
CHAR_TIME_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE);
pCharacteristicTime->setCallbacks(new CharacteristicCallbacks());
pCharacteristicTime->setCallbacks(new CharacteristicCallbacksService());
pCharacteristicTime->setValue("TIME");
BLECharacteristic *pCharacteristicDate = pService->createCharacteristic(
CHAR_DATE_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE);
pCharacteristicDate->setCallbacks(new CharacteristicCallbacks());
pCharacteristicDate->setCallbacks(new CharacteristicCallbacksService());
pCharacteristicDate->setValue("Date");
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
Serial.println(ESP.getFreeHeap());
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);