Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyCallbackHandler : public BLECharacteristicCallbacks
- {
- public:
- QueueHandle_t &queue;
- MyCallbackHandler(QueueHandle_t &queue) : queue(queue) {}
- void onWrite(BLECharacteristic *pCharacteristic)
- {
- std::string value = pCharacteristic->getValue();
- if (value.length() > 0)
- {
- ESP_LOGD(LOG_TAG, "*********");
- ESP_LOGD(LOG_TAG, "New value: %s", value.c_str());
- ESP_LOGD(LOG_TAG, "*********");
- Action action{2, 10, 1};
- action.channel = value[1];
- if (value[0] == 1)
- {
- action.type = 1;
- ESP_LOGI("MESSAGE", "SHOCK");
- }
- else if (value[0] == 2)
- {
- action.type = 2;
- ESP_LOGI("MESSAGE", "VIBRATE");
- }
- else if (value[0] == 3)
- {
- action.type = 3;
- ESP_LOGI("MESSAGE", "SOUND");
- }
- action.value = value[2];
- xQueueSend(queue, &action, portMAX_DELAY);
- }
- }
- };
- class MainBLEServer : public Task
- {
- void run(void *data)
- {
- ESP_LOGD(LOG_TAG, "Starting BLE work!");
- BLEDevice::init("SHOCK_THE_WOLF");
- BLEServer *pServer = BLEDevice::createServer();
- BLEService *pService = pServer->createService("91bad492-b950-4226-aa2b-4ede9fa42f59");
- BLECharacteristic *pCharacteristic = pService->createCharacteristic(
- BLEUUID("0d563a58-196a-48ce-ace2-dfec78acc814"),
- BLECharacteristic::PROPERTY_BROADCAST | BLECharacteristic::PROPERTY_READ |
- BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE |
- BLECharacteristic::PROPERTY_INDICATE);
- pCharacteristic->setCallbacks(new MyCallbackHandler(messagQueue));
- BLE2902 *p2902Descriptor = new BLE2902();
- p2902Descriptor->setNotifications(true);
- pCharacteristic->addDescriptor(p2902Descriptor);
- pService->start();
- BLEAdvertising *pAdvertising = pServer->getAdvertising();
- pAdvertising->addServiceUUID(pService->getUUID());
- pAdvertising->start();
- ESP_LOGD(LOG_TAG, "Advertising started!");
- delay(1000000);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement