This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

where to change name for the scanning peripheral name in the uart central code ??

Hi,

i Have two development kits where one acts as uart central and another acts as uart peripheral .

In that i have given the peripherals different names(DEVICE_NAME), (the default name for ble_app_uart example is Nordic_UART.)

where do i need to change in the uart central for the scanning name ??

Parents
  • Hi.

    The UART Central example filters on UUID

    In line 164 in main.c, in the ble_app_uart_c example you find the following function call for initializing scanning:

    static void scan_init(void)
    {
        ret_code_t          err_code;
        nrf_ble_scan_init_t init_scan;
    
        memset(&init_scan, 0, sizeof(init_scan));
    
        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_UUID_FILTER, &m_nus_uuid);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_UUID_FILTER, false);
        APP_ERROR_CHECK(err_code);
    }

    The two function calls you have to do in order to scan for a name is nrf_ble_scan_filters_enable() and nrf_ble_scan_filters_set().

    API for nrf_ble_scan_filters_set() is the following:

    /**@brief Function for adding any type of filter to the scanning.
     *
     * @details This function adds a new filter by type @ref nrf_ble_scan_filter_type_t.
     *          The filter will be added if the number of filters of a given type does not exceed @ref NRF_BLE_SCAN_UUID_CNT,
     *          @ref NRF_BLE_SCAN_NAME_CNT, @ref NRF_BLE_SCAN_ADDRESS_CNT, or @ref NRF_BLE_SCAN_APPEARANCE_CNT, depending on the filter type,
     *          and if the same filter has not already been set.
     *
     * @param[in,out] p_scan_ctx        Pointer to the Scanning Module instance.
     * @param[in]     type              Filter type.
     * @param[in]     p_data            The filter data to add.
     *
     * @retval NRF_SUCCESS                    If the filter is added successfully.
     * @retval NRF_ERROR_NULL                 If a NULL pointer is passed as input.
     * @retval NRF_ERROR_DATA_SIZE            If the name filter length is too long. Maximum name filter length corresponds to @ref NRF_BLE_SCAN_NAME_MAX_LEN.
     * @retval NRF_ERROR_NO_MEMORY            If the number of available filters is exceeded.
     * @retval NRF_ERROR_INVALID_PARAM        If the filter type is incorrect. Available filter types: @ref nrf_ble_scan_filter_type_t.
     * @retval BLE_ERROR_GAP_INVALID_BLE_ADDR If the BLE address type is invalid.
     */
    ret_code_t nrf_ble_scan_filter_set(nrf_ble_scan_t     * const p_scan_ctx,
                                       nrf_ble_scan_filter_type_t type,
                                       void const               * p_data);

    API for nrf_ble_scan_filters_enable() is the following:

    /**@brief Function for enabling filtering.
     *
     * @details The filters can be combined with each other. For example, you can enable one filter or several filters.
     *          For example, (NRF_BLE_SCAN_NAME_FILTER | NRF_BLE_SCAN_UUID_FILTER) enables UUID and name filters.
     *
     * @param[in] mode                  Filter mode: @ref NRF_BLE_SCAN_FILTER_MODE.
     * @param[in] match_all             If this flag is set, all types of enabled filters must be matched
     *                                  before generating @ref NRF_BLE_SCAN_EVT_FILTER_MATCH to the main application. Otherwise, it is enough to match
     *                                  one filter to trigger the filter match event.
     * @param[in] p_scan_ctx            Pointer to the Scanning Module instance.
     *
     * @retval NRF_SUCCESS              If the filters are enabled successfully.
     * @retval NRF_ERROR_INVALID_PARAM  If the filter mode is incorrect. Available filter modes: @ref NRF_BLE_SCAN_FILTER_MODE.
     * @retval NRF_ERROR_NULL           If a NULL pointer is passed as input.
     */
    ret_code_t nrf_ble_scan_filters_enable(nrf_ble_scan_t * const p_scan_ctx,
                                           uint8_t                mode,
                                           bool                   match_all);
    

    You have to change those in parameters used in the ble_app_uart_c examples scan_init to the following:

    static void scan_init(void)
    {
        ret_code_t          err_code;
        nrf_ble_scan_init_t init_scan;
    
        memset(&init_scan, 0, sizeof(init_scan));
    
        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);
    
        // Setting filters for scanning.
        err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_NAME_FILTER, false);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_ble_scan_filter_set(&m_scan, SCAN_NAME_FILTER, m_target_periph_name);
        APP_ERROR_CHECK(err_code);
    }

    And also define the name you wish do search for under your defines in main.c:

    static char const m_target_periph_name[] = "Nordic_UART";     /**< Name of the device we try to connect to. This name is searched in the scan report data*/

    This should do the trick.

    Hope this was helpful.

    Best regards.

    Andreas

  • I've tried this exact code but I cannot seem to get the Central to see my advertising Peripheral.  It just keeps returning NRF_BLE_SCAN_EVT_NOT_FOUND.  My code is:

    static char const m_target_periph_name[] = "Test_Device";

    static void scan_init(void)
    {
    ret_code_t err_code;
    nrf_ble_scan_init_t init_scan;

    memset(&init_scan, 0, sizeof(init_scan));
    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_filters_enable(&m_scan,
    NRF_BLE_SCAN_NAME_FILTER,
    true);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_scan_filter_set(&m_scan,
    SCAN_NAME_FILTER,
    m_target_periph_name);
    APP_ERROR_CHECK(err_code);
    }

    Am I missing anything?

Reply
  • I've tried this exact code but I cannot seem to get the Central to see my advertising Peripheral.  It just keeps returning NRF_BLE_SCAN_EVT_NOT_FOUND.  My code is:

    static char const m_target_periph_name[] = "Test_Device";

    static void scan_init(void)
    {
    ret_code_t err_code;
    nrf_ble_scan_init_t init_scan;

    memset(&init_scan, 0, sizeof(init_scan));
    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_filters_enable(&m_scan,
    NRF_BLE_SCAN_NAME_FILTER,
    true);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_ble_scan_filter_set(&m_scan,
    SCAN_NAME_FILTER,
    m_target_periph_name);
    APP_ERROR_CHECK(err_code);
    }

    Am I missing anything?

Children
  • Hi.

    In the function call:

    	err_code = nrf_ble_scan_filters_enable(&m_scan,NRF_BLE_SCAN_NAME_FILTER,true);
    	APP_ERROR_CHECK(err_code);
    

    You have set this to true, from the API declaration I posted below you can read this:

     * @param[in] match_all             If this flag is set, all types of enabled filters must be matched
     *                                  before generating @ref NRF_BLE_SCAN_EVT_FILTER_MATCH to the main application. Otherwise, it is enough to match
     *                                  one filter to trigger the filter match event.

    Try to set it as false.

    Best regards,

    Andreas

Related