<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>nRF51: writing to a characteristic</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/18871/nrf51-writing-to-a-characteristic</link><description>Hello, 
 I am using SD 130 on nRF5 with SDK V12.1.0. 
 I want to write to a characteristic using the android debug app (nrf connect). 
 This is my code for the characteristic: 
 ble_uuid_t char_uuid;
 char_uuid.uuid = BLE_UUID_MY_CHARACTERISTC_UUID;</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 10 Jan 2017 23:05:40 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/18871/nrf51-writing-to-a-characteristic" /><item><title>RE: nRF51: writing to a characteristic</title><link>https://devzone.nordicsemi.com/thread/72907?ContentTypeID=1</link><pubDate>Tue, 10 Jan 2017 23:05:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b469d1de-5575-40f5-9086-d4369163959b</guid><dc:creator>Michael Antwih</dc:creator><description>&lt;p&gt;Jorgen is correct. Sorry I overlook that aspect. Not sure why since I mentioned that the variable was on the stack by default. If you make the change as Jorgen has pointed out I don&amp;#39;t believe you will need to manage the variable explicitly. Just need to make sure MyVar buffer persist and can hold the data.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51: writing to a characteristic</title><link>https://devzone.nordicsemi.com/thread/72905?ContentTypeID=1</link><pubDate>Tue, 10 Jan 2017 09:28:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:545f842f-6a4e-4061-8308-dd17fccb3a50</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;You are setting the vloc attribute to &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.api.v3.0.0/group___b_l_e___g_a_t_t_s___v_l_o_c_s.html#gaf51ee5f68c7eea401ee79cd250c1fb16"&gt;&lt;code&gt;BLE_GATTS_VLOC_STACK&lt;/code&gt;&lt;/a&gt;. This will located the value in stack memory. Try changing this to &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.api.v3.0.0/group___b_l_e___g_a_t_t_s___v_l_o_c_s.html#gaddb51f959e18123c5e215e22af0c31a9"&gt;&lt;code&gt;BLE_GATTS_VLOC_USER&lt;/code&gt;&lt;/a&gt;, to locate the value in user memory. Notice that this requires you maintain a valid buffer through the lifetime of the attribute, since the stack will read and write directly to the memory using the pointer provided in the APIs.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51: writing to a characteristic</title><link>https://devzone.nordicsemi.com/thread/72904?ContentTypeID=1</link><pubDate>Tue, 10 Jan 2017 08:15:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a34c0ca3-90ae-459f-94b2-ac9e5781f0b2</guid><dc:creator>mr91</dc:creator><description>&lt;p&gt;Thank you very much for your answer. I made the mentioned changes and it works so far:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static void on_write(ble_soil_t * p_bas, ble_evt_t * p_ble_evt)
{
  ble_gatts_evt_write_t * p_evt_write = &amp;amp;p_ble_evt-&amp;gt;evt.gatts_evt.params.write;
  
  if (p_evt_write-&amp;gt;handle == p_bas-&amp;gt;char_handles_dry.value_handle)
  {  
    
    if(CALIB_TRIGGER == *p_evt_write-&amp;gt;data)
    {
      SoilMoistureThresholdDry = SoilMoistureValue;
    }
  }
           
  
}
void ble_soil_on_ble_evt(ble_soil_t * p_our_service, ble_evt_t * p_ble_evt)
{
  /* Connection status handle updaten*/
  switch (p_ble_evt-&amp;gt;header.evt_id)
  {
    case BLE_GAP_EVT_CONNECTED:
        p_our_service-&amp;gt;conn_handle = p_ble_evt-&amp;gt;evt.gap_evt.conn_handle;
        break;
    case BLE_GAP_EVT_DISCONNECTED:
        p_our_service-&amp;gt;conn_handle = BLE_CONN_HANDLE_INVALID;
        break;
    case BLE_GATTS_EVT_WRITE:
        on_write(p_our_service, p_ble_evt);
        break;
    default:
        // No implementation needed.
        break;
  }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;1. Question&lt;/strong&gt;
As you can see, I want the characteristic variable to be updated when a special number is written to it (calibration trigger, e.g. &amp;quot;1&amp;quot;). It works fine in the debugger, the variable is updated correctly (SoilMoistureThresholdDry is also the variable connected to the characteristic I have written to, it should work like this: write CALIB_TRIGGER to characteristic, but don&amp;#39;t update the connected variable with this value but update it with another value). But when I read the value again in NRF connect, it stills shows the value I have written before and not the updated value. I assume this has also todo with the stack. How can I tell the Softdevice to update the stack with the new value?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. Question&lt;/strong&gt;
When searching the forum I often found solutions which implemented a write_handler, e.g.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static void on_write(ble_dls_t * p_dls, ble_evt_t * p_ble_evt)
{
    ble_gatts_evt_write_t * p_evt_write = &amp;amp;p_ble_evt-&amp;gt;evt.gatts_evt.params.write;
        uint32_t temp_val = 0;

    if ((p_evt_write-&amp;gt;handle == p_dls-&amp;gt;dl_char_handles.value_handle) &amp;amp;&amp;amp;
        (p_evt_write-&amp;gt;len == 4) &amp;amp;&amp;amp;
        (p_dls-&amp;gt;dl_write_handler != NULL))
    {
                p_dls-&amp;gt;dl_write_handler(p_dls, p_evt_write);
    }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Can you tell me how to implement such a write handler?&lt;/p&gt;
&lt;p&gt;Thank you very much for your support!&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51: writing to a characteristic</title><link>https://devzone.nordicsemi.com/thread/72906?ContentTypeID=1</link><pubDate>Mon, 09 Jan 2017 20:09:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c5069dc7-8fb5-46ce-91ec-1806d38f9cee</guid><dc:creator>Michael Antwih</dc:creator><description>&lt;p&gt;The code you show above will just setup the characteristic. You need to handle the writing of the value in the on_write function which is normally called by the on ble evt handler. The value might be wriiten to the stack by default which might be why you are able to read it back correctly. If you handle the on write for your char then you should be able to set the value of myvar.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>