Hello,
Please take a look at the below function. I am able to write/read the characteristic data from nRF App.
static uint32_t our_char_add(ble_os_t * p_our_service) { // OUR_JOB: Step 2.A, Add a custom characteristic UUID uint32_t err_code; ble_uuid_t char_uuid; ble_uuid128_t base_uuid = BLE_UUID_OUR_BASE_UUID; char_uuid.uuid = BLE_UUID_OUR_CHARACTERISTC_UUID; ble_uuid128_t base_uuid1 = BLE_UUID_OUR_CHARACTERISTC_UUID1; char_uuid.uuid = BLE_UUID_OUR_CHARACTERISTC_UUID; err_code = sd_ble_uuid_vs_add(&base_uuid1, &char_uuid.type); APP_ERROR_CHECK(err_code); // OUR_JOB: Step 2.F Add read/write properties to our characteristic ble_gatts_char_md_t char_md; memset(&char_md, 0, sizeof(char_md)); char_md.char_props.read = 1; char_md.char_props.write = 1; // OUR_JOB: Step 3.A, Configuring Client Characteristic Configuration Descriptor metadata and add to char_md structure ble_gatts_attr_md_t cccd_md; memset(&cccd_md, 0, sizeof(cccd_md)); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); cccd_md.vloc = BLE_GATTS_VLOC_STACK; char_md.p_cccd_md = &cccd_md; char_md.char_props.notify = 1; // OUR_JOB: Step 2.B, Configure the attribute metadata ble_gatts_attr_md_t attr_md; memset(&attr_md, 0, sizeof(attr_md)); attr_md.vloc = BLE_GATTS_VLOC_STACK; // OUR_JOB: Step 2.G, Set read/write security levels to our characteristic BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); // OUR_JOB: Step 2.C, Configure the characteristic value attribute ble_gatts_attr_t attr_char_value; memset(&attr_char_value, 0, sizeof(attr_char_value)); attr_char_value.p_uuid = &char_uuid; attr_char_value.p_attr_md = &attr_md; // OUR_JOB: Step 2.H, Set characteristic length in number of bytes attr_char_value.max_len = 2; attr_char_value.init_len = 2; uint8_t value[2] = {0x00}; attr_char_value.p_value = value; // OUR_JOB: Step 2.E, Add our new characteristic to the service sd_ble_gatts_characteristic_add(p_our_service->service_handle, &char_md, &attr_char_value, &p_our_service->char_handles); return NRF_SUCCESS; }
Now here is my function to update the characteristic data.
void our_characteristic_update1( ble_os_t * p_our_service,int8_t *temperature_value) { // OUR_JOB: Step 3.E, Update characteristic value uint16_t len = 2; ble_gatts_hvx_params_t hvx_params; memset(&hvx_params, 0, sizeof(hvx_params)); hvx_params.handle = p_our_service->char_handles.value_handle; hvx_params.type = BLE_GATT_HVX_NOTIFICATION; hvx_params.offset = 0; hvx_params.p_len = &len; hvx_params.p_data = temperature_value; sd_ble_gatts_hvx(p_our_service->conn_handle, &hvx_params); }
But here my problem is in 1st code block if I am setting the attr_char_value.max_len or attr_char_value.init_len =2. Then in 2nd code block, I have to set uint16_t len = 2 so similarly if I set 1st code block attr_char_value.max_len or attr_char_value.init_len =20 I have to set in 2nd code block, I have to set uint16_t len = 20. If it is so, then If I need to write 20 bytes and I am expecting to have 5 bytes response then also I am getting those 5 bytes+ remaining last 15 bytes data in App.
For example, supporting I am writing "HELLO" in the data characteristic and then expecting a response in the update data characteristic is "HI". Then, current I am getting updated data characteristics as ""HILLO".
How can I set dynamic length while I am updating data characteristics?
Note: I am not using ble_nus.h library.
Is it possible to have the required length using sd_ble_gatts_hvx() function? or what is my mistake in above code?
Thanks and regards,
Neeraj Dhekale