Skip to content

Commit 27aa0eb

Browse files
committed
samples: ble_nus_central: add missing addr scan filter code
The Kconfig options for address scan filter was there, but the code to set it up was missing. Add the missing bits. Change print format for logging the address of a connected peer device. Invert the bytes to correctly print the address and add colons. Signed-off-by: Andreas Moltumyr <andreas.moltumyr@nordicsemi.no>
1 parent a0dc3ff commit 27aa0eb

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

  • samples/bluetooth/ble_nus_central/src

samples/bluetooth/ble_nus_central/src/main.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ static uint16_t ble_nus_max_data_len = BLE_NUS_CLIENT_MAX_DATA_LEN_CALC(BLE_GATT
6666
static uint8_t uarte_rx_buf[CONFIG_SAMPLE_NUS_CENTRAL_UART_RX_BUF_SIZE][2];
6767
static int buf_idx;
6868

69+
#if defined(CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR)
70+
/* Target peripheral address (little-endian). */
71+
static const uint8_t target_periph_addr[BLE_GAP_ADDR_LEN] = {
72+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR) & 0xff,
73+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 8) & 0xff,
74+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 16) & 0xff,
75+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 24) & 0xff,
76+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 32) & 0xff,
77+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 40) & 0xff,
78+
};
79+
#endif /* CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR */
80+
6981
/* Forward declaration. */
7082
static void scan_start(void);
7183

@@ -267,12 +279,12 @@ static void on_ble_scan_evt(const struct ble_scan_evt *scan_evt)
267279
break;
268280

269281
case BLE_SCAN_EVT_CONNECTED:
270-
const ble_gap_evt_connected_t *connected = scan_evt->connected.connected;
271-
/** Scan is automatically stopped by the connection. */
272-
LOG_INF("Connecting to target %02x%02x%02x%02x%02x%02x",
273-
connected->peer_addr.addr[0], connected->peer_addr.addr[1],
274-
connected->peer_addr.addr[2], connected->peer_addr.addr[3],
275-
connected->peer_addr.addr[4], connected->peer_addr.addr[5]);
282+
const ble_gap_addr_t *const peer_addr = &scan_evt->connected.connected->peer_addr;
283+
284+
/* Scan is automatically stopped by the connection. */
285+
LOG_INF("Connecting to target %02X:%02X:%02X:%02X:%02X:%02X",
286+
peer_addr->addr[5], peer_addr->addr[4], peer_addr->addr[3],
287+
peer_addr->addr[2], peer_addr->addr[1], peer_addr->addr[0]);
276288
break;
277289

278290
case BLE_SCAN_EVT_SCAN_TIMEOUT:
@@ -515,6 +527,17 @@ static uint32_t scan_init(void)
515527
}
516528
#endif /* CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_NAME */
517529

530+
#if defined(CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR)
531+
filter_data.addr_filter.addr = target_periph_addr;
532+
filter_mode_mask |= BLE_SCAN_ADDR_FILTER;
533+
534+
nrf_err = ble_scan_filter_add(&ble_scan, BLE_SCAN_ADDR_FILTER, &filter_data);
535+
if (nrf_err) {
536+
LOG_ERR("nrf_ble_scan_filter_add address failed, nrf_error %#x", nrf_err);
537+
return nrf_err;
538+
}
539+
#endif /* CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR */
540+
518541
nrf_err = ble_scan_filters_enable(&ble_scan, filter_mode_mask, false);
519542
if (nrf_err) {
520543
LOG_ERR("Failed to enable scan filters, nrf_error %#x", nrf_err);

0 commit comments

Comments
 (0)