<?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>Can I have multiple but distinct BLE event handlers</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/59103/can-i-have-multiple-but-distinct-ble-event-handlers</link><description>I have a multi peripheral sample I am trying to make work 
 I do a 
 BLE_NUS_DEF(m_nus) and BLE_HRS_DEF(m_hrs) 
 this will register two event handlers. 
 When an event occurs, say a connection to HRS, I do see the events in both handlers. Is is possible</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 20 Mar 2020 00:18:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/59103/can-i-have-multiple-but-distinct-ble-event-handlers" /><item><title>RE: Can I have multiple but distinct BLE event handlers</title><link>https://devzone.nordicsemi.com/thread/240736?ContentTypeID=1</link><pubDate>Fri, 20 Mar 2020 00:18:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:331bf23f-1f15-4157-b0b9-01d3f0e03eaf</guid><dc:creator>marcag</dc:creator><description>&lt;p&gt;thanks Edvin, your help has been greatly appreciated !!!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can I have multiple but distinct BLE event handlers</title><link>https://devzone.nordicsemi.com/thread/240256?ContentTypeID=1</link><pubDate>Tue, 17 Mar 2020 12:51:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a03f0bb2-0f22-486c-be94-03ac9377a519</guid><dc:creator>Edvin</dc:creator><description>[quote user="marcag"]I believe the SD provides the service handle of the event and it&amp;#39;s compared to the service handle obtained during nus_init. If they match the event is propogated to the app.&amp;nbsp;[/quote]
&lt;p&gt;&amp;nbsp;Correct.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="marcag"]In the opposite direction, when I send data&amp;nbsp;from the peripheral, a connection handle and &amp;quot;service handle obtained during nus_init&amp;quot; are passed to sd_ble_gatts_hvx. I suspect a check done there by getting the service handles from the connection handle and comparing them to &amp;quot;service handle obstained during NUS init&amp;quot;. If OK, generates a HVN_TX_COMPLETE.[/quote]
&lt;p&gt;&amp;nbsp;Also correct. Just be aware that BLE_GATTS_EVT_HVN_TX_COMPLETE will be issued after (!) a message is ACKed by the central. You can queue up several packets using sd_ble_gatts_hvx(), as long as it returns NRF_SUCCESS. If it return NRF_ERROR_RESOURCES, it means that the sending queue is full, and you must wait for a TX_COMPLETE in order to queue the next packet. Please note that the TX_COMPLETE doesn&amp;#39;t belong to a specific service. The&amp;nbsp;BLE_GATTS_EVT_HVN_TX_COMPLETE events are general, telling you that a message was ACKed. But then again, all the services share the same queue.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I believe this is why the&amp;nbsp;BLE_NUS_EVT_TX_RDY isn&amp;#39;t really used in main.c in this example. It is probably more common to use the BLE_GATTS_EVT_HVN_TX_COMPLETE event directly in main.c, but it is up to you.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can I have multiple but distinct BLE event handlers</title><link>https://devzone.nordicsemi.com/thread/240249?ContentTypeID=1</link><pubDate>Tue, 17 Mar 2020 12:30:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ddc56cce-c9ef-4373-8865-a8f2ba39ee94</guid><dc:creator>marcag</dc:creator><description>&lt;p&gt;Thanks. Agreed. Correct me where I am wrong&lt;/p&gt;
&lt;p&gt;What you described is when&amp;nbsp;something comes FROM central. I see a&amp;nbsp;BLE_GATTS_EVT_WRITE come in and get sent down to ble_nus_on_ble_evt.&lt;/p&gt;
&lt;p&gt;onWrite gets called and a check of two handles occurs. I believe the SD provides the service handle of the event and it&amp;#39;s compared to the service handle obtained during nus_init. If they match the event is propogated to the app.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the opposite direction, when I send data&amp;nbsp;from the peripheral, a connection handle and &amp;quot;service handle obtained during nus_init&amp;quot; are passed to sd_ble_gatts_hvx. I suspect a check done there by getting the service handles from the connection handle and comparing them to &amp;quot;service handle obstained during NUS init&amp;quot;. If OK, generates a HVN_TX_COMPLETE. Otherwise fails in sd_ble_gatts_hvx. So this is done is the SD. I believe&lt;/p&gt;
&lt;p&gt;If I am correct, the key is having the service handles associated to the connection handle to do the checks. These service handles&amp;nbsp;are there for some events (like a WRITE) but not other (on connect) for example.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Correct ?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can I have multiple but distinct BLE event handlers</title><link>https://devzone.nordicsemi.com/thread/240162?ContentTypeID=1</link><pubDate>Tue, 17 Mar 2020 07:56:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fbee5307-7642-4f87-acf1-ce6c81a62d6e</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Please open ble_nus.c and look at the on_write() function. Look at where it is called (inside&amp;nbsp;ble_nus_on_ble_evt()), and what it does. It is all in the source code. The event comes from the softdevice, but the checks and the forwarding of the event is all in the code (not the softdevice).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can I have multiple but distinct BLE event handlers</title><link>https://devzone.nordicsemi.com/thread/240063?ContentTypeID=1</link><pubDate>Mon, 16 Mar 2020 14:52:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e2bd79ea-40e0-4165-b282-fd60c210bc2d</guid><dc:creator>marcag</dc:creator><description>&lt;p&gt;thanks&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I think I am getting closer and I am trying to read and understand. This could help me :&lt;/p&gt;
&lt;p&gt;When you say &amp;quot;&lt;span&gt;Then it checks wether the event has the NUS service&amp;#39;s service_handle&amp;quot;, what specific code does this ?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I *think* this check is done in the softdevice ? Can you confirm how it&amp;#39;s done.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;thanks&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can I have multiple but distinct BLE event handlers</title><link>https://devzone.nordicsemi.com/thread/239971?ContentTypeID=1</link><pubDate>Mon, 16 Mar 2020 11:33:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:60f0bc9d-2ebc-4b7b-bab1-b89650c1cfaf</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Depends on how you trigger the custom event handlers. If you look in the ble_app_uart_example, you can see that the&amp;nbsp;BLE_NUS_EVT_RX_DATA event is one event in the&amp;nbsp;nus_data_handler(). However, this event is not defined by the softdevice. The event comes from the BLE_GATTS_EVT_WRITE event in the NUS&amp;#39; ble_handler in ble_nus.c:&lt;/p&gt;
&lt;p&gt;ble_nus_on_ble_evt() -&amp;gt;&amp;nbsp;BLE_GATTS_EVT_WRITE -&amp;gt; on_write() -&amp;gt;&lt;/p&gt;
&lt;p&gt;Then it checks wether the event has the NUS service&amp;#39;s service_handle. If that is true, then it will generate the event BLE_NUS_EVT_RX_DATA on line 171-175 (line numbers from SDK16) in ble_nus.c:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;        evt.type                  = BLE_NUS_EVT_RX_DATA;
        evt.params.rx_data.p_data = p_evt_write-&amp;gt;data;
        evt.params.rx_data.length = p_evt_write-&amp;gt;len;

        p_nus-&amp;gt;data_handler(&amp;amp;evt);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Please read through the Step 8 on the tutorial. It has a more thorough walkthrough of this procedure.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can I have multiple but distinct BLE event handlers</title><link>https://devzone.nordicsemi.com/thread/239966?ContentTypeID=1</link><pubDate>Mon, 16 Mar 2020 11:18:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3c364232-f47a-4510-9b02-628618a2018b</guid><dc:creator>marcag</dc:creator><description>&lt;p&gt;Thanks Edwin&lt;/p&gt;
