<?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>Automatically start notification upon connection event (manually write cccd?) - Short tutorial on notifications</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/15571/automatically-start-notification-upon-connection-event-manually-write-cccd---short-tutorial-on-notifications</link><description>Hi, 
 I want to implement a Peripheral (GATT Server) that automatically starts notification once a Central connects. No bonding is provided in our system (connection is between arbitrary devices). 
 I have read several times that notifications must</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 27 Aug 2019 02:12:53 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/15571/automatically-start-notification-upon-connection-event-manually-write-cccd---short-tutorial-on-notifications" /><item><title>RE: Automatically start notification upon connection event (manually write cccd?) - Short tutorial on notifications</title><link>https://devzone.nordicsemi.com/thread/206208?ContentTypeID=1</link><pubDate>Tue, 27 Aug 2019 02:12:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c70f497c-4628-4f67-a39d-33837ac0d0e9</guid><dc:creator>lydie</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I try to enable notification of CCCD to send data over Bluetooth from my device to my phone. So in the function on_connect, I enable notifications with this code&lt;pre class="ui-code" data-mode="c_cpp"&gt;    uint16_t buffer[4];
    buffer[0] = p_nus-&amp;gt;rx_handles.cccd_handle;                     //cccd handle
    buffer[1] = 2;                                                 //cccd attribute size = 2
    buffer[2] = 1;                                                  //1 = enable notifications
    buffer[3] = crc16_compute((uint8_t const*)buffer, 6, NULL);    //CR-CCITT 
    sd_ble_gatts_sys_attr_set(p_ble_evt-&amp;gt;evt.gap_evt.conn_handle, (uint8_t*)(buffer), 8, 0);   //initialize cccd attribute&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;but I still have the same mistake: NRF_ERROR_INVALID_STATE which means notification disabled. So why there are no enabled?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automatically start notification upon connection event (manually write cccd?) - Short tutorial on notifications</title><link>https://devzone.nordicsemi.com/thread/201969?ContentTypeID=1</link><pubDate>Thu, 01 Aug 2019 15:56:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0a7c41fb-b862-42ca-9f08-1e30b75156cd</guid><dc:creator>Bhushan P</dc:creator><description>&lt;p&gt;The cccd handle value in my case was 14. So it seems we need to calculate the CRC value in the application itself&amp;nbsp;as it may&amp;nbsp;vary.&amp;nbsp; CRC value can be calculated using crc16_compute( ) API. This API is provided by Nordic.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;
buffer[0] = p_nus-&amp;gt;tx_handles.cccd_handle;                  // cccd handle

buffer[1] = 2;                                              // cccd attribute size = 2

buffer[2] = 1;                                              // 1 = enable notifications

