Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * A BLE client example that is rich in capabilities.
- */
- #include "BLEDevice.h"
- // The remote service we wish to connect to.
- static BLEUUID serviceUUID("00001204-0000-1000-8000-00805f9b34fb");
- // The characteristic of the remote service we are interested in.
- static BLEUUID charUUID("00001a00-0000-1000-8000-00805f9b34fb");
- static BLEUUID charUUID2("00001a01-0000-1000-8000-00805f9b34fb");
- static BLEUUID charUUID3("00001a02-0000-1000-8000-00805f9b34fb");
- static BLEAddress *pServerAddress;
- static BLERemoteCharacteristic* pRemoteCharacteristic, *pRC;
- static boolean doConnect = false;
- static boolean connected = false;
- BLERemoteService* pRemoteService;
- BLEClient* pClient;
- 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(millis());
- Serial.print(" - Forming a connection to ");
- Serial.println(pAddress.toString().c_str());
- pClient = BLEDevice::createClient();
- Serial.print(millis());
- Serial.println(" - Created client");
- // Connect to the remote BLE Server.
- pClient->connect(pAddress);
- Serial.print(millis());
- Serial.println(" - Connected to server");
- // Obtain a reference to the service we are after in the remote BLE server.
- pRemoteService = pClient->getService(serviceUUID);
- if (pRemoteService == nullptr) {
- Serial.print("Failed to find our service UUID: ");
- Serial.println(serviceUUID.toString().c_str());
- for(;;);
- }
- Serial.print(millis());
- Serial.println(" - Found our service");
- // Obtain a reference to the control 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());
- for(;;);
- }
- Serial.print(millis());
- Serial.println(" - Setting control characteristic value to 0xA01F");
- byte newValue[] = {0xA0, 0x1F};
- // Set the characteristic's value to be the array of bytes.
- pRemoteCharacteristic->writeValue(newValue, 2); // Enable real-time reporting
- // Read the value of the sensor characteristic.
- std::string value = pRemoteCharacteristic->readValue();
- delay(100);
- Serial.print(millis());
- Serial.print(" - The characteristic value was: ");
- for (int i=0; i<value.length(); i++) {
- Serial.print((byte)value[i]);
- Serial.print(" ");
- }
- Serial.println();
- return true;
- }
- /**
- * Scan for BLE servers and find the first one named "Flower care"
- */
- class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
- /**
- * Called for each advertising BLE server.
- */
- void onResult(BLEAdvertisedDevice advertisedDevice) {
- Serial.print(millis());
- 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.getName() == "Flower care") {
- Serial.print(millis());
- 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.print(millis());
- 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.print(millis());
- 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 to enable real-time reads
- if (connected) {
- // Obtain a reference to the sensor characteristic in the service of the remote BLE server.
- pRC = pRemoteService->getCharacteristic(charUUID2);
- if (pRC == nullptr) {
- Serial.print("Failed to find our characteristic UUID: ");
- Serial.println(charUUID2.toString().c_str());
- for(;;);
- }
- Serial.print(millis());
- Serial.println(" - Found sensor characteristic");
- // Read the value of the sensor characteristic.
- std::string value = pRC->readValue();
- delay(100);
- Serial.print(millis());
- Serial.print(" - The characteristic value was: ");
- for (int i=0; i<value.length(); i++) {
- Serial.print((byte)value[i]);
- Serial.print(" ");
- }
- Serial.println();
- // Find the battery characteristic
- pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID3);
- if (pRemoteCharacteristic == nullptr) {
- Serial.print("Failed to find our characteristic UUID: ");
- Serial.println(charUUID3.toString().c_str());
- for(;;);
- }
- delay(100);
- Serial.print(millis());
- Serial.println(" - Found our battery/firmware characteristic");
- // Read the value of the characteristic.
- value = pRemoteCharacteristic->readValue();
- Serial.print(millis());
- Serial.print(" - The characteristic value was: ");
- Serial.print("Battery ");
- Serial.print((byte)value[0]);
- Serial.print("% Firmware ");
- Serial.print(String(value.substr(2,5).c_str()));
- Serial.println();
- for(;;)
- delay(1000);
- }
- delay(1000); // Delay a second between loops.
- } // End of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement