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

Parents
  • Hi,

    It seems that the issue is that you initialize the profile using the struct ble_myprofile_t m_myprofile;, defined in service_if.c. This is where the correct characteristics handles are stored. When you try to update the value, you define a new struct in main.c, ble_myprofile_t m_ble_myprofile;. This struct will not contain correct handles.

    To solve the issue, remove the definition of the struct from service_if.c, and add it in ble_myprofile.h:

    extern ble_myprofile_t    m_myprofile; 
    

    and in main.c:

    ble_myprofile_t    m_myprofile;
    

    Now you should be able to update the value in get_data_handler() using:

    ble_myprofile_mycharacteristic_set(&m_myprofile, &m_mycharacteristic);
    

    Best regards,

    Jørgen

Reply
  • Hi,

    It seems that the issue is that you initialize the profile using the struct ble_myprofile_t m_myprofile;, defined in service_if.c. This is where the correct characteristics handles are stored. When you try to update the value, you define a new struct in main.c, ble_myprofile_t m_ble_myprofile;. This struct will not contain correct handles.

    To solve the issue, remove the definition of the struct from service_if.c, and add it in ble_myprofile.h:

    extern ble_myprofile_t    m_myprofile; 
    

    and in main.c:

    ble_myprofile_t    m_myprofile;
    

    Now you should be able to update the value in get_data_handler() using:

    ble_myprofile_mycharacteristic_set(&m_myprofile, &m_mycharacteristic);
    

    Best regards,

    Jørgen

Children
Related