This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ble-app-uart-c-multilink example with whitelist

Hi,

I use the ble-app-uart-c-multilink example with a nRF52840 DK and 5 custom nRF52840 peripheral devices.

For a test, I want to connect to only one of those peripherals with address  {0xDF, 0xF8, 0x94, 0x2B, 0x7D, 0x70} so I need to implement a whitelist in the multilink example like in the ble_app_hrs_c example.

So far I have included the following code parts to multilink example main.c file

#define SCAN_DURATION_WITELIST      3000                                /**< Duration of the scanning in units of 10 milliseconds. */
static uint16_t m_conn_handle;                                      /**< Current connection handle. */
static bool     m_whitelist_disabled;                               /**< True if whitelist has been temporarily disabled. */
static bool     m_memory_access_in_progress;                        /**< Flag to keep track of ongoing operations on persistent memory. */

static ble_gap_scan_params_t const m_scan_param =
{
    .active        = 0x01,
    .interval      = NRF_BLE_SCAN_SCAN_INTERVAL,
    .window        = NRF_BLE_SCAN_SCAN_WINDOW,
    .filter_policy = BLE_GAP_SCAN_FP_WHITELIST,
    .timeout       = SCAN_DURATION_WITELIST,
    .scan_phys     = BLE_GAP_PHY_1MBPS,
};

/**@brief Names which the central applications will scan for, and which will be advertised by the peripherals.
 *  if these are set to empty strings, the UUIDs defined below will be used
 */
static char const m_target_periph_name[] = "";      /**< If you want to connect to a peripheral using a given advertising name, type its name here. */
static bool is_connect_per_addr = true;            /**< If you want to connect to a peripheral with a given address, set this to true and put the correct address in the variable below. */

static ble_gap_addr_t const m_target_periph_addr =
{
    /* Possible values for addr_type:
       BLE_GAP_ADDR_TYPE_PUBLIC,
       BLE_GAP_ADDR_TYPE_RANDOM_STATIC,
       BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE,
       BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE. */
    .addr_type = BLE_GAP_ADDR_TYPE_RANDOM_STATIC,
    .addr      = {0xDF, 0xF8, 0x94, 0x2B, 0x7D, 0x70}
};

I also modified the scan_init() function like this

    ret_code_t          err_code;
    nrf_ble_scan_init_t init_scan;

    memset(&init_scan, 0, sizeof(init_scan));

    init_scan.p_scan_param = &m_scan_param;
    init_scan.connect_if_match = true;
    init_scan.conn_cfg_tag     = APP_BLE_CONN_CFG_TAG;

    err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_ADDR_FILTER, m_target_periph_addr.addr);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ADDR_FILTER, false);
    APP_ERROR_CHECK(err_code);

When I run the code on the central (DK) an error pops up :  "    ble_scan: sd_ble_gap_scan_start returned 0x7  "

What is this error?

Thank you

  • This is printed in ble_nus_c_string_send() in components/ble/ble_services/ble_nus_c/ble_nus_c.c if the connection handle you provided to ble_nus_c_string_send() is BLE_CONN_HANDLE_INVALID, which is the default value used to indicate that there is no connection. You need to provide the connection handle, which you first get with the  case BLE_GAP_EVT_CONNECTED event, and should configure the NUS service for using ble_nus_c_handles_assign().

  • Hi, I did manage to filter specific peripheral devices based on their address the way you said and it works!
    Now, how can I recognize which peripheral device is sending its data to the central when all devices send transmit at the same time and how can I send data only to a specific peripheral? (since I want to send different commands to each peripheral individually)

    Thank you

  • Hi,

    All events related to a connection includes a connection handle, and this is what you use to distinguish between different connections. The connection handle is created when the connection is created, and the application is informed of this when you get the BLE_GAP_EVT_CONNECTED event, and this is valid until you get a BLE_GAP_EVT_DISCONNECTED event for that connection. If you need to map connection handle to address later you should store this somewhere for future reference. That way you can always look up to connection handle and know which address it represents.

  • In the BLE_GAP_EVT_CONNECTED event I placed a static variable where I assign the connection handle to track the value of the connection handle each time a new device is connected but I always receive value 0.

  • Do you have more than one connection in this case? If not, then 0 should be OK as it is a valid connection handle (BLE_CONN_HANDLE_INVALID is 0xFFFF).

Related