mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-23 13:40:55 +01:00
2151386057
* General code cleanup * `NimBLEScan::start` will no longer clear cache or results if scanning is already in progress. * `NimBLEScan::clearResults` will now reset the vector capacity to 0. * `NimBLEScan::stop` will no longer call the `onScanEnd` callback as the caller should know its been stopped when this is called. * `NimBLEScan::clearDuplicateCache` has been removed as it was problematic and only for the esp32. Stop and start the scanner for the same effect. * `NimBLEScan::start` takes a new bool parameter `restart`, default `true`, that will restart an already in progress scan and clear the duplicate filter so all devices will be discovered again. * Scan response data that is received without advertisement first will now create the device and send a callback. * Added new method: `NimBLEAdvertisedDevice::isScannable()` that returns true if the device is scannable. * Added default callbacks for `NimBLEScanCallbacks` * `NimBLEScanCallbacks` function signatures updated: * - `onDiscovered` now takes a `const NimBLEAdvertisedDevice*` * - `onResult` now takes a `const NimBLEAdvertisedDevice*` * - `onScanEnd` now takes a `const NimBLEScanResults&` and `int reason` * Added new erase overload: `NimBLEScan::erase(const NimBLEAdvertisedDevice* device)` * `NimBLEScanResults::getDevice` methods now return `const NimBLEAdvertisedDevice*` * `NimBLEScanResults` iterators are now `const_iterator`
83 lines
2.5 KiB
C++
83 lines
2.5 KiB
C++
|
|
/**
|
|
* NimBLE_Async_client Demo:
|
|
*
|
|
* Demonstrates asynchronous client operations.
|
|
*
|
|
* Created: on November 4, 2024
|
|
* Author: H2zero
|
|
*
|
|
*/
|
|
|
|
#include <NimBLEDevice.h>
|
|
|
|
static constexpr uint32_t scanTimeMs = 5 * 1000;
|
|
|
|
class ClientCallbacks : public NimBLEClientCallbacks {
|
|
void onConnect(NimBLEClient* pClient) {
|
|
printf("Connected to: %s\n", pClient->getPeerAddress().toString().c_str());
|
|
}
|
|
|
|
void onDisconnect(NimBLEClient* pClient, int reason) {
|
|
printf("%s Disconnected, reason = %d - Starting scan\n", pClient->getPeerAddress().toString().c_str(), reason);
|
|
NimBLEDevice::getScan()->start(scanTimeMs);
|
|
}
|
|
} clientCB;
|
|
|
|
class scanCallbacks : public NimBLEScanCallbacks {
|
|
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
|
|
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
|
|
if (advertisedDevice->haveName() && advertisedDevice->getName() == "NimBLE-Server") {
|
|
printf("Found Our Device\n");
|
|
|
|
auto pClient = NimBLEDevice::getDisconnectedClient();
|
|
if (!pClient) {
|
|
pClient = NimBLEDevice::createClient(advertisedDevice->getAddress());
|
|
if (!pClient) {
|
|
printf("Failed to create client\n");
|
|
return;
|
|
}
|
|
}
|
|
|
|
pClient->setClientCallbacks(&clientCB, false);
|
|
if (!pClient->connect(true, true, false)) { // delete attributes, async connect, no MTU exchange
|
|
NimBLEDevice::deleteClient(pClient);
|
|
printf("Failed to connect\n");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void onScanEnd(NimBLEScanResults results) {
|
|
printf("Scan Ended\n");
|
|
NimBLEDevice::getScan()->start(scanTimeMs);
|
|
}
|
|
};
|
|
|
|
extern "C" void app_main(void) {
|
|
printf("Starting NimBLE Async Client\n");
|
|
NimBLEDevice::init("");
|
|
NimBLEDevice::setPower(3); /** +3db */
|
|
|
|
NimBLEScan* pScan = NimBLEDevice::getScan();
|
|
pScan->setScanCallbacks(new scanCallbacks());
|
|
pScan->setInterval(45);
|
|
pScan->setWindow(15);
|
|
pScan->setActiveScan(true);
|
|
pScan->start(scanTimeMs);
|
|
|
|
for (;;) {
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
auto pClients = NimBLEDevice::getConnectedClients();
|
|
if (!pClients.size()) {
|
|
continue;
|
|
}
|
|
|
|
for (auto& pClient : pClients) {
|
|
printf("%s\n", pClient->toString().c_str());
|
|
NimBLEDevice::deleteClient(pClient);
|
|
}
|
|
|
|
NimBLEDevice::getScan()->start(scanTimeMs);
|
|
}
|
|
}
|