我正在使用BLE连接到传感器,但是如果传感器电池已经失效,则需要很长时间(30秒)才能超时。有什么方法可以指定超时吗?
BLEClient* client = BLEDevice::createClient();
bool connected = client->connect(bleAddress);发布于 2022-02-02 17:52:26
我无法理解这个API实际上是阻塞和修复的事实,这需要对FreeRTOS代码进行更改。
简短的回答:不,不可能在不做丑陋的改变的情况下设置超时。
但是如果超时值有意义的话,代码将是一个很好的近似。短时间超时(小于10秒)可能需要一个信号量。
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;
}https://stackoverflow.com/questions/60030996
复制相似问题