WS_Bluetooth.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "WS_Bluetooth.h"
  2. BLEServer* pServer; // Used to represent a BLE server
  3. BLECharacteristic* pInfoCharacteristic;
  4. //BLECharacteristic* pAddressCharacteristic;
  5. //BLECharacteristic* pStatusCharacteristic;
  6. BLECharacteristic* pCommandCharacteristic;
  7. /********************************************************** Bluetooth *********************************************************/
  8. int Info_Status = 0;
  9. int Info_MacAddress = 1;
  10. int Info_ContactInfo = 2;
  11. int infoType = Info_Status;
  12. class ServerCallbacks : public BLEServerCallbacks {
  13. void onConnect(BLEServer* pServer) {
  14. RGB_Light(0, 0, 60);
  15. infoType = Info_Status;
  16. }
  17. void onDisconnect(BLEServer* pServer) {
  18. infoType = Info_Status;
  19. RGB_Light(0, 0, 0);
  20. BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  21. pAdvertising->addServiceUUID(SERVICE_UUID);
  22. pAdvertising->setScanResponse(true);
  23. pAdvertising->setMinPreferred(0x06);
  24. pAdvertising->setMinPreferred(0x12);
  25. BLEDevice::startAdvertising();
  26. pCommandCharacteristic->notify();
  27. pAdvertising->start();
  28. }
  29. };
  30. class CommandCallback : public BLECharacteristicCallbacks
  31. {
  32. void onWrite(BLECharacteristic* pCharacteristic)
  33. {
  34. std::string command = pCharacteristic->getValue();
  35. if (command.find("CFG") == 0)
  36. SetContactInfo(command.substr(3));
  37. if (command.find("RLYS") == 0)
  38. SetRelays(command.substr(4));
  39. if (command.find("CONT") == 0)
  40. infoType = Info_ContactInfo;
  41. if (command.find("ADDR") == 0)
  42. infoType = Info_MacAddress;
  43. pCharacteristic->setValue("");
  44. }
  45. };
  46. class InfoCallback : public BLECharacteristicCallbacks
  47. {
  48. void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param)
  49. {
  50. std::string data = "";
  51. if (infoType == Info_ContactInfo)
  52. data = GetContactInfo().c_str();
  53. else if (infoType == Info_MacAddress)
  54. data = GetMacAddress().c_str();
  55. else
  56. data = GetRelays().c_str();
  57. std::vector<uint8_t> vector(data.begin(), data.end());
  58. uint8_t *p = &vector[0];
  59. pCharacteristic->setValue(p,data.length());
  60. infoType = Info_Status;
  61. }
  62. };
  63. // class InfoCallback : public BLECharacteristicCallbacks
  64. // {
  65. // void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param)
  66. // {
  67. // std::string info = GetContactInfo();
  68. // std::vector<uint8_t> myVector(info.begin(), info.end());
  69. // uint8_t *p = &myVector[0];
  70. // pCharacteristic->setValue(p,info.length());
  71. // }
  72. // };
  73. // class AddressCallback : public BLECharacteristicCallbacks
  74. // {
  75. // void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param)
  76. // {
  77. // std::string info = GetMacAddress();
  78. // std::vector<uint8_t> myVector(info.begin(), info.end());
  79. // uint8_t *p = &myVector[0];
  80. // pCharacteristic->setValue(p,info.length());
  81. // }
  82. // };
  83. void Bluetooth_Init()
  84. {
  85. /*************************************************************************
  86. Bluetooth
  87. *************************************************************************/
  88. infoType = Info_Status;
  89. BLEDevice::init("PRS Digital Key");
  90. pServer = BLEDevice::createServer();
  91. pServer->setCallbacks(new ServerCallbacks());
  92. BLEService* pService = pServer->createService(SERVICE_UUID);
  93. pInfoCharacteristic = pService->createCharacteristic(INFO_CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ);
  94. pInfoCharacteristic->setCallbacks(new InfoCallback());
  95. // pStatusCharacteristic = pService->createCharacteristic(STATUS_CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ);
  96. // pStatusCharacteristic->setCallbacks(new StatusCallback());
  97. // pAddressCharacteristic = pService->createCharacteristic(ADDRESS_CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ);
  98. // pAddressCharacteristic->setCallbacks(new AddressCallback());
  99. pCommandCharacteristic = pService->createCharacteristic(COMMAND_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_WRITE);
  100. pCommandCharacteristic->setCallbacks(new CommandCallback());
  101. pService->start();
  102. BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  103. pAdvertising->addServiceUUID(SERVICE_UUID);
  104. pAdvertising->setScanResponse(true);
  105. pAdvertising->setMinPreferred(0x06);
  106. pAdvertising->setMinPreferred(0x12);
  107. BLEDevice::startAdvertising();
  108. pCommandCharacteristic->notify();
  109. pAdvertising->start();
  110. }