Hello,
In demo code of Central_Uart code in nRF Connect SDK, when the scanning is started the scan address (MAC) keeps on changing everytime the code is run. (The central board and peripheral board are not connected in this case because when the CONNECT_REQ happens I see the correct scan MAC address. In order to stop the connection between central and peripheral boards I have changed this line in the existing Central_Uart code -> bt_scan_init(NULL);
I have turned off privacy modes by putting these 2 lines in the prj.conf file
/* * Copyright (c) 2018 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic */ /** @file * @brief Nordic UART Service Client sample */ #include <errno.h> #include <zephyr.h> #include <sys/byteorder.h> #include <sys/printk.h> #include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <bluetooth/conn.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> #include <bluetooth/services/nus.h> #include <bluetooth/services/nus_c.h> #include <bluetooth/gatt_dm.h> #include <bluetooth/scan.h> #include <settings/settings.h> #include <drivers/uart.h> /* UART payload buffer element size. */ #define UART_BUF_SIZE 20 #define KEY_PASSKEY_ACCEPT DK_BTN1_MSK #define KEY_PASSKEY_REJECT DK_BTN2_MSK #define NUS_WRITE_TIMEOUT K_MSEC(150) static struct device *uart; static bool rx_disabled; K_SEM_DEFINE(nus_write_sem, 0, 1); struct uart_data_t { void *fifo_reserved; u8_t data[UART_BUF_SIZE]; u16_t len; }; static K_FIFO_DEFINE(fifo_uart_tx_data); static K_FIFO_DEFINE(fifo_uart_rx_data); static struct bt_conn *default_conn; static struct bt_gatt_nus_c gatt_nus_c; static void ble_data_sent(u8_t err, const u8_t *const data, u16_t len) { struct uart_data_t *buf; /* Retrieve buffer context. */ buf = CONTAINER_OF(data, struct uart_data_t, data); k_free(buf); if (rx_disabled) { rx_disabled = false; uart_irq_rx_enable(uart); } k_sem_give(&nus_write_sem); if (err) { printk("ATT error code: 0x%02X\n", err); } } static u8_t ble_data_received(const u8_t *const data, u16_t len) { for (u16_t pos = 0; pos != len;) { struct uart_data_t *tx = k_malloc(sizeof(*tx)); if (!tx) { printk("Not able to allocate UART send data buffer\n"); return BT_GATT_ITER_CONTINUE; } /* Keep the last byte of TX buffer for potential LF char. */ size_t tx_data_size = sizeof(tx->data) - 1; if ((len - pos) > tx_data_size) { tx->len = tx_data_size; } else { tx->len = (len - pos); } memcpy(tx->data, &data[pos], tx->len); pos += tx->len; /* Append the LF character when the CR character triggered * transmission from the peer. */ if ((pos == len) && (data[len - 1] == '\r')) { tx->data[tx->len] = '\n'; tx->len++; } k_fifo_put(&fifo_uart_tx_data, tx); } /* Start the UART transfer by enabling the TX ready interrupt */ uart_irq_tx_enable(uart); return BT_GATT_ITER_CONTINUE; } static void uart_cb(struct device *uart) { static struct uart_data_t *rx; uart_irq_update(uart); if (uart_irq_rx_ready(uart)) { int data_length; if (!rx) { rx = k_malloc(sizeof(*rx)); if (rx) { rx->len = 0; } else { /* Disable UART interface, it will be * enable again after releasing the buffer. */ uart_irq_rx_disable(uart); rx_disabled = true; printk("Not able to allocate UART receive buffer\n"); return; } } data_length = uart_fifo_read(uart, &rx->data[rx->len], UART_BUF_SIZE - rx->len); rx->len += data_length; if (rx->len > 0) { /* Send buffer to Bluetooth unit if either buffer size * is reached or the char \n or \r is received, which * ever comes first */ if ((rx->len == UART_BUF_SIZE) || (rx->data[rx->len - 1] == '\n') || (rx->data[rx->len - 1] == '\r')) { k_fifo_put(&fifo_uart_rx_data, rx); rx = NULL; } } } if (uart_irq_tx_ready(uart)) { struct uart_data_t *buf = k_fifo_get(&fifo_uart_tx_data, K_NO_WAIT); u16_t written = 0; /* Nothing in the FIFO, nothing to send */ if (!buf) { uart_irq_tx_disable(uart); return; } while (buf->len > written) { written += uart_fifo_fill(uart, &buf->data[written], buf->len - written); } while (!uart_irq_tx_complete(uart)) { /* Wait for the last byte to get * shifted out of the module */ } if (k_fifo_is_empty(&fifo_uart_tx_data)) { uart_irq_tx_disable(uart); } k_free(buf); } } static void discovery_complete(struct bt_gatt_dm *dm, void *context) { struct bt_gatt_nus_c *nus_c = context; printk("Service discovery completed\n"); bt_gatt_dm_data_print(dm); bt_gatt_nus_c_handles_assign(dm, nus_c); bt_gatt_nus_c_tx_notif_enable(nus_c); bt_gatt_dm_data_release(dm); } static void discovery_service_not_found(struct bt_conn *conn, void *context) { printk("Service not found\n"); } static void discovery_error(struct bt_conn *conn, int err, void *context) { printk("Error while discovering GATT database: (%d)\n", err); } struct bt_gatt_dm_cb discovery_cb = { .completed = discovery_complete, .service_not_found = discovery_service_not_found, .error_found = discovery_error, }; static void gatt_discover(struct bt_conn *conn) { int err; if (conn != default_conn) { return; } err = bt_gatt_dm_start(conn, BT_UUID_NUS_SERVICE, &discovery_cb, &gatt_nus_c); if (err) { printk("could not start the discovery procedure, error " "code: %d\n", err); } } static void connected(struct bt_conn *conn, u8_t conn_err) { char addr[BT_ADDR_LE_STR_LEN]; int err; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { printk("Failed to connect to %s (%d)\n", addr, conn_err); return; } printk("Connected: %s\n", addr); err = bt_conn_set_security(conn, BT_SECURITY_L2); if (err) { printk("Failed to set security: %d\n", err); gatt_discover(conn); } err = bt_scan_stop(); if ((!err) && (err != -EALREADY)) { printk("Stop LE scan failed (err %d)\n", err); } } static void disconnected(struct bt_conn *conn, u8_t reason) { char addr[BT_ADDR_LE_STR_LEN]; int err; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Disconnected: %s (reason %u)\n", addr, reason); if (default_conn != conn) { return; } bt_conn_unref(default_conn); default_conn = NULL; err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE); if (err) { printk("Scanning failed to start (err %d)\n", err); } } static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (!err) { printk("Security changed: %s level %u\n", addr, level); } else { printk("Security failed: %s level %u err %d\n", addr, level, err); } gatt_discover(conn); } static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, .security_changed = security_changed }; static void scan_filter_match(struct bt_scan_device_info *device_info, struct bt_scan_filter_match *filter_match, bool connectable) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(device_info->addr, addr, sizeof(addr)); printk("Filters matched. Address: %s connectable: %d\n", addr, connectable); } static void scan_connecting_error(struct bt_scan_device_info *device_info) { printk("Connecting failed\n"); } static void scan_connecting(struct bt_scan_device_info *device_info, struct bt_conn *conn) { default_conn = bt_conn_ref(conn); } static int uart_init(void) { uart = device_get_binding(DT_LABEL(DT_NODELABEL(uart0))); if (!uart) { printk("UART binding failed\n"); return -ENXIO; } uart_irq_callback_set(uart, uart_cb); uart_irq_rx_enable(uart); printk("UART initialized\n"); return 0; } static int nus_client_init(void) { int err; struct bt_gatt_nus_c_init_param nus_c_init_obj = { .cbs = { .data_received = ble_data_received, .data_sent = ble_data_sent, } }; err = bt_gatt_nus_c_init(&gatt_nus_c, &nus_c_init_obj); if (err) { printk("NUS Client initialization failed (err %d)\n", err); return err; } printk("NUS Client module initialized\n"); return err; } BT_SCAN_CB_INIT(scan_cb, scan_filter_match, NULL, scan_connecting_error, scan_connecting); static int scan_init(void) { int err; struct bt_scan_init_param scan_init = { .connect_if_match = 1, }; bt_scan_init(NULL); bt_scan_cb_register(&scan_cb); err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_NUS_SERVICE); if (err) { printk("Scanning filters cannot be set (err %d)\n", err); return err; } err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER, false); if (err) { printk("Filters cannot be turned on (err %d)\n", err); return err; } printk("Scan module initialized\n"); return err; } static void auth_cancel(struct bt_conn *conn) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Pairing cancelled: %s\n", addr); } static void pairing_confirm(struct bt_conn *conn) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); bt_conn_auth_pairing_confirm(conn); printk("Pairing confirmed: %s\n", addr); } static void pairing_complete(struct bt_conn *conn, bool bonded) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Pairing completed: %s, bonded: %d\n", addr, bonded); } static void pairing_failed(struct bt_conn *conn, enum bt_security_err reason) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Pairing failed conn: %s, reason %d\n", addr, reason); } static struct bt_conn_auth_cb conn_auth_callbacks = { .cancel = auth_cancel, .pairing_confirm = pairing_confirm, .pairing_complete = pairing_complete, .pairing_failed = pairing_failed }; void main(void) { int err; printk("Starting Bluetooth Central UART example\n"); err = bt_conn_auth_cb_register(&conn_auth_callbacks); if (err) { printk("Failed to register authorization callbacks.\n"); return; } err = bt_enable(NULL); if (err) { printk("Bluetooth init failed (err %d)\n", err); return; } printk("Bluetooth initialized\n"); if (IS_ENABLED(CONFIG_SETTINGS)) { settings_load(); } bt_conn_cb_register(&conn_callbacks); int (*module_init[])(void) = {scan_init}; for (size_t i = 0; i < ARRAY_SIZE(module_init); i++) { err = (*module_init[i])(); if (err) { return; } } err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE); if (err) { printk("Scanning failed to start (err %d)\n", err); return; } printk("Scanning successfully started\n"); for (;;) { /* Wait indefinitely for data to be sent over Bluetooth */ struct uart_data_t *buf = k_fifo_get(&fifo_uart_rx_data, K_FOREVER); err = bt_gatt_nus_c_send(&gatt_nus_c, buf->data, buf->len); if (err) { printk("Failed to send data over BLE connection" "(err %d)\n", err); } err = k_sem_take(&nus_write_sem, NUS_WRITE_TIMEOUT); if (err) { printk("NUS send timeout\n"); } } }