'BLE Connect timeout?
I'm using BLE to connect to a sensor, but if the sensors battery has died it takes ages(30 seconds) for it to timeout. Is there some way to specify a timeout?
BLEClient* client = BLEDevice::createClient();
bool connected = client->connect(bleAddress);
Solution 1:[1]
I cannot wrap my head around the fact that this API is actually blocking and fixing this requires changes to FreeRTOS code.
Short answer: no, not possible to set a timeout without making ugly changes.
But the code would be a good approximation if the timeout value is meaningful. Short timeouts (less than 10 secs) will likely require a semaphore.
bool doConnect(uint8_t timeout_secs) {
static bool connected = false;
TaskHandle_t hTask;
xTaskCreate(
[](void *unused)
{
pClient->connect(&device);
connected = true;
vTaskDelete(nullptr); // important line, FreeRTOS will crash.
},
"connect", //name of the task for debugging purposes.
8192, // stack size
nullptr, // object passed as void* unused
2, // priority
&hTask);
uint64_t expiration = millis() + timeout_secs * 1000;
while (!connected && millis() < expiration) {
delay(500); // give the connect task a chance to run.
}
vTaskDelete(hTask);
return connected;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paperino |
