Advertisement
jayemsydney

Untitled

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