Skip to content

Commit 57ac60e

Browse files
maje-embpdunaj
authored andcommitted
samples: central_uart: Add connection to bonded devices based on address
Change adds reconnect to bonded devices using their address. The address of bonded devices is added to the scan filters. Ref: NCSDK-30193 Signed-off-by: Marcin Jelinski <[email protected]>
1 parent e065a2a commit 57ac60e

File tree

3 files changed

+77
-32
lines changed

3 files changed

+77
-32
lines changed

doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ Bluetooth samples
280280

281281
* Added loading of radio trims and a fix of a hardware errata for the nRF54H20 SoC to improve the RF performance.
282282

283+
* :ref:`central_uart` sample:
284+
285+
* Added reconnection to bonded devices based on their address.
286+
283287
Bluetooth Fast Pair samples
284288
---------------------------
285289

samples/bluetooth/central_uart/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CONFIG_BT_NUS_CLIENT=y
2222
CONFIG_BT_SCAN=y
2323
CONFIG_BT_SCAN_FILTER_ENABLE=y
2424
CONFIG_BT_SCAN_UUID_CNT=1
25+
CONFIG_BT_SCAN_ADDRESS_CNT=1
2526
CONFIG_BT_GATT_DM=y
2627
CONFIG_HEAP_MEM_POOL_SIZE=2048
2728

samples/bluetooth/central_uart/src/main.c

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
4747

4848
static const struct device *uart = DEVICE_DT_GET(DT_CHOSEN(nordic_nus_uart));
4949
static struct k_work_delayable uart_work;
50+
static struct k_work scan_work;
5051

5152
K_SEM_DEFINE(nus_write_sem, 0, 1);
5253

@@ -361,11 +362,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
361362
bt_conn_unref(default_conn);
362363
default_conn = NULL;
363364

364-
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
365-
if (err) {
366-
LOG_ERR("Scanning failed to start (err %d)",
367-
err);
368-
}
365+
(void)k_work_submit(&scan_work);
369366
}
370367

371368
return;
@@ -397,7 +394,6 @@ static void connected(struct bt_conn *conn, uint8_t conn_err)
397394
static void disconnected(struct bt_conn *conn, uint8_t reason)
398395
{
399396
char addr[BT_ADDR_LE_STR_LEN];
400-
int err;
401397

402398
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
403399

@@ -410,11 +406,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
410406
bt_conn_unref(default_conn);
411407
default_conn = NULL;
412408

413-
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
414-
if (err) {
415-
LOG_ERR("Scanning failed to start (err %d)",
416-
err);
417-
}
409+
(void)k_work_submit(&scan_work);
418410
}
419411

420412
static void security_changed(struct bt_conn *conn, bt_security_t level,
@@ -486,32 +478,88 @@ static int nus_client_init(void)
486478
BT_SCAN_CB_INIT(scan_cb, scan_filter_match, NULL,
487479
scan_connecting_error, scan_connecting);
488480

489-
static int scan_init(void)
481+
static void try_add_address_filter(const struct bt_bond_info *info, void *user_data)
490482
{
491483
int err;
492-
struct bt_scan_init_param scan_init = {
493-
.connect_if_match = 1,
494-
};
484+
char addr[BT_ADDR_LE_STR_LEN];
485+
uint8_t *filter_mode = user_data;
495486

496-
bt_scan_init(&scan_init);
497-
bt_scan_cb_register(&scan_cb);
487+
bt_addr_le_to_str(&info->addr, addr, sizeof(addr));
488+
489+
struct bt_conn *conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &info->addr);
490+
491+
if (conn) {
492+
bt_conn_unref(conn);
493+
return;
494+
}
495+
496+
err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_ADDR, &info->addr);
497+
if (err) {
498+
LOG_ERR("Address filter cannot be added (err %d): %s", err, addr);
499+
return;
500+
}
501+
502+
LOG_INF("Address filter added: %s", addr);
503+
*filter_mode |= BT_SCAN_ADDR_FILTER;
504+
}
505+
506+
static int scan_start(void)
507+
{
508+
int err;
509+
uint8_t filter_mode = 0;
510+
511+
err = bt_scan_stop();
512+
if (err) {
513+
LOG_ERR("Failed to stop scanning (err %d)", err);
514+
return err;
515+
}
516+
517+
bt_scan_filter_remove_all();
498518

499519
err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_NUS_SERVICE);
500520
if (err) {
501-
LOG_ERR("Scanning filters cannot be set (err %d)", err);
521+
LOG_ERR("UUID filter cannot be added (err %d", err);
502522
return err;
503523
}
524+
filter_mode |= BT_SCAN_UUID_FILTER;
525+
526+
bt_foreach_bond(BT_ID_DEFAULT, try_add_address_filter, &filter_mode);
504527

505-
err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER, false);
528+
err = bt_scan_filter_enable(filter_mode, false);
506529
if (err) {
507530
LOG_ERR("Filters cannot be turned on (err %d)", err);
508531
return err;
509532
}
510533

511-
LOG_INF("Scan module initialized");
512-
return err;
534+
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
535+
if (err) {
536+
LOG_ERR("Scanning failed to start (err %d)", err);
537+
return err;
538+
}
539+
540+
LOG_INF("Scan started");
541+
return 0;
513542
}
514543

544+
static void scan_work_handler(struct k_work *item)
545+
{
546+
ARG_UNUSED(item);
547+
548+
(void)scan_start();
549+
}
550+
551+
static void scan_init(void)
552+
{
553+
struct bt_scan_init_param scan_init = {
554+
.connect_if_match = true,
555+
};
556+
557+
bt_scan_init(&scan_init);
558+
bt_scan_cb_register(&scan_cb);
559+
560+
k_work_init(&scan_work, scan_work_handler);
561+
LOG_INF("Scan module initialized");
562+
}
515563

516564
static void auth_cancel(struct bt_conn *conn)
517565
{
@@ -585,27 +633,19 @@ int main(void)
585633
return 0;
586634
}
587635

588-
err = scan_init();
589-
if (err != 0) {
590-
LOG_ERR("scan_init failed (err %d)", err);
591-
return 0;
592-
}
593-
594636
err = nus_client_init();
595637
if (err != 0) {
596638
LOG_ERR("nus_client_init failed (err %d)", err);
597639
return 0;
598640
}
599641

600-
printk("Starting Bluetooth Central UART sample\n");
601-
602-
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
642+
scan_init();
643+
err = scan_start();
603644
if (err) {
604-
LOG_ERR("Scanning failed to start (err %d)", err);
605645
return 0;
606646
}
607647

608-
LOG_INF("Scanning successfully started");
648+
printk("Starting Bluetooth Central UART sample\n");
609649

610650
struct uart_data_t nus_data = {
611651
.len = 0,

0 commit comments

Comments
 (0)