diff options
Diffstat (limited to 'sensor/patchedBLE/examples')
6 files changed, 188 insertions, 37 deletions
diff --git a/sensor/patchedBLE/examples/BLE_client/BLE_client.ino b/sensor/patchedBLE/examples/BLE_client/BLE_client.ino index c0b6163..4c58299 100644 --- a/sensor/patchedBLE/examples/BLE_client/BLE_client.ino +++ b/sensor/patchedBLE/examples/BLE_client/BLE_client.ino @@ -1,19 +1,23 @@ /** * A BLE client example that is rich in capabilities. + * There is a lot new capabilities implemented. + * author unknown + * updated by chegewara */ #include "BLEDevice.h" //#include "BLEScan.h" // The remote service we wish to connect to. -static BLEUUID serviceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59"); +static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b"); // The characteristic of the remote service we are interested in. -static BLEUUID charUUID("0d563a58-196a-48ce-ace2-dfec78acc814"); +static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8"); -static BLEAddress *pServerAddress; static boolean doConnect = false; static boolean connected = false; +static boolean doScan = false; static BLERemoteCharacteristic* pRemoteCharacteristic; +static BLEAdvertisedDevice* myDevice; static void notifyCallback( BLERemoteCharacteristic* pBLERemoteCharacteristic, @@ -24,17 +28,31 @@ static void notifyCallback( Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str()); Serial.print(" of data length "); Serial.println(length); + Serial.print("data: "); + Serial.println((char*)pData); } -bool connectToServer(BLEAddress pAddress) { +class MyClientCallback : public BLEClientCallbacks { + void onConnect(BLEClient* pclient) { + } + + void onDisconnect(BLEClient* pclient) { + connected = false; + Serial.println("onDisconnect"); + } +}; + +bool connectToServer() { Serial.print("Forming a connection to "); - Serial.println(pAddress.toString().c_str()); + Serial.println(myDevice->getAddress().toString().c_str()); BLEClient* pClient = BLEDevice::createClient(); Serial.println(" - Created client"); + pClient->setClientCallbacks(new MyClientCallback()); + // Connect to the remove BLE Server. - pClient->connect(pAddress); + pClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private) Serial.println(" - Connected to server"); // Obtain a reference to the service we are after in the remote BLE server. @@ -42,6 +60,7 @@ bool connectToServer(BLEAddress pAddress) { if (pRemoteService == nullptr) { Serial.print("Failed to find our service UUID: "); Serial.println(serviceUUID.toString().c_str()); + pClient->disconnect(); return false; } Serial.println(" - Found our service"); @@ -52,16 +71,22 @@ bool connectToServer(BLEAddress pAddress) { if (pRemoteCharacteristic == nullptr) { Serial.print("Failed to find our characteristic UUID: "); Serial.println(charUUID.toString().c_str()); + pClient->disconnect(); 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()); + if(pRemoteCharacteristic->canRead()) { + std::string value = pRemoteCharacteristic->readValue(); + Serial.print("The characteristic value was: "); + Serial.println(value.c_str()); + } - pRemoteCharacteristic->registerForNotify(notifyCallback); + if(pRemoteCharacteristic->canNotify()) + pRemoteCharacteristic->registerForNotify(notifyCallback); + + connected = true; } /** * Scan for BLE servers and find the first one that advertises the service we are looking for. @@ -75,14 +100,12 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { 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(); + if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) { - pServerAddress = new BLEAddress(advertisedDevice.getAddress()); + BLEDevice::getScan()->stop(); + myDevice = new BLEAdvertisedDevice(advertisedDevice); doConnect = true; + doScan = true; } // Found our server } // onResult @@ -96,11 +119,13 @@ void setup() { // 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. + // scan to run for 5 seconds. BLEScan* pBLEScan = BLEDevice::getScan(); pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); + pBLEScan->setInterval(1349); + pBLEScan->setWindow(449); pBLEScan->setActiveScan(true); - pBLEScan->start(30); + pBLEScan->start(5, false); } // End of setup. @@ -111,9 +136,8 @@ void loop() { // 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)) { + if (connectToServer()) { 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."); } @@ -128,6 +152,8 @@ void loop() { // Set the characteristic's value to be the array of bytes that is actually a string. pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length()); + }else if(doScan){ + BLEDevice::getScan()->start(0); // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino } delay(1000); // Delay a second between loops. diff --git a/sensor/patchedBLE/examples/BLE_iBeacon/BLE_iBeacon.ino b/sensor/patchedBLE/examples/BLE_iBeacon/BLE_iBeacon.ino index 5f6ed00..e43174d 100644 --- a/sensor/patchedBLE/examples/BLE_iBeacon/BLE_iBeacon.ino +++ b/sensor/patchedBLE/examples/BLE_iBeacon/BLE_iBeacon.ino @@ -18,7 +18,6 @@ #include "sys/time.h" #include "BLEDevice.h" -#include "BLEServer.h" #include "BLEUtils.h" #include "BLEBeacon.h" #include "esp_sleep.h" @@ -85,9 +84,9 @@ void setup() { BLEDevice::init(""); // Create the BLE Server - BLEServer *pServer = BLEDevice::createServer(); + // BLEServer *pServer = BLEDevice::createServer(); // <-- no longer required to instantiate BLEServer, less flash and ram usage - pAdvertising = pServer->getAdvertising(); + pAdvertising = BLEDevice::getAdvertising(); setBeacon(); // Start advertising diff --git a/sensor/patchedBLE/examples/BLE_notify/BLE_notify.ino b/sensor/patchedBLE/examples/BLE_notify/BLE_notify.ino index 5e915be..42b9e72 100644 --- a/sensor/patchedBLE/examples/BLE_notify/BLE_notify.ino +++ b/sensor/patchedBLE/examples/BLE_notify/BLE_notify.ino @@ -2,6 +2,7 @@ Video: https://www.youtube.com/watch?v=oCMOYS71NIU Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp Ported to Arduino ESP32 by Evandro Copercini + updated by chegewara Create a BLE server that, once we receive a connection, will send periodic notifications. The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b @@ -27,7 +28,7 @@ BLEServer* pServer = NULL; BLECharacteristic* pCharacteristic = NULL; bool deviceConnected = false; bool oldDeviceConnected = false; -uint8_t value = 0; +uint32_t value = 0; // See the following for generating UUIDs: // https://www.uuidgenerator.net/ @@ -52,7 +53,7 @@ void setup() { Serial.begin(115200); // Create the BLE Device - BLEDevice::init("MyESP32"); + BLEDevice::init("ESP32"); // Create the BLE Server pServer = BLEDevice::createServer(); @@ -78,17 +79,21 @@ void setup() { pService->start(); // Start advertising - pServer->getAdvertising()->start(); + BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); + pAdvertising->addServiceUUID(SERVICE_UUID); + pAdvertising->setScanResponse(false); + pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter + BLEDevice::startAdvertising(); Serial.println("Waiting a client connection to notify..."); } void loop() { // notify changed value if (deviceConnected) { - pCharacteristic->setValue(&value, 1); + pCharacteristic->setValue((uint8_t*)&value, 4); pCharacteristic->notify(); value++; - delay(10); // bluetooth stack will go into congestion, if too many packets are sent + delay(3); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms } // disconnecting if (!deviceConnected && oldDeviceConnected) { diff --git a/sensor/patchedBLE/examples/BLE_scan/BLE_scan.ino b/sensor/patchedBLE/examples/BLE_scan/BLE_scan.ino index ef7d892..094f793 100644 --- a/sensor/patchedBLE/examples/BLE_scan/BLE_scan.ino +++ b/sensor/patchedBLE/examples/BLE_scan/BLE_scan.ino @@ -8,7 +8,8 @@ #include <BLEScan.h> #include <BLEAdvertisedDevice.h> -int scanTime = 30; //In seconds +int scanTime = 5; //In seconds +BLEScan* pBLEScan; class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { void onResult(BLEAdvertisedDevice advertisedDevice) { @@ -21,16 +22,19 @@ void setup() { Serial.println("Scanning..."); BLEDevice::init(""); - BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan + pBLEScan = BLEDevice::getScan(); //create new scan pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster - BLEScanResults foundDevices = pBLEScan->start(scanTime); - Serial.print("Devices found: "); - Serial.println(foundDevices.getCount()); - Serial.println("Scan done!"); + pBLEScan->setInterval(100); + pBLEScan->setWindow(99); // less or equal setInterval value } void loop() { // put your main code here, to run repeatedly: + BLEScanResults foundDevices = pBLEScan->start(scanTime, false); + Serial.print("Devices found: "); + Serial.println(foundDevices.getCount()); + Serial.println("Scan done!"); + pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory delay(2000); }
\ No newline at end of file diff --git a/sensor/patchedBLE/examples/BLE_server/BLE_server.ino b/sensor/patchedBLE/examples/BLE_server/BLE_server.ino index 38224a6..3f9176a 100644 --- a/sensor/patchedBLE/examples/BLE_server/BLE_server.ino +++ b/sensor/patchedBLE/examples/BLE_server/BLE_server.ino @@ -1,6 +1,7 @@ /* Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp Ported to Arduino ESP32 by Evandro Copercini + updates by chegewara */ #include <BLEDevice.h> @@ -17,7 +18,7 @@ void setup() { Serial.begin(115200); Serial.println("Starting BLE work!"); - BLEDevice::init("MyESP32"); + BLEDevice::init("Long name works now"); BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService(SERVICE_UUID); BLECharacteristic *pCharacteristic = pService->createCharacteristic( @@ -28,8 +29,13 @@ void setup() { pCharacteristic->setValue("Hello World says Neil"); pService->start(); - BLEAdvertising *pAdvertising = pServer->getAdvertising(); - pAdvertising->start(); + // BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility + BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); + pAdvertising->addServiceUUID(SERVICE_UUID); + pAdvertising->setScanResponse(true); + pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue + pAdvertising->setMinPreferred(0x12); + BLEDevice::startAdvertising(); Serial.println("Characteristic defined! Now you can read it in your phone!"); } diff --git a/sensor/patchedBLE/examples/BLE_server_multiconnect/BLE_server_multiconnect.ino b/sensor/patchedBLE/examples/BLE_server_multiconnect/BLE_server_multiconnect.ino new file mode 100644 index 0000000..90704ef --- /dev/null +++ b/sensor/patchedBLE/examples/BLE_server_multiconnect/BLE_server_multiconnect.ino @@ -0,0 +1,111 @@ +/* + Video: https://www.youtube.com/watch?v=oCMOYS71NIU + Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp + Ported to Arduino ESP32 by Evandro Copercini + updated by chegewara + + Create a BLE server that, once we receive a connection, will send periodic notifications. + The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b + And has a characteristic of: beb5483e-36e1-4688-b7f5-ea07361b26a8 + + The design of creating the BLE server is: + 1. Create a BLE Server + 2. Create a BLE Service + 3. Create a BLE Characteristic on the Service + 4. Create a BLE Descriptor on the characteristic + 5. Start the service. + 6. Start advertising. + + A connect hander associated with the server starts a background task that performs notification + every couple of seconds. +*/ +#include <BLEDevice.h> +#include <BLEServer.h> +#include <BLEUtils.h> +#include <BLE2902.h> + +BLEServer* pServer = NULL; +BLECharacteristic* pCharacteristic = NULL; +bool deviceConnected = false; +bool oldDeviceConnected = false; +uint32_t value = 0; + +// See the following for generating UUIDs: +// https://www.uuidgenerator.net/ + +#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" +#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" + + +class MyServerCallbacks: public BLEServerCallbacks { + void onConnect(BLEServer* pServer) { + deviceConnected = true; + BLEDevice::startAdvertising(); + }; + + void onDisconnect(BLEServer* pServer) { + deviceConnected = false; + } +}; + + + +void setup() { + Serial.begin(115200); + + // Create the BLE Device + BLEDevice::init("ESP32"); + + // Create the BLE Server + pServer = BLEDevice::createServer(); + pServer->setCallbacks(new MyServerCallbacks()); + + // Create the BLE Service + BLEService *pService = pServer->createService(SERVICE_UUID); + + // Create a BLE Characteristic + pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID, + BLECharacteristic::PROPERTY_READ | + BLECharacteristic::PROPERTY_WRITE | + BLECharacteristic::PROPERTY_NOTIFY | + BLECharacteristic::PROPERTY_INDICATE + ); + + // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml + // Create a BLE Descriptor + pCharacteristic->addDescriptor(new BLE2902()); + + // Start the service + pService->start(); + + // Start advertising + BLEAdvertising *pAdvertising = BLEDevice::getAdvertising(); + pAdvertising->addServiceUUID(SERVICE_UUID); + pAdvertising->setScanResponse(false); + pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter + BLEDevice::startAdvertising(); + Serial.println("Waiting a client connection to notify..."); +} + +void loop() { + // notify changed value + if (deviceConnected) { + pCharacteristic->setValue((uint8_t*)&value, 4); + pCharacteristic->notify(); + value++; + delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms + } + // disconnecting + if (!deviceConnected && oldDeviceConnected) { + delay(500); // give the bluetooth stack the chance to get things ready + pServer->startAdvertising(); // restart advertising + Serial.println("start advertising"); + oldDeviceConnected = deviceConnected; + } + // connecting + if (deviceConnected && !oldDeviceConnected) { + // do stuff here on connecting + oldDeviceConnected = deviceConnected; + } +} |