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

Unable to receive full data over BLE

I am trying to receive a string of 708 characters via BLE. As BLE cap a maximum of 20 bytes per transmission, at the mobile app side I break the data to be send every 20 characters (each char is 1 byte as < 7F). The code at Nordic to receive the data is as follows:

uint8_t *buf = NULL;
size_t total_len = 0;
.
.
.
case BLE_GATTS_EVT_WRITE:
{ 
            ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
            size_t len = p_evt_write->len;
            buf = realloc(buf, total_len + len);
        	memcpy(buf + total_len, p_evt_write->data, len); 	
        	total_len += len;
        	SEGGER_RTT_printf(0, "current concatenated str: %s \n", buf);
        	SEGGER_RTT_printf(0, "total_len: %d \n", total_len);
        	if((p_evt_write->data[len-2] == 123) && (p_evt_write->data[len-1] == 123)){
        		 char* strall= malloc(total_len + 1);
        		 memcpy(strall, buf, total_len);
        		 strall[total_len] = 0;
        		 SEGGER_RTT_printf(0, "whole string is: %s \n", strall);
        		 total_len = 0;		
        	}

}

After receiving each round of characters, I concatenate it together to form back the original string. However, I am able to receive up till 429th character before it stops concatenating anymore new data. The 'buf' string either prints 0-429th characters then concatenate with some characters that was already previously received, or it just keep printing 0-429th character repeatedly even when new data is received (total_len keeps increasing till 708).

Why is this the case? Is there anything preventing Nordic from receiving the whole data? I will need to receive and concatenate all the characters.

Parents
  • Stop using malloc/realloc and similar crap and just create global static 1kB buffer. Your problem will be most probably (magically) solved;)

  • Thank you for the help. I tried what you suggested and is still encountering the same error. I made another strall buffer as I intend to manipulate the data received from BLE, and I am afraid there may be new data coming in when I am dealing with the original packet (resulting in strall to be the new data instead of the original one). As for the condition 123, the sending party put that as the indication of the end of the packet, hence in Nordic I am checking for it to know whether it receives the whole data. I changed size_t to uint32_t as well.

Reply
  • Thank you for the help. I tried what you suggested and is still encountering the same error. I made another strall buffer as I intend to manipulate the data received from BLE, and I am afraid there may be new data coming in when I am dealing with the original packet (resulting in strall to be the new data instead of the original one). As for the condition 123, the sending party put that as the indication of the end of the packet, hence in Nordic I am checking for it to know whether it receives the whole data. I changed size_t to uint32_t as well.

Children
No Data
Related