buffer[3] = crc16_compute((uint8_t const *)buffer,  6, NULL); // calculate CRC-CCITT (0xFFFF);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This will also take care of any change in the cccd handle value&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automatically start notification upon connection event (manually write cccd?) - Short tutorial on notifications</title><link>https://devzone.nordicsemi.com/thread/199349?ContentTypeID=1</link><pubDate>Thu, 18 Jul 2019 15:53:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:241be3d5-d016-444f-bf6e-8f6b55e13c70</guid><dc:creator>Prasad</dc:creator><description>&lt;p&gt;In hex enter 100002000100.&amp;nbsp;&lt;span&gt;CRC-CCITT should be&amp;nbsp;0xCACD.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Its in big endian format. All the three values are 16-bit so their big endian format is as follows.&lt;/p&gt;
&lt;p&gt;0010 = 1000&lt;/p&gt;
&lt;p&gt;0002 = 0200&lt;/p&gt;
&lt;p&gt;0001 = 0100&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automatically start notification upon connection event (manually write cccd?) - Short tutorial on notifications</title><link>https://devzone.nordicsemi.com/thread/132400?ContentTypeID=1</link><pubDate>Thu, 17 May 2018 10:16:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3625157e-a266-4da2-bd9c-512cd42ae6c4</guid><dc:creator>Muhammad Akram Karimi</dc:creator><description>&lt;p&gt;Dear Anders,&lt;/p&gt;
&lt;p&gt;I have the same question.&lt;/p&gt;
&lt;p&gt;Did you get the answer about CRC calculation?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automatically start notification upon connection event (manually write cccd?) - Short tutorial on notifications</title><link>https://devzone.nordicsemi.com/thread/59432?ContentTypeID=1</link><pubDate>Tue, 14 Nov 2017 08:39:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8081b3c2-b8e1-41d6-91ea-c8f627ab5f89</guid><dc:creator>Fanis</dc:creator><description>&lt;p&gt;Is there any way to &amp;quot;reset&amp;quot; this cccd information between connect sessions? I mean if disconnection occured while notification between bonded devices, on next connect not to automatically be subscribed on notifications.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automatically start notification upon connection event (manually write cccd?) - Short tutorial on notifications</title><link>https://devzone.nordicsemi.com/thread/59431?ContentTypeID=1</link><pubDate>Thu, 09 Feb 2017 13:58:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b7e1926d-401f-44e7-beb2-fa25fb84b388</guid><dc:creator>Anders Strand</dc:creator><description>&lt;p&gt;Hi. Could you explain how you entered the numbers 16,2 and 1 into the CRC calculator?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Automatically start notification upon connection event (manually write cccd?) - Short tutorial on notifications</title><link>https://devzone.nordicsemi.com/thread/59430?ContentTypeID=1</link><pubDate>Thu, 11 Aug 2016 11:43:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:311ee1a4-f789-4c08-a13f-352dde0d25d4</guid><dc:creator>puz_md</dc:creator><description>&lt;p&gt;Oookay, after spending a lot of time to get my communication flow work I would like to share my current knowledge about notifications with everyone.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How to enable notifications in the GATT Server upon connection establishment:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Use &lt;code&gt;sd_ble_gatts_sys_attr_set()&lt;/code&gt; when the connection is established (&lt;code&gt;BLE_GAP_EVT_CONNECTED&lt;/code&gt;) to initialize the related cccd values. The cccd cannot be written using the &lt;code&gt;sd_ble_gatts_value_set()&lt;/code&gt; function like I assumend before!&lt;/p&gt;
&lt;p&gt;As I couldn&amp;#39;t find any information about the syntax of the buffer handed over to &lt;code&gt;sd_ble_gatts_sys_attr_set()&lt;/code&gt;, I will give a short summary here. The buffer contains a list of 6 byte (interpreted as 3x uint16_t) long entries which contain cccd initialization data:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;uint16_t handle    // use the cccd handle contained in the handles structure when initalizing the characteristic.
uint16_t length    // lentgh of the attribute. Set this value to 2 as cccd attributes are uint16_t.
uint16_t data      // the content of the attribute. Set this value to 1 to enable notifications. Set it to 2 to enable indications (not tested).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;At the end of the list, a CRC16 value is added. You may use the following webpage to calculate the CRC (please use the CRC-CCITT (0xFFFF) value): &lt;a href="https://www.lammertbies.nl/comm/info/crc-calculation.html"&gt;CRC calculator&lt;/a&gt; - please remember to add the CRC bytes with LSB first.&lt;/p&gt;
&lt;p&gt;The following example enables notifications for the characteristic whose cccd handle is 16:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static void on_connect(ble_cbs_t* p_service, ble_evt_t* p_event)
{
    uint16_t buffer[4];
    conn_handle = p_event-&amp;gt;evt.gap_evt.conn_handle;    // save connection handle to global variable
    buffer[0] = 16;      // cccd handle
    buffer[1] = 2;       // cccd attribute size = 2
    buffer[2] = 1;       // 1 = enable notifications
    buffer[3] = 0xCACD;  // CRC-CCITT (0xFFFF)
    sd_ble_gatts_sys_attr_set(conn_handle, (uint8_t*)(buffer), 8, 0);    // initialize cccd attribute
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That&amp;#39;s it for the GATT Server side. If something doesn&amp;#39;t work, you may enable notifications with Master Control Panel and read the system attribute buffer using &lt;code&gt;sd_ble_gatts_sys_attr_get()&lt;/code&gt; in the disconnection event handler (in fact this is what I did to find out how this mechanism works). Also remember to configure the characteristic for notifications (set corresponding flag) when initializing your service in the SoftDevice.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How to receive notifications in the GATT Client:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;All we have to do is handle the &lt;code&gt;BLE_GATTC_EVT_HVX&lt;/code&gt; event. The notification data is contained in the event parameter structure.&lt;/p&gt;
&lt;p&gt;First I was confused why the data is not written to my characteristic variables that I use for manual read operations. Then I realized that characteristic values are only stored in the GATT Server and not in the GATT Client (the user is responsible for saving the data to local variables, if necessary).&lt;/p&gt;
&lt;p&gt;When I tried to invole &lt;code&gt;sd_ble_gattc_hv_confirm()&lt;/code&gt; in my notification event handler, I received &lt;code&gt;NRF_ERROR_INVALID_STATE&lt;/code&gt; as return code. I think this is because notifications do not use handshaking, so no confirmation packet can be sent. But it should be necessary to use this function when working with indications (that use handshaking).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Future things to find out:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;How to initiate notifications from a nRF GATT Client the official way. I know that the GATT Client is supposed to write the cccd attribute to 1, but do not know yet how to find out the cccs handle and which SoftDevice interface function shall be used to set the cccd to 1. I have read that some devices perform a complete discovery during which the cccd handle is obtained. Although I consider this procedure pretty inefficient (especially if I already know the UUID of the characteristic that I want to observe).&lt;/p&gt;
&lt;p&gt;I hope this short tutorial may help some of you to get notifications running faster.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>