Advertisement
jayemsydney

Untitled

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