sd_ble_gatts_hvx gives error as NRF_ERROR_RESOURCES and my code gets stuck

uint32_t ble_cus_sensor_state_update(ble_cus_t * p_cus, int8_t  * p_buttons_states, int length, uint16_t conn_handle)
{
    ble_gatts_hvx_params_t params;
    uint16_t len = length;
    uint32_t err_code;

    memset(&params, 0, sizeof(params));
    params.type   = BLE_GATT_HVX_NOTIFICATION;//BLE_GATT_HVX_NOTIFICATION;
    params.handle = p_cus->leds_states_char_handles.value_handle;
    params.p_data = p_buttons_states;
    params.p_len  = &len;

    return sd_ble_gatts_hvx(conn_handle, &params);
}



void ble_cus_send_sensor_data(uint8_t *data, uint16_t len)
{
      ret_code_t err_code;
      uint32_t timer = millis();
      do{
        err_code = ble_cus_sensor_state_update(&m_cus, data, len, m_conn_handle);
      }while(err_code == BLE_ERROR_GATTS_SYS_ATTR_MISSING && millis() - timer < 3000 && (err_code == NRF_ERROR_RESOURCES));
      if ( err_code != NRF_SUCCESS                   &&
           err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
           err_code != NRF_ERROR_INVALID_STATE       &&
        /*   (err_code != NRF_ERROR_RESOURCES)         &&*/
           err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
      {

        APP_ERROR_CHECK(err_code);
      }
}

void send_current_data(void)
{
	printf("Sending current data \n");
	for(int i=0;i<500;i++)
	{
		memset(lv_data_header,0,sizeof(lv_data_header));
		lv_data_header[0] =0xFF;
		lv_data_header[0] =i;
		ble_cus_send_sensor_data(lv_data_header, 2); 
		if( m_ble_cust_packet_sent == true)
		{
			printf("Sending current data packet %d  \n",i);
			ble_cus_send_sensor_data(lv_data_header, 2); 
		}
	}
}


void cus_evt_handler(ble_cus_t * p_cus, ble_cus_evt_t * p_evt)
{ 
   uint8_t u8_cmd_id = 0;

  switch(p_evt->evt_type)
  {
    case BLE_LEDS_STATES_CHAR_EVT_COMMAND_RX:
    {
        if(p_evt->params_command.command_data.p_data[0] == 0XFD)
		{
			send_current_data();
		}
        

	}
}

Hi Nordic team

Currently I am trying to send a big chunk of data close to 100 KB from my nRF52832 to nRF connect for mobile app in Android phone. I am able to send few bytes in one shot. But when I kept a loop for sending more than 10 data packets, my code is getting stuck in while loop and sd_ble_gatts_hvx  returns NRF_ERROR_RESOURCES error. After that my application is stuck until I restart the application. It is not accepting any new command and stops sending the data. 

I already change hvn_tx_queue_size = 20 and NRF_SDH_BLE_GAP_EVENT_LENGTH to 20 even to larger number , but still it doesn't resolve the issue.

Also it is blocking BLE_GATTS_EVT_HVN_TX_COMPLETE event handler. So I am unable to understand if the queue buffer resource is empty or not.

  • So what does it return?

    I did notice that you didn't follow the suggestion that I mentioned in my pseudo code snippet, to leave the event and trigger the sending from another place (see what I did with the send_data_flag parameter).

    However, when you are stuck, what does ble_cus_sensor_state_update() return? It is not NRF_ERROR_RESOURCES, because according to your ble_cus_send_sensor_data(), because if it did, it would be passed onto the APP_ERROR_CHECK(err_code), and the application would restart.

    Does it return BLE_ERROR_GATTS_SYS_ATTR_MISSING? If so, what is the expected behavior? Because now it will keep trying, but your timer parameter will not change.

    Best regards,

    Edvin

Related