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

How to get the value from a WRITE_EVT into a variable.

I am trying to modify the ble_app_hrs so that i can write a value to the board and save it as a new heart rate value. So far i have added a BLE_GATTS_EVT_WRITE case to the event manager, as well as adding the specific handle i want the write value from.

How can i write the value given in the write event into the heart rate variable?

  • Hi Mathias,

    if I understand you correct, you just need to copy the value of the event/data.

    
    if (p_ble_evt->evt.gatts_evt.params.write.handle == my_handle)
    {
        memcpy(&destination, p_ble_evt->evt.gatts_evt.params.write.data, p_ble_evt->evt.gatts_evt.params.write.len);
    }
    
    

    if you would like to write to the server database, and store the value of the write there, then the best way to do that is to:

    
    uint16_t len = p_ble_evt->evt.gatts_evt.params.write.len;
    sd_ble_gatts_value_set(handle, 
                                           p_ble_evt->evt.gatts_evt.params.write.offset,
                                           &len,
                                           p_ble_evt->evt.gatts_evt.params.write.data);
    
    

    Hope this helps, BR Pål

  • Hi Mathias,

    If I write 123456 to device from Master Control Panel and want to get it ,then store it and display on the LCD .How can i achieve it?

    Thanks!

  • First you need to enable write permission to the service you want to write to. In the ble_app_hrs main.c method static void services_init(void) you can change BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_hrm_attr_md.read_perm); and BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&hrs_init.hrs_hrm_attr_md.write_perm); from _SET_NO_ACCES to _SET_OPEN. This should give you read/write permission to the heart-rate service.

    You then need to add this code snippet to the static void on_ble_evt(ble_evt_t * p_ble_evt) method as a case:

    case BLE_GATTS_EVT_WRITE: if(p_ble_evt->evt.gatts_evt.params.write.handle==yourhandle) { memcpy(&m_cur_heart_rate, p_ble_evt- >evt.gatts_evt.params.write.data, p_ble_evt->evt.gatts_evt.params.write.len);

    					}
    					break;
    

    where yourhandle is the handle of the heart rate measurement variable you can see in Master Control Panel on the heart rate service.

    From here you can use the variable to what you want.

    Hope this answers your question,

    Regards Mathias

Related