I have a characteristic which is a long string. I need to be able to read and write it.
Below is the function for writing to start with. Please comment on this function. The guesswork it has is inverse proportional to my level of BLE expertise(very low) May be someone already has such function debugged and is willing to share.
#define MAX_MSG_LEN 20
uint32_t long_write (uint16_t conn_handle,
uint16_t msg_handle,
uint16_t len,
uint8_t * p_msg)
{
// Send value if connected. notification is assumed
if (conn_handle != BLE_CONN_HANDLE_INVALID)
{
ble_gatts_hvx_params_t hvx_params;
uint16_t block;
for (uint16_t i = 0; i < len; i += MAX_MSG_LEN)
{
block = ((len - i) > MAX_MSG_LEN) ? MAX_MSG_LEN : len - i;
memset(&hvx_params, 0, sizeof(hvx_params));
hvx_params.handle = msg_handle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = i;
hvx_params.p_len = █
hvx_params.p_data = &msg[i];
err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
if (err_code != NRF_SUCCESS) break;
}
else
{
err_code = NRF_ERROR_INVALID_STATE;
}
return err_code;
}
Pretty sure it can be optimized, at least I'm guessing that block has value of actually written bytes...
Thank you.
Edit: one sentence question, commas between tags.