<?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>Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/61831/cycling-power-service-with-cscs-connection-handlers</link><description>Hello, 
 I&amp;#39;m getting err_code BLE_CONN_HANDLE_INVALID after calling ble_cps_measurement_send 
 
 I thought ble_cps_on_ble_evt was handling all of this but seems not. 
 
 I tried adding this line : 
 
 in ble_stack_init() but it didn&amp;#39;t work 
 
 I tried</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 29 May 2020 09:22:43 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/61831/cycling-power-service-with-cscs-connection-handlers" /><item><title>RE: Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/thread/252340?ContentTypeID=1</link><pubDate>Fri, 29 May 2020 09:22:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:855e0e7c-da70-4a8b-836d-0360e221f95e</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;So your ble_cps_on_ble_evt() doesn&amp;#39;t receive the&amp;nbsp;BLE_GAP_EVT_CONNECTED event?&lt;/p&gt;
&lt;p&gt;Look at the implementation of ble_nus.c:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void ble_nus_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
{
    if ((p_context == NULL) || (p_ble_evt == NULL))
    {
        return;
    }

    ble_nus_t * p_nus = (ble_nus_t *)p_context;

    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            on_connect(p_nus, p_ble_evt);
            break;

        case BLE_GATTS_EVT_WRITE:
            on_write(p_nus, p_ble_evt);
            break;

        case BLE_GATTS_EVT_HVN_TX_COMPLETE:
            on_hvx_tx_complete(p_nus, p_ble_evt);
            break;

        default:
            // No implementation needed.
            break;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;It looks pretty much like the one in your implementation. Compare your implementation with the unmodified ble_app_cscs example. You are probably missing something, or there is something wrong with your NRF_SDH_BLE_OBSERVER() implementation, or your cps_init() function.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I see that you have your cps event handler set to NULL. Make sure you don&amp;#39;t call this handler from your .._cps.c file as long as this is NULL.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t know why it doesn&amp;#39;t work. It is too much for me to manually go through. We have a working example, the cscs example, which looks quite a lot like this. Perhaps you can take this as a starting point, and modify it to your service.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/thread/252275?ContentTypeID=1</link><pubDate>Thu, 28 May 2020 17:32:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c3b7c0c-4e21-424c-934e-52b835fb56d5</guid><dc:creator>jjr</dc:creator><description>&lt;p&gt;Thank you so much for your feedback,&lt;/p&gt;
&lt;p&gt;I have been throught that tutorial and I think I found the issue ;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;pre class="ui-code" data-mode="c_cpp"&gt;void ble_cps_on_ble_evt(ble_cps_t * p_cps, ble_evt_t * p_ble_evt)
{   
   
    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
    case BLE_GAP_EVT_CONNECTED:
        NRF_LOG_INFO(&amp;quot;on connect&amp;quot;);
        on_connect(p_cps, p_ble_evt);
        break;

    case BLE_GAP_EVT_DISCONNECTED:    
        NRF_LOG_INFO(&amp;quot;on disconnect&amp;quot;);
        on_disconnect(p_cps, p_ble_evt);
        break;

    case BLE_GATTS_EVT_WRITE:
        NRF_LOG_INFO(&amp;quot;on write&amp;quot;);
        on_write(p_cps, p_ble_evt);
        break;

    default:
        // No implementation needed.
        NRF_LOG_INFO(&amp;quot;def&amp;quot;);
        break;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;will output :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/Screenshot-from-2020_2D00_05_2D00_28-13_2D00_21_2D00_54.png" /&gt;&lt;/p&gt;
&lt;p&gt;Note : &amp;quot;Connected&amp;quot; is not &amp;quot;on connect&amp;quot;... &amp;quot;Connected&amp;quot; is written somewhere else.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;so&amp;nbsp;p_ble_evt-&amp;gt;header.evt_id&amp;nbsp;doesnt have&amp;nbsp;the values&amp;nbsp;BLE_GAP_EVT_CONNECTED,&amp;nbsp;BLE_GAP_EVT_DISCONNECTED or&amp;nbsp;BLE_GATTS_EVT_WRITE when&amp;nbsp;ble_cps_on_ble_evt gets called.&lt;/p&gt;
&lt;p&gt;I know&amp;nbsp;&lt;span&gt;p_ble_evt-&amp;gt;header.evt_id does have the above value when&amp;nbsp;ble_evt_handler gets called&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Now I cant figure out why that is the case thought.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;this is my macro definition in ble_cp.h :&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define BLE_CPS_DEF(_name)                                                                         \
static ble_cps_t _name;                                                                            \
NRF_SDH_BLE_OBSERVER(_name ## _obs,                                                                 \
                     BLE_BPS_BLE_OBSERVER_PRIO,                                                    \
                     ble_cps_on_ble_evt, &amp;amp;_name)&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/thread/252254?ContentTypeID=1</link><pubDate>Thu, 28 May 2020 15:34:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3fa4fa1b-5a9d-487d-a97b-3d3fe37311ab</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Perhaps that event isn&amp;#39;t forwarded to the application properly. It does in some of the example services in our SDK, but not all. If the app timer isn&amp;#39;t started, it would suggest that it doesn&amp;#39;t.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Try to start the timer when you start the application and ignore the NRF_ERROR_INVALID_STATE, like I tried to show in the snippet.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t remember the details from my head, but you can check out the guide below, which shows how to send events to the main() file&amp;#39;s service event handler. It is not trivial, but if you want this, read through the guide, and make sure you understand what happens to your event handler in your cps file when you enable notifications.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Guide:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/edvinand/custom_ble_service_example"&gt;https://github.com/edvinand/custom_ble_service_example&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Step 8 and 9 are particularly relevant for you.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/thread/252248?ContentTypeID=1</link><pubDate>Thu, 28 May 2020 14:49:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ad3c0376-04d3-4d7e-8863-4e0814151308</guid><dc:creator>jjr</dc:creator><description>&lt;p&gt;So from what I understand,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;ble_cps_measurement_send() will start being called after the&amp;nbsp;app_timer_start(m_cps_timer_id, CPS_MEAS_INTERVAL, NULL) gets called.&lt;/p&gt;
&lt;p&gt;but since the notifications arent enabled from the central, it returns this invalid error statement.&lt;/p&gt;
&lt;p&gt;Enabling notifications from the central after the connection is established doesnt fix it... the &amp;quot;damage&amp;quot; was already done.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure I understand your recommendation for the fix. I tried to stop looking for NRF_ERROR_INVALID_STATE as err_code after ble_cps_measurement_send()... was that your proposition ? Sorry.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I had tried :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void cps_evt(ble_cps_t * p_cps, ble_cps_evt_t * p_evt){
        
    ret_code_t err_code;
    switch(p_evt-&amp;gt;evt_type)
    {
        case BLE_CPS_EVT_NOTIFICATION_ENABLED:
            err_code = app_timer_start(m_cps_timer_id, CPS_MEAS_INTERVAL, NULL);
            APP_ERROR_CHECK(err_code);
            break;
        
        case BLE_CPS_EVT_NOTIFICATION_DISABLED:
            err_code = app_timer_stop(m_cps_timer_id);
            APP_ERROR_CHECK(err_code);
            break;

        default:
            break;
    }
}


static void services_init(void){

...
cps_init.evt_handler = cps_evt;
...
}

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;But now the timer wont start after enabling notification from the central and&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;ble_cps_measurement_send() doesnt called at all anymore.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/thread/252231?ContentTypeID=1</link><pubDate>Thu, 28 May 2020 14:05:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:db12ee64-693b-4a7b-94f6-bca9a0618c7b</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;That means that you have not enabled notifications from your central yet. Try to do this. If you call ble_cps_measurement_send() before notifications are enabled from the central, you can ignore the return value NRF_ERROR_INVALID_STATE from ble_cps_measurement_send().&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;err_code = ble_cps_measurement_send()
if (err_code != NRF_ERROR_INVALID_STATE)
{
    APP_ERROR_CHECK(err_code);
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/thread/252205?ContentTypeID=1</link><pubDate>Thu, 28 May 2020 13:24:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2ea4b71f-736b-4ae4-b2ed-fac69901eb6b</guid><dc:creator>jjr</dc:creator><description>&lt;p&gt;it&amp;#39;s a good catch, I dont remember commenting it out.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Unfortunately it didnt fix the issue but now the error code I get at&amp;nbsp;&lt;span&gt;ble_cps_measurement_send() is&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;NRF_ERROR_INVALID_STATE&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/thread/252116?ContentTypeID=1</link><pubDate>Thu, 28 May 2020 09:15:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6c617d58-6918-4c22-a174-232d64ce82be</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Any reason why you commented out the line:&lt;/p&gt;
&lt;p&gt;//connection_params_init.start_on_notify_cccd_handle&amp;nbsp; &amp;nbsp; = m_cps.meas_handles.cccd_handle;&lt;/p&gt;
&lt;p&gt;in main.c? Does it work if you include that line again?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/thread/252001?ContentTypeID=1</link><pubDate>Wed, 27 May 2020 14:14:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:426bc76a-9ae4-41d9-b916-6a94231441eb</guid><dc:creator>jjr</dc:creator><description>&lt;p&gt;Hello, yes, ble_cps_measurement_send() is a custon function. I&amp;#39;m using SDK 160098a08e2 with softdevice s132.&lt;/p&gt;
&lt;p&gt;Running the unmodified&amp;nbsp;ble_app_cscs example runs perfectly.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I have included ble_cp.h and ble_cp.c if you want to reproduce the error.&lt;/p&gt;
&lt;p&gt;Thank you very much Edvin.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/2806.ble_5F00_cp.c"&gt;devzone.nordicsemi.com/.../2806.ble_5F00_cp.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/4555.ble_5F00_cp.h"&gt;devzone.nordicsemi.com/.../4555.ble_5F00_cp.h&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cycling Power Service with CSCS connection handlers</title><link>https://devzone.nordicsemi.com/thread/251961?ContentTypeID=1</link><pubDate>Wed, 27 May 2020 12:56:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:10c8e2dd-e424-442c-b2af-629380d5cf6e</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I can&amp;#39;t find&amp;nbsp;&lt;span&gt;ble_cps_measurement_send() in our SDK. Is this a custom function? If not, what SDK are you using?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Did you test the unmodified ble_app_rscs example? Does it behave the same?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>