Greetings,
Although multi-NUS is not officially a sample in the nRF Connect SDK, it is closely related to nrf/samples/bluetooth/central_uart, and I thought it would be helpful if I post some bugs. Then a NS person could theoretically fix the bugs in the git repo.
The first bug carries over from an older version of central_uart. Please ignore my line numbers in what I present to you from git diff, but it should be easy to find the code.
@@ -637,8 +650,9 @@ static void connected(struct bt_conn *conn, uint8_t conn_err
)
gatt_discover(conn);
/*Stop scanning during the discovery*/
err = bt_scan_stop();
- if ((!err) && (err != -EALREADY)) {
+ if ((err) && (err != -EALREADY)) {
LOG_ERR("Stop LE scan failed (err %d)", err);
}
}
That one was pretty simple. The second one is bad because it can effectively shut down the whole system in the sense that scanning stops:
@@ -662,11 +676,11 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
bt_conn_unref(conn);
default_conn = NULL;
- // err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
- // if (err) {
- // LOG_ERR("Scanning failed to start (err %d)",
- // err);
- // }
+ err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
+ if (err && err != -EALREADY) {
+ LOG_ERR("Scanning failed to start (err %d)",
+ err);
+ }
}
static void security_changed(struct bt_conn *conn, bt_security_t level,
So this problem will occur frequently if I push and release the reset button quickly on a connected peripheral. In that case the peripheral will often try to connect before it has been disconnected. I need to show my log because the problem happens in several steps and is not obvious without the log
00:00:20.739,746] <inf> central_uart: Filters matched. Address: E8:37:D4:53:1F:AC (random) connectable: 1
scan_connect_with_target() about to bt_scan_stop()
[00:00:20.744,659] <wrn> bt_conn: Found valid connection (0x2001bbd0) with address E8:37:D4:53:1F:AC (random) in connected state
[00:00:20.744,689] <wrn> central_uart: Connecting failed
[00:00:23.767,547] <inf> central_uart: Disconnected: E8:37:D4:53:1F:AC (random) (reason 8)
First you see the filters match. This will lead to the scan.c function I highlighted in orange being called and stopping the scan. Since the peripheral connecting in is already connected, bt_conn() sees that and leads to Connecting failed, and hence, no discovery_complete() to call bt_scan_start(). There is a call to disconnected, finally, but since NordicMatt had commented out the bt_scan_start() there (the original central_uart is coded slightly differently but effectively had a bt_scan_start() call), scanning is dead for good.
Reverting the code to start the scan will sometimes try to start it when it is already started, and an EGAIN error will result. So I modified the error handling to not print in that harmless case.
Burt