-
In my current project I would like to establish a connection to a device via the onResult callback of a NimBLEScan as soon as the device I am looking for is detected (asynchronous). Unfortunately, this does not seem to be possible - the connection is not established. If I connect from outside the callback (by calling a function in the main loop like Do you see a way to avoid this extra call? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
On an advanced level yes this is possible (direct calls to the NimBLE stack), however, from this library API it is not, due to task blocking and other things. There are other ways though, such as having the result callback unblock a task that then performs the connection, which is my method currently. |
Beta Was this translation helpful? Give feedback.
-
This would be exceptionally difficult to make work with the rest of the library, you would need to write your own client code and callback handling etc.. I certainly would not suggest it, just being thorough with my answer since it is possible.
If you're familiar with FreeRTOS queues you could just have the connection handler task block on a queue receive call and push the device pointer into the queue from the scan callback (some priority management and task stack sizing my be required here) but that's the gist of it. There are other ways as well but this method is straight forward. I don't have any basic snippets at the ready right now but I'll see if I can give an example tomorrow. |
Beta Was this translation helpful? Give feedback.
-
I think I have found a suitable solution with void MyClass::begin() {
...
xTaskCreate(connect_task, "connect", 2048, this, tskIDLE_PRIORITY, nullptr);
}
void MyClass::connect_task(void* vParams) {
MyClass* instance = (MyClass*)vParams;
for (;;) {
if (instance->found_connectable_device()) {
NimBLEAdvertisedDevice* device = instance->get_connectable_device();
instance->connect_device(device);
}
vTaskDelay(1); // feed the watchdog
}
} |
Beta Was this translation helpful? Give feedback.
This would be exceptionally difficult to make work with the rest of the library, you would need to write your own client code and callback handling etc.. I certainly would not suggest it, just being thorough with my answer since it is possible.
If you're familiar with FreeRTOS queues you could just have the connection handler task block on a queue receive call and push the device pointer into the queu…