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

Updating variable in custom service characteristic

(/attachment/fad3a1e781331c528a9db48ec177b9b7)Hi there! i need help. I am using NRF52Dk with Bluetooth developmebt studio. I set up my custom service in BDS an then imported in Keil project. Now i have service with my characteristic. I can see it in nrf tool box. And write and read my value by smartphone. Also i can read it on write event nrf52 in code function (ble_myprofile.c) :

static void on_write(ble_myprofile_t * p_myprofile, ble_gatts_evt_write_t * p_ble_evt)
{

if(p_ble_evt->handle == p_myprofile->mycharacteristic_handles.cccd_handle)
{
if(p_myprofile->evt_handler != NULL)
{
ble_myprofile_evt_t evt;
evt.evt_type = BLE_MYPROFILE_MYCHARACTERISTIC_EVT_CCCD_WRITE;
bds_uint16_decode(p_ble_evt->len, p_ble_evt->data, &evt.params.cccd_value);
p_myprofile->evt_handler(p_myprofile, &evt);
}
} 
if(p_ble_evt->handle == p_myprofile->mycharacteristic_handles.value_handle)
{
if(p_myprofile->evt_handler != NULL)
{
ble_myprofile_evt_t evt;
evt.evt_type = BLE_MYPROFILE_MYCHARACTERISTIC_EVT_WRITE;
mycharacteristic_decode(p_ble_evt->len, p_ble_evt->data, &evt.params.mycharacteristic);
p_myprofile->evt_handler(p_myprofile, &evt);
}
}
}

But how i can to write a new value (update value: data) in my characteristic in Keil source code? I tried to make a function as in devzone example (devzone.nordicsemi.com/.../) but it has no effect...

uint32_t ble_my_service_value_update(ble_myprofile_t * p_myprofile, uint8_t data)
{
uint32_t err_code = NRF_SUCCESS;

ble_myprofile_evt_t ble_myprofile_evt;
ble_gatts_value_t gatts_value; 
ble_myprofile_t ble_myprofile;
ble_myprofile_mycharacteristic_t * p_mycharacteristic;

memset(&gatts_value, 0, sizeof(gatts_value));
gatts_value.len = sizeof(uint8_t);
gatts_value.offset = 0;
gatts_value.p_value = &data;
err_code = sd_ble_gatts_value_set(p_myprofile->conn_handle, 
p_myprofile->mycharacteristic_handles.value_handle, 
&gatts_value);

if (p_myprofile->conn_handle != BLE_CONN_HANDLE_INVALID)
{
ble_gatts_hvx_params_t hvx_params;
// Initialize value struct.
memset(&hvx_params, 0, sizeof(hvx_params));

hvx_params.handle = p_myprofile->mycharacteristic_handles.value_handle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = gatts_value.offset;
hvx_params.p_len = &gatts_value.len;
hvx_params.p_data = gatts_value.p_value;
err_code = sd_ble_gatts_hvx(p_myprofile->conn_handle, &hvx_params);
}
return err_code;
}

What i have to do to update val in code? I attached my project for nrf52 in .rar file experimental_bluetoothds_template.rar ble_myprofile.c service_if.c ble_myprofile.h service_if.h

Related