<?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>General question about services, characteristics, BLE connection</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/11710/general-question-about-services-characteristics-ble-connection</link><description>Hello, i recently followed your tutorial nAN-36 to understand how to add a custom service to my project. I managed to do this and i also created three characteristics to control RGB led on my board. However now i have some, i think, basic questions, but</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 17 Feb 2016 15:46:23 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/11710/general-question-about-services-characteristics-ble-connection" /><item><title>RE: General question about services, characteristics, BLE connection</title><link>https://devzone.nordicsemi.com/thread/44280?ContentTypeID=1</link><pubDate>Wed, 17 Feb 2016 15:46:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:128ac04f-298c-42c3-8ff0-e84f88ce512f</guid><dc:creator>Artucas</dc:creator><description>&lt;p&gt;You make things so clear Terje, thank you very much. Regards.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: General question about services, characteristics, BLE connection</title><link>https://devzone.nordicsemi.com/thread/44279?ContentTypeID=1</link><pubDate>Wed, 17 Feb 2016 14:57:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46f0c512-0b0c-4613-947c-0299cdbbd930</guid><dc:creator>tesc</dc:creator><description>&lt;p&gt;Yes, the characteristic value can be of several bytes. For characteristics that are part of the GATT specifications the size is part of that specification (the format is known). For custom characteristics you can rely on the format being known in both ends. You can also add a characteristic presentation format descriptor which describes the format of the value, but I do not know much about that descriptor nor how widely it is actually used. Others may be able to fill you in on this.&lt;/p&gt;
&lt;p&gt;Basically what you suggest is correct, but be aware that if you store the value in application memory then that memory allocation must be adjusted accordingly. For the LBS examples the value is in SoftDevice memory (&lt;code&gt;cccd_md.vloc = BLE_GATTS_VLOC_STACK;&lt;/code&gt;) which means what you wrote above is all that is needed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: General question about services, characteristics, BLE connection</title><link>https://devzone.nordicsemi.com/thread/44278?ContentTypeID=1</link><pubDate>Tue, 09 Feb 2016 14:09:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:19bfedaa-b108-4f32-8a17-2b4417254592</guid><dc:creator>Artucas</dc:creator><description>&lt;p&gt;Wow Terje, thank you for such a good update on my questions. You made things a lot clearer. Im marking  this question as answered, thank you very much for your support. However if you could also answer the last question, even in the comments, or you could maybe update the 2 question answer it would be fantastic. So we are dealing with characteristics, and one characteristic can have multiple bytes, not a single byte. Correct? In ble_app_lbs example, led_char size is ony byte? So can i make a characteristic value larger? if yes, what are the limits of it(5,10,20 bytes for a single characteristic? ) and is it this piece of code in lbs_example that controls char size?&lt;/p&gt;
&lt;p&gt;attr_char_value.init_len     = sizeof(uint8_t);&lt;/p&gt;
&lt;p&gt;attr_char_value.init_offs    = 0;&lt;/p&gt;
&lt;p&gt;attr_char_value.max_len      = sizeof(uint8_t);&lt;/p&gt;
&lt;p&gt;attr_char_value.p_value      = NULL;&lt;/p&gt;
&lt;p&gt;Thank you very much for your support Terje, Regards.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: General question about services, characteristics, BLE connection</title><link>https://devzone.nordicsemi.com/thread/44277?ContentTypeID=1</link><pubDate>Fri, 05 Feb 2016 16:28:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8fb7c874-2e2e-4f83-9092-10455c316ba0</guid><dc:creator>tesc</dc:creator><description>&lt;p&gt;It was nothing wrong with the language. The problem was that the questions were short and general. They could have been related to almost anything. Thank you for making things clear. I will address your questions and update my initial answer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: General question about services, characteristics, BLE connection</title><link>https://devzone.nordicsemi.com/thread/44276?ContentTypeID=1</link><pubDate>Fri, 05 Feb 2016 16:19:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ce1f9237-af4e-4e5b-b5a9-ebd16b6d5e3f</guid><dc:creator>Artucas</dc:creator><description>&lt;p&gt;And sorry for the second comment, but didn&amp;#39;t have enough space to put everything in one. For the question number 4: I was playing with &amp;quot;ble_app_hrs&amp;quot; example, using NRF51 Dongle, i modified it a little bit and created a service to blink RGB LED. When i connect to it with my phone, other phones doesn&amp;#39;t find my Dongle anymore. I think what really happens is that dongle just stops advertising. So my question basically was: is there an example, where my nrf51 dongle wouldn&amp;#39;t stop advertising after i connect to it?  And i would be able to connect to my dongle let&amp;#39;s say with Three or Four phones? Thank you very much for your help. Regards.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: General question about services, characteristics, BLE connection</title><link>https://devzone.nordicsemi.com/thread/44275?ContentTypeID=1</link><pubDate>Fri, 05 Feb 2016 16:13:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:435220c4-18b3-4255-90a8-0450b6e7c802</guid><dc:creator>Artucas</dc:creator><description>&lt;p&gt;Thank you very much for your answer. You helped me a lot here. Sorry that you didn&amp;#39;t understand my questions. My english isn&amp;#39;t very good :), so i am trying to elaborate what i want to to ask:&lt;/p&gt;
&lt;p&gt;2)I was following nAN-36 and &amp;quot;ble_app_hrs&amp;quot; for custom service creation. So i am creating service with my 128-bit UUID, and i am adding one Characteristic. So i need a handler for that. In nAN-36 it is shown like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;typedef void (*ble_blinky_led_write_handler_t) (ble_blinky_t * p_blinky, uint8_t new_state);

