SHARE
TWEET

BLE Client Example

a guest Jul 7th, 2018 363 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 service we wish to connect to.
  9. static BLEUUID serviceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");
  10. // The characteristic of the remote service we are interested in.
  11. static BLEUUID    charUUID("0d563a58-196a-48ce-ace2-dfec78acc814");
  12.  
  13. static BLEAddress *pServerAddress;
  14. static boolean doConnect = false;
  15. static boolean connected = false;
  16. static BLERemoteCharacteristic* pRemoteCharacteristic;
  17.  
  18. static void notifyCallback(
  19.   BLERemoteCharacteristic* pBLERemoteCharacteristic,
  20.   uint8_t* pData,
  21.   size_t length,
  22.   bool isNotify) {
  23.     Serial.print("Notify callback for characteristic ");
  24.     Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
  25.     Serial.print(" of data length ");
  26.     Serial.println(length);
  27. }
  28.  
  29. bool connectToServer(BLEAddress pAddress) {
  30.     Serial.print("Forming a connection to ");
  31.     Serial.println(pAddress.toString().c_str());
  32.    
  33.     BLEClient*  pClient  = BLEDevice::createClient();
  34.     Serial.println(" - Created client");
  35.  
  36.     // Connect to the remove BLE Server.
  37.     pClient->connect(pAddress);
  38.     Serial.println(" - Connected to server");
  39.  
  40.     // Obtain a reference to the service we are after in the remote BLE server.
  41.     BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  42.     if (pRemoteService == nullptr) {
  43.       Serial.print("Failed to find our service UUID: ");
  44.       Serial.println(serviceUUID.toString().c_str());
  45.       return false;
  46.     }
  47.     Serial.println(" - Found our service");
  48.  
  49.  
  50.     // Obtain a reference to the characteristic in the service of the remote BLE server.
  51.     pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  52.     if (pRemoteCharacteristic == nullptr) {
  53.       Serial.print("Failed to find our characteristic UUID: ");
  54.       Serial.println(charUUID.toString().c_str());
  55.       return false;
  56.     }
  57.     Serial.println(" - Found our characteristic");
  58.  
  59.     // Read the value of the characteristic.
  60.     std::string value = pRemoteCharacteristic->readValue();
  61.     Serial.print("The characteristic value was: ");
  62.     Serial.println(value.c_str());
  63.  
  64.     pRemoteCharacteristic->registerForNotify(notifyCallback);
  65. }
  66. /**
  67.  * Scan for BLE servers and find the first one that advertises the service we are looking for.
  68.  */
  69. class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
  70.  /**
  71.    * Called for each advertising BLE server.
  72.    */
  73.   void onResult(BLEAdvertisedDevice advertisedDevice) {
  74.     Serial.print("BLE Advertised Device found: ");
  75.     Serial.println(advertisedDevice.toString().c_str());
  76.  
  77.     // We have found a device, let us now see if it contains the service we are looking for.
  78.     if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
  79.  
  80.       //
  81.       Serial.print("Found our device!  address: ");
  82.       advertisedDevice.getScan()->stop();
  83.  
  84.       pServerAddress = new BLEAddress(advertisedDevice.getAddress());
  85.       doConnect = true;
  86.  
  87.     } // Found our server
  88.   } // onResult
  89. }; // MyAdvertisedDeviceCallbacks
  90.  
  91.  
  92. void setup() {
  93.   Serial.begin(115200);
  94.   Serial.println("Starting Arduino BLE Client application...");
  95.   BLEDevice::init("");
  96.  
  97.   // Retrieve a Scanner and set the callback we want to use to be informed when we
  98.   // have detected a new device.  Specify that we want active scanning and start the
  99.   // scan to run for 30 seconds.
  100.   BLEScan* pBLEScan = BLEDevice::getScan();
  101.   pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  102.   pBLEScan->setActiveScan(true);
  103.   pBLEScan->start(30);
  104. } // End of setup.
  105.  
  106.  
  107. // This is the Arduino main loop function.
  108. void loop() {
  109.  
  110.   // If the flag "doConnect" is true then we have scanned for and found the desired
  111.   // BLE Server with which we wish to connect.  Now we connect to it.  Once we are
  112.   // connected we set the connected flag to be true.
  113.   if (doConnect == true) {
  114.     if (connectToServer(*pServerAddress)) {
  115.       Serial.println("We are now connected to the BLE Server.");
  116.       connected = true;
  117.     } else {
  118.       Serial.println("We have failed to connect to the server; there is nothin more we will do.");
  119.     }
  120.     doConnect = false;
  121.   }
  122.  
  123.   // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  124.   // with the current time since boot.
  125.   if (connected) {
  126.     String newValue = "Time since boot: " + String(millis()/1000);
  127.     Serial.println("Setting new characteristic value to \"" + newValue + "\"");
  128.    
  129.     // Set the characteristic's value to be the array of bytes that is actually a string.
  130.     pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  131.   }
  132.  
  133.   delay(1000); // Delay a second between loops.
  134. } // End of loop
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top