123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #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<uint8_t> 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<uint8_t> 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<uint8_t> 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();
-
- }
|