kolban

Untitled

Sep 22nd, 2017
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. `/**
  2. ` A BLE client example that is rich in capabilities.
  3. `*/
  4. `
  5. `#include "BLEDevice.h"
  6. `//#include "BLEScan.h"
  7. `
  8. `//The remote device (peripheral) we wish to connect
  9. `#define peripheralAddr "c4:be:84:71:39:84"
  10. `// The remote service we wish to connect to.
  11. `//static BLEUUID serviceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");
  12. `static BLEUUID serviceUUID("f000aa00-0451-4000-b000-000000000000");
  13. `// The characteristic of the remote service we are interested in.
  14. `static BLEUUID charUUID("f000aa01-0451-4000-b000-000000000000");
  15. `
  16. `static BLEAddress *pServerAddress;
  17. `static BLEAddress *pPeripheralAddr;
  18. `static boolean doConnect = false;
  19. `static boolean characteristicFound = false;
  20. `static boolean connected = false;
  21. `static BLERemoteCharacteristic* pRemoteCharacteristic;
  22. `
  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. `}
  33. `
  34. `void connectToServer(BLEAddress pAddress)
  35. `{
  36. ` delay(50); /*--D- -(-6-293--)- -B-LE-U--t-i-l-s : coRennceecivtTeodS ae rvGeAPr -e-ve--n-t-: --E-S-P-_-GA--P-_-B-LE--_-S-C-A-N-_S--TO--P-_C-O--M-P-L-E-T-E-_E--VT-<\r>-<\n>
  37. ` -D-- -(6--3-0-7<\r>)<\n>
  38. `*/
  39. ` //Serial.println("----------------- connectToServer ------------------------------");
  40. ` Serial.print("Forming a connection to:");
  41. ` Serial.println(pAddress.toString().c_str());
  42. `
  43. ` BLEClient* pClient = BLEDevice::createClient();
  44. ` Serial.println(" - Created client");
  45. `
  46. ` // Connect to the remove BLE Server.
  47. ` pClient->connect(pAddress);
  48. ` Serial.println(" - Connected to server");
  49. `
  50. ` // Obtain a reference to the service we are after in the remote BLE server.
  51. ` BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  52. ` if (pRemoteService == nullptr)
  53. ` {
  54. ` Serial.print("Failed to find our service UUID: ");
  55. ` Serial.println(serviceUUID.toString().c_str());
  56. ` return;
  57. ` }
  58. ` else
  59. ` {
  60. ` Serial.println("----------------- Found your service ------------------------");
  61. ` }
  62. `
  63. `
  64. ` // Obtain a reference to the characteristic in the service of the remote BLE server.
  65. ` pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  66. ` if (pRemoteCharacteristic == nullptr) {
  67. ` Serial.print("Failed to find our characteristic UUID: ");
  68. ` Serial.println(charUUID.toString().c_str());
  69. ` return;
  70. ` }
  71. ` else
  72. ` {
  73. ` Serial.println("----------------- Found your characteristic ------------------------");
  74. ` characteristicFound = true;
  75. ` }
  76. `
  77. ` // Read the value of the characteristic.
  78. ` std::string value = pRemoteCharacteristic->readValue();
  79. ` Serial.print("The characteristic value was: ");
  80. ` Serial.println(value.c_str());
  81. `
  82. ` pRemoteCharacteristic->registerForNotify(notifyCallback);
  83. `}
  84. `/**
  85. ` Scan for BLE servers and find the first one that advertises the service we are looking for.
  86. `*/
  87. `class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
  88. ` /**
  89. ` Called for each advertising BLE server.
  90. ` */
  91. ` void onResult(BLEAdvertisedDevice advertisedDevice)
  92. ` {
  93. ` //some infos and use of methods
  94. ` Serial.println("----------------------------------------------------------------------");
  95. ` Serial.print("BLE Advertised Device found: [");
  96. ` Serial.println(advertisedDevice.toString().c_str());
  97. ` Serial.print("]\nAddress: [");
  98. ` Serial.print(advertisedDevice.getAddress().toString().c_str());
  99. ` Serial.print("]\nName: [");
  100. ` Serial.print(advertisedDevice.getName().c_str());
  101. ` Serial.print("]\nManufacturerData: [");
  102. ` Serial.print(advertisedDevice.getManufacturerData().c_str());
  103. ` Serial.print("]\nServiceUUID: [");
  104. ` Serial.print(advertisedDevice.getServiceUUID().toString().c_str());
  105. ` Serial.print("]\nRSSI: [");
  106. ` Serial.print(advertisedDevice.getRSSI());
  107. ` Serial.print("]\nTXPower: [");
  108. ` Serial.print(advertisedDevice.getTXPower());
  109. ` Serial.print("]\nAppearance: [");
  110. ` Serial.print(advertisedDevice.getApperance()); //misspelled ? Appearance
  111. ` Serial.println("]");
  112. `
  113. ` // We have found a device, let us now see if it contains the devieceAddress we are looking for.
  114. ` if (advertisedDevice.getAddress().equals(*pPeripheralAddr))
  115. ` {
  116. ` pServerAddress = new BLEAddress(advertisedDevice.getAddress());
  117. ` Serial.println("*******************************************************************");
  118. ` Serial.println("Found matching device!");
  119. ` advertisedDevice.getScan()->stop();
  120. ` doConnect = true;
  121. `
  122. ` } // Found our server
  123. ` } // onResult
  124. `}; // MyAdvertisedDeviceCallbacks
  125. `
  126. `
  127. `void setup() {
  128. ` Serial.begin(115200);
  129. ` Serial.println("Starting Arduino BLE Client application...");
  130. `
  131. ` pPeripheralAddr = new BLEAddress(peripheralAddr);
  132. `
  133. ` BLEDevice::init("");
  134. `
  135. ` // Retrieve a Scanner and set the callback we want to use to be informed when we
  136. ` // have detected a new device. Specify that we want active scanning and start the
  137. ` // scan to run for 30 seconds.
  138. ` BLEScan* pBLEScan = BLEDevice::getScan();
  139. ` pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  140. ` pBLEScan->setActiveScan(true);
  141. ` pBLEScan->start(30);
  142. `} // End of setup.
  143. `
  144. `
  145. `// This is the Arduino main loop function.
  146. `void loop() {
  147. `
  148. ` // If the flag "doConnect" is true then we have scanned for and found the desired
  149. ` // BLE Server with which we wish to connect. Now we connect to it. Once we are
  150. ` // connected we set the connected flag to be true.
  151. ` if (doConnect == true) {
  152. ` connectToServer(*pServerAddress); //this internal reads the GATT
  153. ` doConnect = false;
  154. ` connected = true;
  155. ` }
  156. `
  157. ` // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  158. ` // with the current time since boot.
  159. ` if (connected)
  160. ` {
  161. ` Serial.println("----------------- still connected ------------------------");
  162. ` String newValue = "Time since boot: " + String(millis() / 1000);
  163. `
  164. ` if (characteristicFound == true)
  165. ` {
  166. ` Serial.println("Setting new characteristic value to \"" + newValue + "\"");
  167. ` // Set the characteristic's value to be the array of bytes that is actually a string.
  168. ` pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  169. ` }
  170. ` }
  171. `
  172. ` delay(1000); // Delay a second between loops.
  173. `} // End of loop
Advertisement
Add Comment
Please, Sign In to add comment