#include "WS_Bluetooth.h" BLEServer* pServer; // Used to represent a BLE server BLECharacteristic* pTxCharacteristic; BLECharacteristic* pRxCharacteristic; /********************************************************** Bluetooth *********************************************************/ class MyServerCallbacks : public BLEServerCallbacks { //By overriding the onConnect() and onDisconnect() functions void onConnect(BLEServer* pServer) { // When the Device is connected, "Device connected" is printed. RGB_Light(0, 0, 60); } void onDisconnect(BLEServer* pServer) { // "Device disconnected" will be printed when the device is disconnected RGB_Light(0, 0, 0); BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); // Re-broadcast so that the device can query pAdvertising->addServiceUUID(SERVICE_UUID); // Re-broadcast so that the device can query pAdvertising->setScanResponse(true); // Re-broadcast so that the device can query pAdvertising->setMinPreferred(0x06); // Re-broadcast so that the device can query pAdvertising->setMinPreferred(0x12); // Re-broadcast so that the device can query BLEDevice::startAdvertising(); // Re-broadcast so that the device can query pRxCharacteristic->notify(); // Re-broadcast so that the device can query pAdvertising->start(); // Re-broadcast so that the device can query } }; class MyRXCallback : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic* pCharacteristic) { SetRelays(pCharacteristic->getValue()); pRxCharacteristic->setValue(""); } }; class MyTXCallback : public BLECharacteristicCallbacks { void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) { pTxCharacteristic->setValue(GetRelays()); } }; // void SetStatus(char* Data) { // Send data using Bluetooth // if (Data != nullptr && strlen(Data) > 0) { // if (pServer->getConnectedCount() > 0) { // std::string SendValue = Data; // pTxCharacteristic->setValue(SendValue); // Set SendValue to the eigenvalue // pTxCharacteristic->notify(); // Sends a notification to all connected devices // } // } // } void Bluetooth_Init() { /************************************************************************* Bluetooth *************************************************************************/ BLEDevice::init("ESP32 S3 Relay 6CH"); // Initialize Bluetooth and start broadcasting pServer = BLEDevice::createServer(); pServer->setCallbacks(new MyServerCallbacks()); BLEService* pService = pServer->createService(SERVICE_UUID); pTxCharacteristic = pService->createCharacteristic( TX_CHARACTERISTIC_UUID, BLECharacteristic:: PROPERTY_READ); // The eigenvalues are readable and can be read by remote devices pTxCharacteristic->setCallbacks(new MyTXCallback()); pRxCharacteristic = pService->createCharacteristic( RX_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_WRITE); // The eigenvalues are writable and can be written to by remote devices pRxCharacteristic->setCallbacks(new MyRXCallback()); //pRxCharacteristic->setValue("Connected!"); pService->start(); BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(true); pAdvertising->setMinPreferred(0x06); pAdvertising->setMinPreferred(0x12); BLEDevice::startAdvertising(); pRxCharacteristic->notify(); pAdvertising->start(); }