Advertisement
kolban

Untitled

Nov 25th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. **************************************************************************
  2. /**
  3. * A BLE client example that is rich in capabilities.
  4. */
  5.  
  6. #include "BLEDevice.h"
  7. //#include "BLEScan.h"
  8. uint8_t BLEtimeout =0;
  9. uint8_t txData[20];
  10. // The remote service we wish to connect to.
  11. static BLEUUID serviceUUID("0783b03e-8535-b5a0-7140-a304d2495cb7");
  12. // The characteristic of the remote service we are interested in.
  13. static BLEUUID charUUID("0783b03e-8535-b5a0-7140-a304d2495cba");
  14. static BLEUUID rxUUID("0783b03e-8535-b5a0-7140-a304d2495cb8");
  15.  
  16. static BLEAddress *pServerAddress;
  17. BLERemoteService* pRemoteService;
  18. BLEClient* pClient = BLEDevice::createClient();
  19. static boolean doConnect = false;
  20. static boolean connected = false;
  21. bool Scanning = false;
  22. static BLERemoteCharacteristic* pRemoteCharacteristic;
  23. static void notifyCallback(
  24. BLERemoteCharacteristic* pBLERemoteCharacteristic,
  25. uint8_t* pData,
  26. size_t length,
  27. bool isNotify) {
  28. Serial.print("Notify callback for characteristic ");
  29. Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
  30. Serial.print(" of data length ");
  31. Serial.println(length);
  32. Serial.print("Data: ");
  33. Serial.print(pData[0]);
  34. Serial.print(pData[1]);
  35. Serial.print(pData[2]);
  36. Serial.print(pData[3]);
  37. Serial.print(pData[4]);
  38. Serial.print(pData[5]);
  39. Serial.print(pData[6]);
  40. Serial.print(pData[7]);
  41. Serial.print(pData[8]);
  42. Serial.print(pData[9]);
  43. Serial.println(pData[10]);
  44. BLEtimeout = 0;
  45. }
  46.  
  47. bool connectToServer(BLEAddress pAddress) {
  48. Serial.print("Forming a connection to ");
  49. Serial.println(pAddress.toString().c_str());
  50.  
  51.  
  52. Serial.println(" - Created client");
  53.  
  54. // Connect to the remove BLE Server.
  55. pClient->connect(pAddress);
  56. Serial.println(" - Connected to server");
  57.  
  58. // Obtain a reference to the service we are after in the remote BLE server.
  59. pRemoteService = pClient->getService(serviceUUID);
  60. if (pRemoteService == nullptr) {
  61. Serial.print("Failed to find our service UUID: ");
  62. Serial.println(serviceUUID.toString().c_str());
  63. return false;
  64. }
  65. Serial.println(" - Found our service");
  66.  
  67. pRemoteCharacteristic = pRemoteService->getCharacteristic(rxUUID);
  68. if (pRemoteCharacteristic == nullptr) {
  69. Serial.print("Failed to find RX UUID: ");
  70. Serial.println(rxUUID.toString().c_str());
  71. return false;
  72. }
  73.  
  74. // Serial.println(" - Found RX characteristic");
  75. // Read the value of the characteristic.
  76. //std::string value1 = pRemoteCharacteristic->readValue();
  77. // Serial.print("The characteristic value was: ");
  78. // Serial.println(value1.c_str());
  79.  
  80. pRemoteCharacteristic->registerForNotify(notifyCallback);
  81. //pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  82.  
  83. // Obtain a reference to the characteristic in the service of the remote BLE server.
  84. pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  85. if (pRemoteCharacteristic == nullptr) {
  86. Serial.print("Failed to find our characteristic UUID: ");
  87. Serial.println(charUUID.toString().c_str());
  88. return false;
  89. }
  90. Serial.println(" - Found our characteristic");
  91.  
  92. //std::string value = pRemoteCharacteristic->readValue();
  93. //Serial.print("The characteristic value was: ");
  94. //Serial.println(value.c_str());
  95.  
  96.  
  97.  
  98. }
  99. /**
  100. * Scan for BLE servers and find the first one that advertises the service we are looking for.
  101. */
  102. class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
  103. /**
  104. * Called for each advertising BLE server.
  105. */
  106. void onResult(BLEAdvertisedDevice advertisedDevice) {
  107. Serial.print("BLE Advertised Device found: ");
  108. Serial.println(advertisedDevice.toString().c_str());
  109.  
  110. // We have found a device, let us now see if it contains the service we are looking for.
  111. if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
  112.  
  113. //
  114. Serial.print("Found our device! address: ");
  115. advertisedDevice.getScan()->stop();
  116.  
  117. pServerAddress = new BLEAddress(advertisedDevice.getAddress());
  118. doConnect = true;
  119.  
  120. } // Found our server
  121. } // onResult
  122. }; // MyAdvertisedDeviceCallbacks
  123.  
  124.  
  125. void setup() {
  126. Serial.begin(115200);
  127. Serial.println("Starting Arduino BLE Client application...");
  128. BLEDevice::init("");
  129.  
  130. // Retrieve a Scanner and set the callback we want to use to be informed when we
  131. // have detected a new device. Specify that we want active scanning and start the
  132. // scan to run for 30 seconds.
  133. Scanning = true;
  134. BLEScan* pBLEScan = BLEDevice::getScan();
  135. pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  136. pBLEScan->setActiveScan(true);
  137. pBLEScan->start(30);
  138. } // End of setup.
  139.  
  140.  
  141. // This is the Arduino main loop function.
  142. void loop() {
  143.  
  144. // If the flag "doConnect" is true then we have scanned for and found the desired
  145. // BLE Server with which we wish to connect. Now we connect to it. Once we are
  146. // connected we set the connected flag to be true.
  147. if (doConnect == true) {
  148. if (connectToServer(*pServerAddress)) {
  149. Serial.println("We are now connected to the BLE Server.");
  150. connected = true;
  151. // Scanning = false;
  152. }
  153. else {
  154. Serial.println("We have failed to connect to the server; there is nothin more we will do.");
  155. }
  156. doConnect = false;
  157. //Scanning = true;
  158. }
  159.  
  160. // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  161. // with the current time since boot.
  162. if (connected) {
  163. String newValue = "Time since boot: " + String(millis() / 1000);
  164. Serial.println("Setting new characteristic value to \"" + newValue + "\"");
  165.  
  166. Scanning = false;
  167. // Set the characteristic's value to be the array of bytes that is actually a string.
  168.  
  169. //pRemoteCharacteristic->writeValue(txData,20);
  170. //pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  171. pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  172. //std::string value3 = pRemoteCharacteristic->readValue();
  173. // Serial.print("Data:");
  174. //Serial.println(value3.c_str());
  175. }
  176.  
  177.  
  178.  
  179. if (BLEtimeout >= 5) {
  180.  
  181. connected = false;
  182. doConnect = false;
  183. pClient->disconnect();
  184. //Scanning = true;
  185. BLEtimeout = 6;
  186. }
  187. if(connected == false && doConnect == false){
  188. Scanning = true;
  189. BLEScan* pBLEScan = BLEDevice::getScan();
  190. pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  191. pBLEScan->setActiveScan(true);
  192. pBLEScan->start(30);
  193. BLEtimeout = 0;
  194. }
  195. if (Scanning == false){
  196. BLEtimeout++;
  197. }
  198.  
  199.  
  200. Serial.print("connected: ");
  201. Serial.println(connected);
  202.  
  203. Serial.print("doConnect: ");
  204. Serial.println(doConnect);
  205.  
  206.  
  207. Serial.print("Scanning: ");
  208. Serial.println(Scanning);
  209.  
  210. Serial.print("BLETO: ");
  211. Serial.println(BLEtimeout);
  212.  
  213. delay(1000); // Delay a second between loops.
  214. } // End of loop
  215. **********************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement