WS_Bluetooth.cpp 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "WS_Bluetooth.h"
  2. BLEServer* pServer; // Used to represent a BLE server
  3. BLECharacteristic* pTxCharacteristic;
  4. BLECharacteristic* pRxCharacteristic;
  5. /********************************************************** Bluetooth *********************************************************/
  6. class MyServerCallbacks : public BLEServerCallbacks { //By overriding the onConnect() and onDisconnect() functions
  7. void onConnect(BLEServer* pServer) { // When the Device is connected, "Device connected" is printed.
  8. RGB_Light(0, 0, 60);
  9. }
  10. void onDisconnect(BLEServer* pServer) { // "Device disconnected" will be printed when the device is disconnected
  11. RGB_Light(0, 0, 0);
  12. BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); // Re-broadcast so that the device can query
  13. pAdvertising->addServiceUUID(SERVICE_UUID); // Re-broadcast so that the device can query
  14. pAdvertising->setScanResponse(true); // Re-broadcast so that the device can query
  15. pAdvertising->setMinPreferred(0x06); // Re-broadcast so that the device can query
  16. pAdvertising->setMinPreferred(0x12); // Re-broadcast so that the device can query
  17. BLEDevice::startAdvertising(); // Re-broadcast so that the device can query
  18. pRxCharacteristic->notify(); // Re-broadcast so that the device can query
  19. pAdvertising->start(); // Re-broadcast so that the device can query
  20. }
  21. };
  22. class MyRXCallback : public BLECharacteristicCallbacks
  23. {
  24. void onWrite(BLECharacteristic* pCharacteristic)
  25. {
  26. SetRelays(pCharacteristic->getValue());
  27. pRxCharacteristic->setValue("");
  28. }
  29. };
  30. class MyTXCallback : public BLECharacteristicCallbacks
  31. {
  32. void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param)
  33. {
  34. pTxCharacteristic->setValue(GetRelays());
  35. }
  36. };
  37. // void SetStatus(char* Data) { // Send data using Bluetooth
  38. // if (Data != nullptr && strlen(Data) > 0) {
  39. // if (pServer->getConnectedCount() > 0) {
  40. // std::string SendValue = Data;
  41. // pTxCharacteristic->setValue(SendValue); // Set SendValue to the eigenvalue
  42. // pTxCharacteristic->notify(); // Sends a notification to all connected devices
  43. // }
  44. // }
  45. // }
  46. void Bluetooth_Init()
  47. {
  48. /*************************************************************************
  49. Bluetooth
  50. *************************************************************************/
  51. BLEDevice::init("ESP32 S3 Relay 6CH"); // Initialize Bluetooth and start broadcasting
  52. pServer = BLEDevice::createServer();
  53. pServer->setCallbacks(new MyServerCallbacks());
  54. BLEService* pService = pServer->createService(SERVICE_UUID);
  55. pTxCharacteristic = pService->createCharacteristic(
  56. TX_CHARACTERISTIC_UUID,
  57. BLECharacteristic:: PROPERTY_READ); // The eigenvalues are readable and can be read by remote devices
  58. pTxCharacteristic->setCallbacks(new MyTXCallback());
  59. pRxCharacteristic = pService->createCharacteristic(
  60. RX_CHARACTERISTIC_UUID,
  61. BLECharacteristic::PROPERTY_WRITE); // The eigenvalues are writable and can be written to by remote devices
  62. pRxCharacteristic->setCallbacks(new MyRXCallback());
  63. //pRxCharacteristic->setValue("Connected!");
  64. pService->start();
  65. BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  66. pAdvertising->addServiceUUID(SERVICE_UUID);
  67. pAdvertising->setScanResponse(true);
  68. pAdvertising->setMinPreferred(0x06);
  69. pAdvertising->setMinPreferred(0x12);
  70. BLEDevice::startAdvertising();
  71. pRxCharacteristic->notify();
  72. pAdvertising->start();
  73. }