Skip to content

Commit 47268a3

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 cb13f44 commit 47268a3

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

  • samples/bluetooth/ble_nus_central/src

samples/bluetooth/ble_nus_central/src/main.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ static uint16_t ble_nus_max_data_len = BLE_NUS_CLIENT_MAX_DATA_LEN_CALC(BLE_GATT
7676
static uint8_t uarte_rx_buf[CONFIG_SAMPLE_NUS_CENTRAL_UART_RX_BUF_SIZE][2];
7777
static int buf_idx;
7878

79+
#if defined(CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR)
80+
/* Target peripheral address (little-endian). */
81+
static const uint8_t target_periph_addr[BLE_GAP_ADDR_LEN] = {
82+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR) & 0xff,
83+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 8) & 0xff,
84+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 16) & 0xff,
85+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 24) & 0xff,
86+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 32) & 0xff,
87+
(CONFIG_SAMPLE_TARGET_PERIPHERAL_ADDR >> 40) & 0xff,
88+
};
89+
#endif /* CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR */
90+
7991
/* Forward declaration. */
8092
static void scan_start(void);
8193

@@ -276,12 +288,12 @@ static void scan_evt_handler(const struct ble_scan_evt *scan_evt)
276288
break;
277289

278290
case BLE_SCAN_EVT_CONNECTED:
279-
const ble_gap_evt_connected_t *connected = scan_evt->connected.connected;
291+
const ble_gap_addr_t *const peer_addr = &scan_evt->connected.connected->peer_addr;
292+
280293
/* Scan is automatically stopped by the connection. */
281-
LOG_INF("Connecting to target %02x%02x%02x%02x%02x%02x",
282-
connected->peer_addr.addr[0], connected->peer_addr.addr[1],
283-
connected->peer_addr.addr[2], connected->peer_addr.addr[3],
284-
connected->peer_addr.addr[4], connected->peer_addr.addr[5]);
294+
LOG_INF("Connecting to target %02X:%02X:%02X:%02X:%02X:%02X",
295+
peer_addr->addr[5], peer_addr->addr[4], peer_addr->addr[3],
296+
peer_addr->addr[2], peer_addr->addr[1], peer_addr->addr[0]);
285297
break;
286298

287299
case BLE_SCAN_EVT_SCAN_TIMEOUT:
@@ -524,6 +536,17 @@ static uint32_t scan_init(void)
524536
}
525537
#endif /* CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_NAME */
526538

539+
#if defined(CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR)
540+
filter_data.addr_filter.addr = target_periph_addr;
541+
filter_mode_mask |= BLE_SCAN_ADDR_FILTER;
542+
543+
nrf_err = ble_scan_filter_add(&ble_scan, BLE_SCAN_ADDR_FILTER, &filter_data);
544+
if (nrf_err) {
545+
LOG_ERR("nrf_ble_scan_filter_add address failed, nrf_error %#x", nrf_err);
546+
return nrf_err;
547+
}
548+
#endif /* CONFIG_SAMPLE_USE_TARGET_PERIPHERAL_ADDR */
549+
527550
nrf_err = ble_scan_filters_enable(&ble_scan, filter_mode_mask, false);
528551
if (nrf_err) {
529552
LOG_ERR("Failed to enable scan filters, nrf_error %#x", nrf_err);

0 commit comments

Comments
 (0)