Advertisement
Guest User

Untitled

a guest
May 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. class MyCallbackHandler : public BLECharacteristicCallbacks
  2. {
  3. public:
  4.     QueueHandle_t &queue;
  5.     MyCallbackHandler(QueueHandle_t &queue) : queue(queue) {}
  6.     void onWrite(BLECharacteristic *pCharacteristic)
  7.     {
  8.         std::string value = pCharacteristic->getValue();
  9.         if (value.length() > 0)
  10.         {
  11.             ESP_LOGD(LOG_TAG, "*********");
  12.             ESP_LOGD(LOG_TAG, "New value: %s", value.c_str());
  13.             ESP_LOGD(LOG_TAG, "*********");
  14.             Action action{2, 10, 1};
  15.             action.channel = value[1];
  16.             if (value[0] == 1)
  17.             {
  18.                 action.type = 1;
  19.                 ESP_LOGI("MESSAGE", "SHOCK");
  20.             }
  21.             else if (value[0] == 2)
  22.             {
  23.                 action.type = 2;
  24.                 ESP_LOGI("MESSAGE", "VIBRATE");
  25.             }
  26.             else if (value[0] == 3)
  27.             {
  28.                 action.type = 3;
  29.                 ESP_LOGI("MESSAGE", "SOUND");
  30.             }
  31.             action.value = value[2];
  32.             xQueueSend(queue, &action, portMAX_DELAY);
  33.         }
  34.     }
  35. };
  36.  
  37.  
  38. class MainBLEServer : public Task
  39. {
  40.     void run(void *data)
  41.     {
  42.         ESP_LOGD(LOG_TAG, "Starting BLE work!");
  43.  
  44.         BLEDevice::init("SHOCK_THE_WOLF");
  45.         BLEServer *pServer = BLEDevice::createServer();
  46.         BLEService *pService = pServer->createService("91bad492-b950-4226-aa2b-4ede9fa42f59");
  47.  
  48.         BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  49.             BLEUUID("0d563a58-196a-48ce-ace2-dfec78acc814"),
  50.             BLECharacteristic::PROPERTY_BROADCAST | BLECharacteristic::PROPERTY_READ |
  51.                 BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE |
  52.                 BLECharacteristic::PROPERTY_INDICATE);
  53.  
  54.         pCharacteristic->setCallbacks(new MyCallbackHandler(messagQueue));
  55.  
  56.         BLE2902 *p2902Descriptor = new BLE2902();
  57.         p2902Descriptor->setNotifications(true);
  58.         pCharacteristic->addDescriptor(p2902Descriptor);
  59.  
  60.         pService->start();
  61.  
  62.         BLEAdvertising *pAdvertising = pServer->getAdvertising();
  63.         pAdvertising->addServiceUUID(pService->getUUID());
  64.         pAdvertising->start();
  65.  
  66.         ESP_LOGD(LOG_TAG, "Advertising started!");
  67.         delay(1000000);
  68.     }
  69. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement