Skip to content

Commit eb6076c

Browse files
alexsvenkoffes
authored andcommitted
applications: nrf5340_audio: Add scan for address
- Add possibility to scan for a given address - Useful for testing against a specific device with a static address - OCT-NONE Signed-off-by: Alexander Svensen <[email protected]>
1 parent 9072ba1 commit eb6076c

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

applications/nrf5340_audio/src/bluetooth/bt_management/scanning/Kconfig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@ config SCAN_MODE_ACTIVE
2020

2121
endchoice
2222

23+
choice BT_MGMT_CONNECT_BY
24+
prompt "Scan Filter"
25+
default BT_MGMT_CONNECT_BY_NAME
26+
27+
config BT_MGMT_CONNECT_BY_NAME
28+
bool "Name"
29+
help
30+
If enabled, the scanner will look for a specific name instead of just connecting
31+
to the first device it finds. The name to look for is CONFIG_BT_DEVICE_NAME.
32+
This is useful for testing with a known device.
33+
Note: This is only used if no CSIP Set Identity Resolving Key (SIRK) is available
34+
in the bond information.
35+
36+
config BT_MGMT_CONNECT_BY_ADDR
37+
bool "Address"
38+
help
39+
If enabled, the scanner will look for a specific address instead of a name.
40+
The address to look for is set in bt_mgmt_scan_for_conn.c in the addr_check()
41+
function. This is useful for testing with a known device.
42+
Note: This is only used if no CSIP Set Identity Resolving Key (SIRK) is available
43+
in the bond information.
44+
45+
endchoice
46+
47+
config CONNECT_TO_BT_ADDR_LE_STR
48+
string "Bluetooth LE Address to connect to"
49+
default "00:00:00:00:00:00"
50+
help
51+
The Bluetooth LE address to connect to. This is useful for testing with a
52+
specific device.
53+
2354
#----------------------------------------------------------------------------#
2455
menu "Connection"
2556

applications/nrf5340_audio/src/bluetooth/bt_management/scanning/bt_mgmt_scan_for_conn.c

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <zephyr/logging/log.h>
2020
LOG_MODULE_DECLARE(bt_mgmt_scan);
2121

22+
#define BT_ADDR_LE_STR CONFIG_CONNECT_TO_BT_ADDR_LE_STR /* Replace with desired address */
2223
#define CONNECTION_PARAMETERS \
2324
BT_LE_CONN_PARAM(CONFIG_BLE_ACL_CONN_INTERVAL, CONFIG_BLE_ACL_CONN_INTERVAL, \
2425
CONFIG_BLE_ACL_SLAVE_LATENCY, CONFIG_BLE_ACL_SUP_TIMEOUT)
@@ -184,6 +185,73 @@ static bool device_name_check(struct bt_data *data, void *user_data)
184185
return true;
185186
}
186187

188+
/**
189+
* @brief Check the advertising data for the matching address.
190+
*
191+
* @param[in] data The advertising data to be checked.
192+
* @param[in] user_data Pointer to the address.
193+
*
194+
* @retval false Stop going through adv data.
195+
* @retval true Continue checking the data.
196+
*/
197+
static bool addr_check(struct bt_data *data, void *user_data)
198+
{
199+
int ret;
200+
char addr_string[BT_ADDR_LE_STR_LEN];
201+
bt_addr_le_t *addr = user_data;
202+
struct bt_conn *conn = NULL;
203+
204+
bt_addr_le_to_str(addr, addr_string, BT_ADDR_LE_STR_LEN);
205+
206+
bt_addr_le_t cmp_addr;
207+
/* BT_ADDR_LE_STR defined at the start of this file */
208+
209+
ret = bt_addr_le_from_str(BT_ADDR_LE_STR, "public", &cmp_addr);
210+
if (ret) {
211+
LOG_ERR("Failed to create bt_addr_le from string: %d", ret);
212+
return true;
213+
}
214+
215+
bt_addr_le_to_str(&cmp_addr, addr_string, BT_ADDR_LE_STR_LEN);
216+
217+
if (!bt_addr_le_cmp(addr, &cmp_addr)) {
218+
/* Check if the device is still connected due to waiting for ACL timeout */
219+
if (conn_exist_check(addr)) {
220+
/* Device is already connected, stop parsing the adv data */
221+
return false;
222+
}
223+
224+
LOG_INF("Device found: %s", srch_name);
225+
226+
bt_le_scan_cb_unregister(&scan_callback);
227+
cb_registered = false;
228+
229+
ret = bt_le_scan_stop();
230+
if (ret) {
231+
LOG_ERR("Stop scan failed: %d", ret);
232+
}
233+
234+
bt_addr_le_to_str(addr, addr_string, BT_ADDR_LE_STR_LEN);
235+
236+
LOG_INF("Creating connection to device: %s", addr_string);
237+
238+
ret = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, CONNECTION_PARAMETERS, &conn);
239+
if (ret) {
240+
LOG_ERR("Could not init connection: %d", ret);
241+
242+
ret = bt_mgmt_scan_start(0, 0, BT_MGMT_SCAN_TYPE_CONN, NULL,
243+
BRDCAST_ID_NOT_USED);
244+
if (ret) {
245+
LOG_ERR("Failed to restart scanning: %d", ret);
246+
}
247+
}
248+
249+
return false;
250+
}
251+
252+
return true;
253+
}
254+
187255
/**
188256
* @brief Check the advertising data for the matching 'Set Identity Resolving Key' (SIRK).
189257
*
@@ -267,7 +335,11 @@ static void scan_recv_cb(const struct bt_le_scan_recv_info *info, struct net_buf
267335
/* Note: May lead to connection creation */
268336
if (bonded_num < CONFIG_BT_MAX_PAIRED) {
269337
if (server_sirk == NULL) {
270-
bt_data_parse(ad, device_name_check, (void *)info->addr);
338+
if (IS_ENABLED(CONFIG_BT_MGMT_CONNECT_BY_ADDR)) {
339+
bt_data_parse(ad, addr_check, (void *)info->addr);
340+
} else {
341+
bt_data_parse(ad, device_name_check, (void *)info->addr);
342+
}
271343
} else {
272344
bt_data_parse(ad, csip_found, (void *)info->addr);
273345
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ nRF5340 Audio
236236

237237
* The :ref:`Audio application API documentation <audio_api>` page.
238238
* The :ref:`config_audio_app_options` page.
239+
* The API documentation in the header files listed on the :ref:`audio_api` page.
240+
* Ability to connect by address as a unicast client.
239241

240242
* Updated:
241243

0 commit comments

Comments
 (0)