typedef struct
{
    ble_blinky_led_write_handler_t led_write_handler; 
} ble_blinky_init_t;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then i am adding this handler to the service, initializing in the main and adding a handler code. This handler code is executed when i write something to this characteristic. Correct? So i wanted to ask can i add a second Characteristic to that service, and handle writes to it with same handler?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: General question about services, characteristics, BLE connection</title><link>https://devzone.nordicsemi.com/thread/44274?ContentTypeID=1</link><pubDate>Fri, 05 Feb 2016 14:55:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:41797039-6ca0-4590-b309-79fd39591ae4</guid><dc:creator>tesc</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Limits to advertising custom UUIDs&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It is correct that advertising packets and scan response packets have a maximum payload of 31 bytes each. A custom service has a 16 bytes adress, so the maths should be obvious. You will not have space for advertising more than one custom service in one packet.&lt;/p&gt;
&lt;p&gt;However, it is perfectly valid to provide services that are not advertised. They will still be found during service discovery after connecting. In fact, there are no requirements to advertise any service at all. (But it would still be a good idea, so that centrals know that it is worth connecting.)&lt;/p&gt;
&lt;ol start="2"&gt;
&lt;li&gt;Write handler for characteristic&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I certainly understand that this area can be a little confusing. Sorry for such a long answer, I have tried to give a thorough answer, yet as short as possbile.&lt;/p&gt;
&lt;p&gt;The program flow of the LED Button Service (peripheral blinky) example on a BLE GATTS write event is as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A BLE event occurs. The SoftDevice calls the &lt;code&gt;ble_evt_dispatch()&lt;/code&gt; callback function, which was initialised in &lt;code&gt;ble_stack_init()&lt;/code&gt; using the &lt;code&gt;softdevice_ble_evt_handler_set(ble_evt_dispatch)&lt;/code&gt; SoftDevice call. &lt;code&gt;ble_evt_dispatch()&lt;/code&gt; is the main BLE event handler for your application, and it is called whenever a BLE event occurs from the SoftDevice.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ble_evt_dispatch()&lt;/code&gt; calls &lt;code&gt;ble_lbs_on_ble_evt()&lt;/code&gt;, with a pointer to the static structure containing state information for the service, &lt;code&gt;&amp;amp;m_lbs&lt;/code&gt;, and a pointer to the BLE event from the SoftDevice, &lt;code&gt;p_ble_evt&lt;/code&gt;. &lt;code&gt;ble_lbs_on_ble_evt()&lt;/code&gt; is the dispatch function for the LED Button Service, and it is responsible for handling all BLE events related to this service.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ble_lbs_on_ble_evt()&lt;/code&gt; uses a &lt;code&gt;switch&lt;/code&gt; to filter on the &lt;code&gt;BLE_GATTS_EVT_WRITE&lt;/code&gt; event and in that case call &lt;code&gt;on_write()&lt;/code&gt;, still with a pointer to the&lt;code&gt;lbs&lt;/code&gt;structure and &lt;code&gt;p_ble_evt&lt;/code&gt; as parameters. &lt;code&gt;on_write&lt;/code&gt; is a &lt;code&gt;static&lt;/code&gt; function in the LED Button Service module responsible for GATTS write event handling for that module.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;on_write()&lt;/code&gt; checks if the attribute handle from the BLE event equals the attribute handle of the value of the LED characteristic (as well as write data length and that a handler exists.) It then calls the &lt;code&gt;led_write_handler&lt;/code&gt; with the pointer to the &lt;code&gt;lbs&lt;/code&gt; data structure and the write value (&lt;code&gt;led_state&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;led_write_handler&lt;/code&gt; simply turns the LED on or off depending on the value provided in &lt;code&gt;led_state&lt;/code&gt;. It is defined in &lt;code&gt;main.c&lt;/code&gt;, and it is initialised to be the write handler of the &lt;code&gt;m_lbs&lt;/code&gt;structure by being set in the &lt;code&gt;ble_lbs_init_t&lt;/code&gt; structure sent to &lt;code&gt;ble_lbs_init()&lt;/code&gt; in &lt;code&gt;services_init()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To sum up: SoftDevice -&amp;gt; &lt;code&gt;ble_evt_dispatch(ble_evt)&lt;/code&gt; -&amp;gt; &lt;code&gt;ble_lbs_on_ble_evt(lbs, ble_evt)&lt;/code&gt; -&amp;gt; &lt;code&gt;on_write(lbs, ble_evt)&lt;/code&gt; -&amp;gt; &lt;code&gt;led_write_handler(lbs, value)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now what to do to add a characteristic to the LED Button Service. You still want to send all BLE events to &lt;code&gt;ble_lbs_on_ble_evt()&lt;/code&gt; and to handle &lt;code&gt;BLE_GATTS_EVT_WRITE&lt;/code&gt; in &lt;code&gt;on_write()&lt;/code&gt;, so you should leave &lt;code&gt;ble_evt_dispatch()&lt;/code&gt; and &lt;code&gt;ble_lbs_on_ble_evt()&lt;/code&gt; as is.&lt;/p&gt;
&lt;p&gt;A few changes are needed in &lt;code&gt;on_write&lt;/code&gt; and &lt;code&gt;led_write_handler&lt;/code&gt;. Currently &lt;code&gt;on_write&lt;/code&gt; checks to see if the handle is correct, i.e. if the characteristic value that has been written to is the characteristic value of the LED characteristic. Now you must check for the attribute handles of both characteristics. To prepare for this, the &lt;code&gt;ble_lbs_s&lt;/code&gt; structure (this is the &lt;code&gt;lbs&lt;/code&gt; structure) must be extended. Previously it had a &lt;code&gt;ble_gatts_char_handles_t&lt;/code&gt; structure for the LED characteristic, but it will now need another one for the new characteristic. Further, &lt;code&gt;m_lbs&lt;/code&gt; must be populated with the handle values for the new characteristic (and indeed, a new characteristic must be set up). I will leave these details as an exercise for the reader.&lt;/p&gt;
&lt;p&gt;So now you should be able to check for both characteristics. You will then have to choose if the same handler can be used for both characteristics (i.e. it does the same or a similar action for both characteristics). If there are two LEDs, for instance, then it could make sense to use the same handler and to use an additional parameter for what LED to set. In &lt;code&gt;on_write()&lt;/code&gt; you would check the characteristic value and determine what the LED parameter should be for the handler call.&lt;/p&gt;
&lt;p&gt;If the characteristics are not similar, you should create a new handler for the new functionality related to the new characteristic, and do the required changes to &lt;code&gt;ble_lbs_s&lt;/code&gt;, &lt;code&gt;ble_lbs_init_t&lt;/code&gt;, initialisation function &amp;amp;c. using the same pattern as for the existing LED characteristic. The job of &lt;code&gt;on_write()&lt;/code&gt; would then be to choose what handler to call based on what characteristic value has been written.&lt;/p&gt;
&lt;ol start="3"&gt;
&lt;li&gt;Examples of adding a descriptor to a characteristic&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Adding characteristics and descriptors uses the &lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.s110.api.v8.0.0%2Fgroup___b_l_e___g_a_t_t_s___f_u_n_c_t_i_o_n_s.html&amp;amp;cp=2_7_0_0_0_2_3_5"&gt;GATT server API&lt;/a&gt; of the SoftDevice, and discovering/reading uses the &lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.s110.api.v8.0.0%2Fgroup___b_l_e___g_a_t_t_c___f_u_n_c_t_i_o_n_s.html&amp;amp;cp=2_7_0_0_0_2_2_4"&gt;GATT client API&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The SDK examples do not set up characteristic descriptors directly, but if you have a look at the source for the &lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk51.v10.0.0%2Fservice_battery.html&amp;amp;cp=4_0_1_3_3_1"&gt;Battery Service module&lt;/a&gt; you will find that it uses &lt;code&gt;sd_ble_gatts_descriptor_add()&lt;/code&gt; to add a descriptor at the bottom of &lt;code&gt;battery_level_char_add()&lt;/code&gt;. In &lt;a href="http://developer.nordicsemi.com/nRF5_SDK/nRF51_SDK_v10.x.x/"&gt;SDK 10&lt;/a&gt; this is in &lt;code&gt;&amp;lt;SDK folder&amp;gt;\components\ble\ble_services\ble_bas\ble_bas.c&lt;/code&gt;, line 214. Two more uses can be found in &lt;code&gt;(...)\ble_services\ble_hids\ble_hids.c&lt;/code&gt;, which is the &lt;a href="http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk51.v10.0.0%2Fservice_hid.html&amp;amp;cp=4_0_1_3_3_10"&gt;Human Interface Device Service module&lt;/a&gt;.&lt;/p&gt;
&lt;ol start="4"&gt;
&lt;li&gt;Peripheral connected to multiple centrals&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;None of the current SoftDevices supports concurrent connections to more than one central. This means you must end the current connection before connecting to a new central. You can bond with several centrals, which basically means you have performed a handshake and will recognise the central on future connection requests. Bonding can be performed when in a connection. You can however combine the (connected) peripheral role with the (non-connectable advertising) broadcast role.&lt;/p&gt;
&lt;p&gt;Regards, Terje&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>