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

Parsing Device_name

I have a number of nRF52832 beacons that advertise at a set interval. All the beacons have a "device_name" set as "ECO". My application scans for these beacons with the device_name == "ECO". When the application reads a beacon device_name of "ECO", I then read the beacon MAC address of the beacon with the best RSSI. However my problem is that when I parse the device name, I am reading beacons with other device_name. There seems to be a problem with my parsing of the scanned device_name.


My Code:

static uint32_t adv_report_parse(uint8_t type, uint8_array_t * p_advdata, uint8_array_t * p_typedata)
{
    uint32_t  index = 0;
    uint8_t * p_data;
    p_data = p_advdata->p_data;

    while (index < p_advdata->size)
    {
        uint8_t field_length = p_data[index];
        uint8_t field_type   = p_data[index + 1];

        if (field_type == type)
        {
            p_typedata->p_data = &p_data[index + 2];
            p_typedata->size   = field_length - 1;
            return NRF_SUCCESS;
        }
        index += field_length + 1;
    }
    return NRF_ERROR_NOT_FOUND;
}

/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in] p_ble_evt  Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
    uint32_t            err_code = 0;
    const ble_gap_evt_t * p_gap_evt = &p_ble_evt->evt.gap_evt;
	uint8_array_t adv_data;
    uint8_array_t dev_name; 
	bool found_name = false; 
	int count;
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_ADV_REPORT:
        {
            const ble_gap_evt_adv_report_t * p_adv_report = &p_gap_evt->params.adv_report;
            {
                {  
					// Prepare advertisement report for parsing.
					adv_data.p_data = (uint8_t *)p_gap_evt->params.adv_report.data;
					adv_data.size	= p_gap_evt->params.adv_report.dlen;
					
					// Search for advertising names.
					err_code = adv_report_parse(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
												&adv_data,
												&dev_name);
					if (err_code != NRF_SUCCESS)
					{
						// Look for the short local name if it was not found as complete.
						err_code = adv_report_parse(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, &adv_data, &dev_name);
						if (err_code != NRF_SUCCESS)
						{
							// If we can't parse the data, then exit.
							return;
						}
						else
						{
							found_name = true;
						}
					}
					else
					{
						found_name = true;
					}
					
					if (found_name)
					{   						
						if (strlen(m_target_periph_name) != 0)
						{
							if (memcmp((char*)dev_name.p_data,m_target_periph_name , dev_name.size) == 0)
							{								
								if(last_beacon.rssi < p_adv_report->rssi)
								{
									
									for(count = 0;count < 6; count++)
									{
										last_beacon.addr[count] = p_adv_report->peer_addr.addr[5-count];
										
									}
									last_beacon.rssi =  p_adv_report->rssi;
								}
								
								cPrintLog(CDBG_FCTRL_INFO,"found beacon : %s\n",dev_name.p_data );
								
								cPrintLog(CDBG_FCTRL_INFO,"Scanning target: %02x%02x%02x%02x%02x%02x\r\n",
														
													 p_adv_report->peer_addr.addr[0],
													 p_adv_report->peer_addr.addr[1],
													 p_adv_report->peer_addr.addr[2],
													 p_adv_report->peer_addr.addr[3],
													 p_adv_report->peer_addr.addr[4],
													 p_adv_report->peer_addr.addr[5]
													 );
							}
						}
					}
                }
            }
        }break; // BLE_GAP_EVT_ADV_
Related