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

nrf52832 central device: how to list peripheral advertising names?

Hello,

I'm using a central device that lists all nearby peripheral devices by their MAC addresses like so:

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{

...

	case BLE_GAP_EVT_ADV_REPORT:
	{
		const ble_gap_evt_adv_report_t *p_adv = &p_ble_evt->evt.gap_evt.params.adv_report;
		DEBUG_BLUETOOTH("RSSI: %d\n", p_adv->rssi);
		DEBUG_BLUETOOTH("PHY: 0x%02x%02x%02x%02x\n", p_ble_evt->evt.gap_evt.params.scan_req_report.peer_addr.addr[3],
				p_ble_evt->evt.gap_evt.params.scan_req_report.peer_addr.addr[2],
				p_ble_evt->evt.gap_evt.params.scan_req_report.peer_addr.addr[1],
				p_ble_evt->evt.gap_evt.params.scan_req_report.peer_addr.addr[0]);
	}

...
}

but I also want to list the names of the devices as they appear in the nRF Toolbox -> UART.

Is there any struct or variable that holds this information?

Regards,

L. B.

  • Check out the implementation of ble_advdata_name_find() and ble_advdata_search() in ble_advdata.c module:

    /**@brief Function for searching encoded Advertising or Scan Response data for specific data types.
     *
     * @details This function searches through encoded data e.g. the data produced by
     *          @ref ble_advdata_encode, or the data found in Advertising reports
     *          (@ref BLE_GAP_EVT_ADV_REPORT), and gives the offset of the data within the data buffer.
     *          The data with type \p ad_type can be found at p_encoded_data[*p_offset] after calling
     *          the function. This function can iterate through multiple instances of data of one
     *          type by calling it again with the offset provided by the previous call.
     *
     *          Example code for finding multiple instances of one type of data:
     *              offset = 0;
     *              ble_advdata_search(&data, len, &offset, AD_TYPE);
     *              first_instance_of_data = data[offset];
     *              ble_advdata_search(&data, len, &offset, AD_TYPE);
     *              second_instance_of_data = data[offset];
     *
     * @param[in]    p_encoded_data  The data buffer containing the encoded Advertising data.
     * @param[in]    data_len        The length of the data buffer \p p_encoded_data.
     * @param[inout] p_offset        \c in: The offset to start searching from.
     *                               \c out: The offset the data type can be found at.
     *                                       This value is not changed if the call returns 0.
     * @param[in]    ad_type         The type of data to search for.
     *
     * @return The length of the found data, or 0 if no data was found with the the type \p ad_type,
     *         or if \p p_encoded_data or \p p_offset were NULL.
     */
    uint16_t ble_advdata_search(uint8_t const * p_encoded_data,
                                uint16_t        data_len,
                                uint16_t      * p_offset,
                                uint8_t         ad_type);

  • Hi, this helped me and it works! Thanks!

    Here is what I did:

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context){
    ...
    
    	case BLE_GAP_EVT_ADV_REPORT:
    	{
    		char name[32];
    		uint16_t offset = 0;
    		uint16_t length = ble_advdata_search(p_ble_evt->evt.gap_evt.params.adv_report.data.p_data, p_ble_evt->evt.gap_evt.params.adv_report.data.len, &offset, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);
    		if(length == 0){
    			length = ble_advdata_search(p_ble_evt->evt.gap_evt.params.adv_report.data.p_data, p_ble_evt->evt.gap_evt.params.adv_report.data.len, &offset, BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME);
    		}
    
    		if(length != 0){
    			memcpy(name, &p_ble_evt->evt.gap_evt.params.adv_report.data.p_data[offset], length);
    			name[length] = '\0';
    			DEBUG_BLUETOOTH("@@@name: %s\n", name);
    		}
    	}
    }
    
    void bluetooth_scan_connect_to_peripheral(uint32_t peripheral_index){
    	ret_code_t err_code;
    	nrf_ble_scan_init_t init_scan;
    	char arr[6] = { 'm', 'y', 'n', 'a', 'm', 'e' };
    
    	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_NAME_FILTER, arr);
    	APP_ERROR_CHECK(err_code);
    	err_code = nrf_ble_scan_filters_enable(&m_scan, NRF_BLE_SCAN_ALL_FILTER, false);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nrf_ble_scan_start(&m_scan);
    	APP_ERROR_CHECK(err_code);
    }

Related