In the on_ble_evt(ble_evt_t*) function from the ble_app_template, I added a case of 'BLE_GATTS_EVT_WRITE'. In it has the following code:
case BLE_GATTS_EVT_WRITE:
{
ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
SEGGER_RTT_printf(0, "Length = %d \n", p_evt_write->len);
int n;
char* buf2 = "";
char* buf3 = "";
count += p_evt_write->len;
for(n=0; n<p_evt_write->len; n++)
{
SEGGER_RTT_printf(0, "Received[%d] : %X \n", n, p_evt_write->data[n]);
SEGGER_RTT_printf(0, "Received[%d] : %d \n", n, p_evt_write->data[n]);
if(n>0){
sprintf(buf2, "%s", ((char*)p_evt_write->data[n]));
strcat(buf3, buf2);
}
else{
sprintf(buf2, "%s", ((char*)p_evt_write->data[n]));
strcpy(buf3, buf2);
}
SEGGER_RTT_printf(0, "buf2 string: %s \n", buf2);
SEGGER_RTT_printf(0, "buf2 hex: %X \n", buf2[0]);
SEGGER_RTT_printf(0, "buf3 in string: %s \n", buf3);
SEGGER_RTT_printf(0, "count: %d \n", count);
}
}
I am receiving hex values from BLE stored in p_evt_write->data[n]. I want to concatenate all these received hex values into a string, storing it in 'buf2'.
However I am receiving errors at the line of sprintf. When I put as "%X" at sprintf, the values are not converted into a string as buf2 string does not print anything. The current code of
sprintf(buf2, "%s", ((char*)p_evt_write->data[n]));
returns an error of 'error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]'. If I removed the (char*) typecast, it returns an error of 'error: format '%s' expects argument of type 'char *''.
I saw that p_evt_write->data[n] where data is declared as
uint8_t data[1];
How can I successfully concatenate all the hex values together to form a string? Thank you.