This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to deal with this race conditon

I'm using nRF52832, soft device 2.0, SDK11 and as a central.

When it happens I'm scanning for peripheral to connect. Usually it's a peripheral advertises at very low rate, ie only a few times per second and with very weak RSSI, ie around -90dbm.

My central device will try to connect to it. Then I have an arbitary timeout because I don't want to wait for too long for really weak signal. When time is up and I haven't received BLE_GAP_EVT_CONNECTED, I'll call sd_ble_gap_connect_cancel() to cancel the connection.

But some time I get NRF_ERROR_INVALID_STATE from the call. If I ignore the error and return to scanning, I'll get soft device fault. What's the reason for the error code? I guess one possibility is it gets connected right when I want to cancel it? How to deal with it?

Parents
  • just set the scan timeout to whatever time you want to wait for connection. You'll either get a connection callback or you'll get a connection/scan timeout callback. Then you can retry or not depending on what it is you're trying to do. If you always wait for one or other callback, you know you are in a good state all the time, no race condition.

Reply
  • just set the scan timeout to whatever time you want to wait for connection. You'll either get a connection callback or you'll get a connection/scan timeout callback. Then you can retry or not depending on what it is you're trying to do. If you always wait for one or other callback, you know you are in a good state all the time, no race condition.

Children
Related