&lt;p&gt;When you say &amp;quot;all events propagate from the same BT event handler&amp;quot;. Understood.&lt;/p&gt;
&lt;p&gt;But my understanding is all event handlers will get even events from other sources. IE if I have a handler for NUS events and another for HRS events, each handler will get the other&amp;#39;s events. Correct ?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can I have multiple but distinct BLE event handlers</title><link>https://devzone.nordicsemi.com/thread/239937?ContentTypeID=1</link><pubDate>Mon, 16 Mar 2020 10:13:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:90c98ee4-dbb2-4fe5-8767-59589bb851d8</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Yes. This is possible, but please be aware that all the events propagate from the same bluetooth event handler.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Please see how the ble_app_uart example set up the nus event handler. Please note that it is not enough to set the&amp;nbsp;nus_init.data_handler = nus_data_handler; You also need to manually send the events from inside the ble event handler in ble_nus.c.&lt;/p&gt;
&lt;p&gt;If you are interrested, there is also a tutorial &lt;a href="https://github.com/edvinand/custom_ble_service_example" rel="noopener noreferrer" target="_blank"&gt;here&lt;/a&gt; that I have used in a couple of courses. It is rather long, but you can see in the section &amp;quot;Step 8 - Propagating Custom Service events to the application&amp;quot; how to send the events to a custom service event handler.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Please note that by default the HRS example doesn&amp;#39;t have an event handler in main.c. But it is possible to set it up, if you follow the same method as the nus data handler, or the tutorial from the link.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>