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"
- //#include "BLEScan.h"
- uint8_t BLEtimeout =0;
- uint8_t txData[20];
- // The remote service we wish to connect to.
- static BLEUUID serviceUUID("0783b03e-8535-b5a0-7140-a304d2495cb7");
- // The characteristic of the remote service we are interested in.
- static BLEUUID charUUID("0783b03e-8535-b5a0-7140-a304d2495cba");
- static BLEUUID rxUUID("0783b03e-8535-b5a0-7140-a304d2495cb8");
- static BLEAddress *pServerAddress;
- BLERemoteService* pRemoteService;
- BLEClient* pClient = BLEDevice::createClient();
- static boolean doConnect = false;
- static boolean connected = false;
- bool Scanning = 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);
- Serial.print("Data: ");
- Serial.print(pData[0]);
- Serial.print(pData[1]);
- Serial.print(pData[2]);
- Serial.print(pData[3]);
- Serial.print(pData[4]);
- Serial.print(pData[5]);
- Serial.print(pData[6]);
- Serial.print(pData[7]);
- Serial.print(pData[8]);
- Serial.print(pData[9]);
- Serial.println(pData[10]);
- BLEtimeout = 0;
- }
- bool connectToServer(BLEAddress pAddress) {
- Serial.print("Forming a connection to ");
- Serial.println(pAddress.toString().c_str());
- 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.
- 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");
- pRemoteCharacteristic = pRemoteService->getCharacteristic(rxUUID);
- if (pRemoteCharacteristic == nullptr) {
- Serial.print("Failed to find RX UUID: ");
- Serial.println(rxUUID.toString().c_str());
- return false;
- }
- // Serial.println(" - Found RX characteristic");
- // Read the value of the characteristic.
- //std::string value1 = pRemoteCharacteristic->readValue();
- // Serial.print("The characteristic value was: ");
- // Serial.println(value1.c_str());
- pRemoteCharacteristic->registerForNotify(notifyCallback);
- //pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
- // 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");
- //std::string value = pRemoteCharacteristic->readValue();
- //Serial.print("The characteristic value was: ");
- //Serial.println(value.c_str());
- }
- /**
- * 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.
- Scanning = true;
- 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;
- // Scanning = false;
- }
- else {
- Serial.println("We have failed to connect to the server; there is nothin more we will do.");
- }
- doConnect = false;
- //Scanning = true;
- }
- // 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 + "\"");
- Scanning = false;
- // Set the characteristic's value to be the array of bytes that is actually a string.
- //pRemoteCharacteristic->writeValue(txData,20);
- //pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
- pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
- //std::string value3 = pRemoteCharacteristic->readValue();
- // Serial.print("Data:");
- //Serial.println(value3.c_str());
- }
- if (BLEtimeout >= 5) {
- connected = false;
- doConnect = false;
- pClient->disconnect();
- //Scanning = true;
- BLEtimeout = 6;
- }
- if(connected == false && doConnect == false){
- Scanning = true;
- BLEScan* pBLEScan = BLEDevice::getScan();
- pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
- pBLEScan->setActiveScan(true);
- pBLEScan->start(30);
- BLEtimeout = 0;
- }
- if (Scanning == false){
- BLEtimeout++;
- }
- Serial.print("connected: ");
- Serial.println(connected);
- Serial.print("doConnect: ");
- Serial.println(doConnect);
- Serial.print("Scanning: ");
- Serial.println(Scanning);
- Serial.print("BLETO: ");
- Serial.println(BLEtimeout);
- delay(1000); // Delay a second between loops.
- } // End of loop
- **********************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement