#include "WS_Bluetooth.h" BLEServer* pServer; // Used to represent a BLE server BLECharacteristic* pInfoCharacteristic; //BLECharacteristic* pAddressCharacteristic; //BLECharacteristic* pStatusCharacteristic; BLECharacteristic* pCommandCharacteristic; /********************************************************** Bluetooth *********************************************************/ int Info_Status = 0; int Info_MacAddress = 1; int Info_ContactInfo = 2; int infoType = Info_Status; class ServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer* pServer) { RGB_Light(0, 0, 60); infoType = Info_Status; } void onDisconnect(BLEServer* pServer) { infoType = Info_Status; RGB_Light(0, 0, 0); BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(true); pAdvertising->setMinPreferred(0x06); pAdvertising->setMinPreferred(0x12); BLEDevice::startAdvertising(); pCommandCharacteristic->notify(); pAdvertising->start(); } }; class CommandCallback : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic* pCharacteristic) { std::string command = pCharacteristic->getValue(); if (command.find("CFG") == 0) SetContactInfo(command.substr(3)); if (command.find("RLYS") == 0) SetRelays(command.substr(4)); if (command.find("CONT") == 0) infoType = Info_ContactInfo; if (command.find("ADDR") == 0) infoType = Info_MacAddress; pCharacteristic->setValue(""); } }; class InfoCallback : public BLECharacteristicCallbacks { void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) { std::string data = ""; if (infoType == Info_ContactInfo) data = GetContactInfo().c_str(); else if (infoType == Info_MacAddress) data = GetMacAddress().c_str(); else data = GetRelays().c_str(); std::vector vector(data.begin(), data.end()); uint8_t *p = &vector[0]; pCharacteristic->setValue(p,data.length()); infoType = Info_Status; } }; // class InfoCallback : public BLECharacteristicCallbacks // { // void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) // { // std::string info = GetContactInfo(); // std::vector myVector(info.begin(), info.end()); // uint8_t *p = &myVector[0]; // pCharacteristic->setValue(p,info.length()); // } // }; // class AddressCallback : public BLECharacteristicCallbacks // { // void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) // { // std::string info = GetMacAddress(); // std::vector myVector(info.begin(), info.end()); // uint8_t *p = &myVector[0]; // pCharacteristic->setValue(p,info.length()); // } // }; void Bluetooth_Init() { /************************************************************************* Bluetooth *************************************************************************/ infoType = Info_Status; BLEDevice::init("PRS Digital Key"); pServer = BLEDevice::createServer(); pServer->setCallbacks(new ServerCallbacks()); BLEService* pService = pServer->createService(SERVICE_UUID); pInfoCharacteristic = pService->createCharacteristic(INFO_CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ); pInfoCharacteristic->setCallbacks(new InfoCallback()); // pStatusCharacteristic = pService->createCharacteristic(STATUS_CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ); // pStatusCharacteristic->setCallbacks(new StatusCallback()); // pAddressCharacteristic = pService->createCharacteristic(ADDRESS_CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ); // pAddressCharacteristic->setCallbacks(new AddressCallback()); pCommandCharacteristic = pService->createCharacteristic(COMMAND_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_WRITE); pCommandCharacteristic->setCallbacks(new CommandCallback()); pService->start(); BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); pAdvertising->addServiceUUID(SERVICE_UUID); pAdvertising->setScanResponse(true); pAdvertising->setMinPreferred(0x06); pAdvertising->setMinPreferred(0x12); BLEDevice::startAdvertising(); pCommandCharacteristic->notify(); pAdvertising->start(); }