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.