Advertisement
kolban

Untitled

Oct 7th, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #include "BLE.h"
  2. #include "BLEUtils.h"
  3. #include "BLEServer.h"
  4. #include <esp_log.h>
  5. #include
  6. #include <sys/time.h>
  7. #include
  8. #include "Task.h"
  9. #include "BLE2902.h"
  10.  
  11. #include "sdkconfig.h"
  12.  
  13. static char LOG_TAG[] = "SampleHrMon";
  14.  
  15. /* These are the org.bluetooth.service.heart_rate and org.bluetooth.service.battery_service
  16.  
  17. assigned UUIDs they are 16 bit, in 128 bit format. This is for ADV only use the
  18. 16 bit versions for the services and characteristics
  19. */
  20. #define HR_SERVICE_UUID128 "0000180D-0000-1000-8000-00805F9B34FB"
  21. #define BATT_SERVICE_UUID128 "0000180F-0000-1000-8000-00805F9B34FB"
  22. #define HR_SERVICE_UUID (uint16_t) 0x180D
  23. #define BATT_SERVICE_UUID (uint16_t) 0x180F
  24. #define BATT_LEVEL_CHAR_UUID (uint16_t) 0x2A19
  25. #define HR_MEASURE_CHAR_UUID (uint16_t) 0x2A37
  26. #define HR_BODY_LOC_CHAR_UUID (uint16_t) 0x2A38
  27.  
  28. uint8_t serviceUuids[2][16] = {};
  29.  
  30. BLECharacteristic *pBattCharacteristic;
  31. BLECharacteristic *pHrCharacteristic;
  32. BLECharacteristic *pBdSnLocCharacteristic;
  33.  
  34. class MyNotifyTask: public Task {
  35. void run(void *data) {
  36. uint8_t value = 100;
  37. uint8_t hrMeas[8] = {}; // for now it is empty
  38. uint8_t hRate = 60;
  39.  
  40. while(1) {
  41. delay(2000);
  42. ESP_LOGD(LOG_TAG, "*** NOTIFY: %d ***", value);
  43. pBattCharacteristic->setValue(&value, 1);
  44. pBattCharacteristic->notify();
  45.  
  46. // over time our battery will get to 0%
  47.  
  48. if (value > 0)
  49. {
  50. value--;
  51. }
  52.  
  53. ESP_LOGD(LOG_TAG, "*** NOTIFY HR MEASURE %d ***", hRate);
  54. hrMeas[0] = 0x0; // this says we have 1 byte of measure nothing else
  55. hrMeas[1] = hRate;
  56. hRate += 5;
  57. pHrCharacteristic->setValue(hrMeas, 2);
  58. pHrCharacteristic->notify();
  59. }
  60. }
  61. };
  62.  
  63. MyNotifyTask *pMyNotifyTask;
  64.  
  65. class MyServerCallbacks: public BLEServerCallbacks {
  66. void onConnect(BLEServer *pServer) {
  67. pMyNotifyTask->start();
  68. };
  69.  
  70. void onDisconnect(BLEServer *pServer) {
  71. pMyNotifyTask->stop();
  72. }
  73. };
  74.  
  75. static void run() {
  76. esp_ble_adv_data_t *pAdvData;
  77. pMyNotifyTask = new MyNotifyTask();
  78. pMyNotifyTask->setStackSize(8000);
  79.  
  80. BLE::initServer("MYHRMON");
  81. BLEServer *pServer = new BLEServer();
  82.  
  83. BLEService *pBattService = pServer->createService(BLEUUID(BATT_SERVICE_UUID));
  84.  
  85. pServer->setCallbacks(new MyServerCallbacks());
  86.  
  87. pBattCharacteristic = pBattService->createCharacteristic(
  88. BLEUUID(BATT_LEVEL_CHAR_UUID),
  89. BLECharacteristic::PROPERTY_READ |
  90. BLECharacteristic::PROPERTY_WRITE |
  91. BLECharacteristic::PROPERTY_NOTIFY
  92. );
  93.  
  94. // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
  95. BLE2902 *pBLE2902 = new BLE2902();
  96. pBLE2902->setNotifications(true);
  97. pBattCharacteristic->addDescriptor(pBLE2902);
  98.  
  99. pBattService->start();
  100.  
  101. BLEService *pHrService = pServer->createService(BLEUUID(HR_SERVICE_UUID));
  102.  
  103. pHrCharacteristic = pHrService->createCharacteristic(
  104. BLEUUID(HR_MEASURE_CHAR_UUID),
  105. // BLECharacteristic::PROPERTY_READ |
  106. // BLECharacteristic::PROPERTY_WRITE |
  107. BLECharacteristic::PROPERTY_NOTIFY
  108. );
  109.  
  110. // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
  111. BLE2902 *pHmBLE2902 = new BLE2902();
  112. pHmBLE2902->setNotifications(true);
  113. pHrCharacteristic->addDescriptor(pHmBLE2902);
  114.  
  115. pBdSnLocCharacteristic = pHrService->createCharacteristic(
  116. BLEUUID(HR_BODY_LOC_CHAR_UUID),
  117. BLECharacteristic::PROPERTY_READ
  118. );
  119.  
  120. uint8_t newValue = 4; // I think 4 is finger
  121. pBdSnLocCharacteristic->setValue(&newValue, 1);
  122.  
  123. pHrService->start();
  124.  
  125. BLEAdvertising *pAdvertising = pServer->getAdvertising();
  126. pAdvertising->setAppearance(833);
  127.  
  128. /* this is just what we advertise */
  129.  
  130. /* try to create 2 UUIDS in a row. */
  131. BLEUUID *tempUUID = new BLEUUID(HR_SERVICE_UUID128);
  132. memcpy((void *) serviceUuids[0], (const void *) (tempUUID->getNative()->uuid.uuid128), (size_t) 16);
  133. tempUUID = new BLEUUID(BATT_SERVICE_UUID128);
  134. memcpy((void *) serviceUuids[1], (const void *) (tempUUID->getNative()->uuid.uuid128), (size_t) 16);
  135.  
  136. pAdvData = pAdvertising->getAdvDataPtr();
  137. pAdvData->p_service_uuid = &serviceUuids[0][0];
  138. pAdvData->service_uuid_len = 32;
  139.  
  140. // pAdvertising->setServiceUUID(BLEUUID(HR_SERVICE_UUID));
  141. pAdvertising->start();
  142. }
  143.  
  144. void SampleNotify(void)
  145. {
  146. run();
  147. } // app_main`
  148.  
  149. and here are the simple methods I added to BLEAdvertising.h
  150.  
  151. ` esp_ble_adv_data_t *getAdvDataPtr(void)
  152. {
  153. return &m_advData;
  154. };
  155.  
  156. esp_ble_adv_params_t *getAdvParamsPtr(void)
  157. {
  158. return &m_advParams;
  159. };`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement