2024-10-29 20:50:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2024-12-09 20:58:14 +01:00
|
|
|
void onConnect(NimBLEClient* pClient) override {
|
2024-10-29 20:50:12 +01:00
|
|
|
printf("Connected to: %s\n", pClient->getPeerAddress().toString().c_str());
|
|
|
|
}
|
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
void onDisconnect(NimBLEClient* pClient, int reason) override {
|
2024-10-29 20:50:12 +01:00
|
|
|
printf("%s Disconnected, reason = %d - Starting scan\n", pClient->getPeerAddress().toString().c_str(), reason);
|
|
|
|
NimBLEDevice::getScan()->start(scanTimeMs);
|
|
|
|
}
|
2024-12-09 20:58:14 +01:00
|
|
|
} clientCallbacks;
|
2024-10-29 20:50:12 +01:00
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
class ScanCallbacks : public NimBLEScanCallbacks {
|
|
|
|
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override {
|
2024-10-29 20:50:12 +01:00
|
|
|
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
|
|
|
|
if (advertisedDevice->haveName() && advertisedDevice->getName() == "NimBLE-Server") {
|
|
|
|
printf("Found Our Device\n");
|
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
/** Async connections can be made directly in the scan callbacks */
|
2024-10-29 20:50:12 +01:00
|
|
|
auto pClient = NimBLEDevice::getDisconnectedClient();
|
|
|
|
if (!pClient) {
|
|
|
|
pClient = NimBLEDevice::createClient(advertisedDevice->getAddress());
|
|
|
|
if (!pClient) {
|
|
|
|
printf("Failed to create client\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
pClient->setClientCallbacks(&clientCallbacks, false);
|
2024-10-29 20:50:12 +01:00
|
|
|
if (!pClient->connect(true, true, false)) { // delete attributes, async connect, no MTU exchange
|
|
|
|
NimBLEDevice::deleteClient(pClient);
|
|
|
|
printf("Failed to connect\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
void onScanEnd(const NimBLEScanResults& results, int reason) override {
|
2024-10-29 20:50:12 +01:00
|
|
|
printf("Scan Ended\n");
|
|
|
|
NimBLEDevice::getScan()->start(scanTimeMs);
|
|
|
|
}
|
2024-12-09 20:58:14 +01:00
|
|
|
} scanCallbacks;
|
2024-10-29 20:50:12 +01:00
|
|
|
|
|
|
|
extern "C" void app_main(void) {
|
|
|
|
printf("Starting NimBLE Async Client\n");
|
2024-12-09 20:58:14 +01:00
|
|
|
NimBLEDevice::init("Async-Client");
|
2024-10-29 20:50:12 +01:00
|
|
|
NimBLEDevice::setPower(3); /** +3db */
|
|
|
|
|
|
|
|
NimBLEScan* pScan = NimBLEDevice::getScan();
|
2024-12-09 20:58:14 +01:00
|
|
|
pScan->setScanCallbacks(&scanCallbacks);
|
2024-10-29 20:50:12 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|