summaryrefslogtreecommitdiff
path: root/sensor/patchedBLE/examples/BLE_client/BLE_client.ino
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2018-08-03 12:23:17 +0100
committerdakkar <dakkar@thenautilus.net>2018-08-03 12:23:17 +0100
commitbb00c2da73b6ab8837f50c9f889ee151f8abc036 (patch)
tree3922eb826386260568ccaa1da7056c141f752a97 /sensor/patchedBLE/examples/BLE_client/BLE_client.ino
parentdon't print from callback, there's races (diff)
parentUpload of 0.4.16 (diff)
downloadthermostat-bb00c2da73b6ab8837f50c9f889ee151f8abc036.tar.gz
thermostat-bb00c2da73b6ab8837f50c9f889ee151f8abc036.tar.bz2
thermostat-bb00c2da73b6ab8837f50c9f889ee151f8abc036.zip
Add 'sensor/patchedBLE/' from commit '7951347ed68313d75c367e1f2cce763cb56d1eb2'
git-subtree-dir: sensor/patchedBLE git-subtree-mainline: 27e209cf58abeda1dc110777f99a98804a13fdbf git-subtree-split: 7951347ed68313d75c367e1f2cce763cb56d1eb2
Diffstat (limited to 'sensor/patchedBLE/examples/BLE_client/BLE_client.ino')
-rw-r--r--sensor/patchedBLE/examples/BLE_client/BLE_client.ino134
1 files changed, 134 insertions, 0 deletions
diff --git a/sensor/patchedBLE/examples/BLE_client/BLE_client.ino b/sensor/patchedBLE/examples/BLE_client/BLE_client.ino
new file mode 100644
index 0000000..c0b6163
--- /dev/null
+++ b/sensor/patchedBLE/examples/BLE_client/BLE_client.ino
@@ -0,0 +1,134 @@
+/**
+ * A BLE client example that is rich in capabilities.
+ */
+
+#include "BLEDevice.h"
+//#include "BLEScan.h"
+
+// The remote service we wish to connect to.
+static BLEUUID serviceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");
+// The characteristic of the remote service we are interested in.
+static BLEUUID charUUID("0d563a58-196a-48ce-ace2-dfec78acc814");
+
+static BLEAddress *pServerAddress;
+static boolean doConnect = false;
+static boolean connected = false;
+static BLERemoteCharacteristic* pRemoteCharacteristic;
+
+static void notifyCallback(
+ BLERemoteCharacteristic* pBLERemoteCharacteristic,
+ uint8_t* pData,
+ size_t length,
+ bool isNotify) {
+ Serial.print("Notify callback for characteristic ");
+ Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
+ Serial.print(" of data length ");
+ Serial.println(length);
+}
+
+bool connectToServer(BLEAddress pAddress) {
+ Serial.print("Forming a connection to ");
+ Serial.println(pAddress.toString().c_str());
+
+ BLEClient* pClient = BLEDevice::createClient();
+ Serial.println(" - Created client");
+
+ // Connect to the remove BLE Server.
+ pClient->connect(pAddress);
+ Serial.println(" - Connected to server");
+
+ // Obtain a reference to the service we are after in the remote BLE server.
+ BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
+ if (pRemoteService == nullptr) {
+ Serial.print("Failed to find our service UUID: ");
+ Serial.println(serviceUUID.toString().c_str());
+ return false;
+ }
+ Serial.println(" - Found our service");
+
+
+ // Obtain a reference to the characteristic in the service of the remote BLE server.
+ pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
+ if (pRemoteCharacteristic == nullptr) {
+ Serial.print("Failed to find our characteristic UUID: ");
+ Serial.println(charUUID.toString().c_str());
+ return false;
+ }
+ Serial.println(" - Found our characteristic");
+
+ // Read the value of the characteristic.
+ std::string value = pRemoteCharacteristic->readValue();
+ Serial.print("The characteristic value was: ");
+ Serial.println(value.c_str());
+
+ pRemoteCharacteristic->registerForNotify(notifyCallback);
+}
+/**
+ * Scan for BLE servers and find the first one that advertises the service we are looking for.
+ */
+class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
+ /**
+ * Called for each advertising BLE server.
+ */
+ void onResult(BLEAdvertisedDevice advertisedDevice) {
+ Serial.print("BLE Advertised Device found: ");
+ Serial.println(advertisedDevice.toString().c_str());
+
+ // We have found a device, let us now see if it contains the service we are looking for.
+ if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(serviceUUID)) {
+
+ //
+ Serial.print("Found our device! address: ");
+ advertisedDevice.getScan()->stop();
+
+ pServerAddress = new BLEAddress(advertisedDevice.getAddress());
+ doConnect = true;
+
+ } // Found our server
+ } // onResult
+}; // MyAdvertisedDeviceCallbacks
+
+
+void setup() {
+ Serial.begin(115200);
+ Serial.println("Starting Arduino BLE Client application...");
+ BLEDevice::init("");
+
+ // Retrieve a Scanner and set the callback we want to use to be informed when we
+ // have detected a new device. Specify that we want active scanning and start the
+ // scan to run for 30 seconds.
+ BLEScan* pBLEScan = BLEDevice::getScan();
+ pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
+ pBLEScan->setActiveScan(true);
+ pBLEScan->start(30);
+} // End of setup.
+
+
+// This is the Arduino main loop function.
+void loop() {
+
+ // If the flag "doConnect" is true then we have scanned for and found the desired
+ // BLE Server with which we wish to connect. Now we connect to it. Once we are
+ // connected we set the connected flag to be true.
+ if (doConnect == true) {
+ if (connectToServer(*pServerAddress)) {
+ Serial.println("We are now connected to the BLE Server.");
+ connected = true;
+ } else {
+ Serial.println("We have failed to connect to the server; there is nothin more we will do.");
+ }
+ doConnect = false;
+ }
+
+ // If we are connected to a peer BLE Server, update the characteristic each time we are reached
+ // with the current time since boot.
+ if (connected) {
+ String newValue = "Time since boot: " + String(millis()/1000);
+ Serial.println("Setting new characteristic value to \"" + newValue + "\"");
+
+ // Set the characteristic's value to be the array of bytes that is actually a string.
+ pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
+ }
+
+ delay(1000); // Delay a second between loops.
+} // End of loop