<?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>I combined Bonding in the UART example, but after autoconnect, the notification is not enabled.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/51345/i-combined-bonding-in-the-uart-example-but-after-autoconnect-the-notification-is-not-enabled</link><description>I applied Bonding to the UART example(ble_app_uart). 
 Bonding combined with the UART example works well, but there is one problem. 
 1. I did Bonding first. 2. After that I set Notification Enable. 3. Rebooted the device and reconnected to the application</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 26 Aug 2019 11:50:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/51345/i-combined-bonding-in-the-uart-example-but-after-autoconnect-the-notification-is-not-enabled" /><item><title>RE: I combined Bonding in the UART example, but after autoconnect, the notification is not enabled.</title><link>https://devzone.nordicsemi.com/thread/206092?ContentTypeID=1</link><pubDate>Mon, 26 Aug 2019 11:50:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a00a4e96-a2f9-4a6a-bd71-3a38c1616f97</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Ok, then&amp;nbsp;if you want&amp;nbsp;&lt;em&gt;is_notification_enabled&lt;/em&gt; to be true after&amp;nbsp;auto-connect, then you need to implement the functionally shown in the snippet. You might also need to bond to the device for it to work.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I combined Bonding in the UART example, but after autoconnect, the notification is not enabled.</title><link>https://devzone.nordicsemi.com/thread/205918?ContentTypeID=1</link><pubDate>Fri, 23 Aug 2019 13:42:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0506500e-7439-4f2c-9b7f-ec67dddd621f</guid><dc:creator>alan.kim</dc:creator><description>&lt;p&gt;SDK 12.3, Softdevice is S130 2.0.1&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I combined Bonding in the UART example, but after autoconnect, the notification is not enabled.</title><link>https://devzone.nordicsemi.com/thread/205874?ContentTypeID=1</link><pubDate>Fri, 23 Aug 2019 11:34:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1b2358d1-5d4a-4537-9c03-647377144244</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;What SDK version is this ?&lt;/p&gt;
&lt;p&gt;In the latest SDK versions we read the CCCD value in the function on_connect() with sd_ble_gatts_value_get(), and then we update the variable &lt;em&gt;is_notification_enabled&lt;/em&gt;. Some SDKs might have a bug here though, ref &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/46904/nus-server-does-not-restore-notification-state/186038#186038"&gt;this post.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Snippet:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    /* Check the hosts CCCD value to inform of readiness to send data using the RX characteristic */
    memset(&amp;amp;gatts_val, 0, sizeof(ble_gatts_value_t));
    gatts_val.p_value = cccd_value;
    gatts_val.len     = sizeof(cccd_value);
    gatts_val.offset  = 0;

    err_code = sd_ble_gatts_value_get(p_ble_evt-&amp;gt;evt.gap_evt.conn_handle,
                                      p_nus-&amp;gt;rx_handles.cccd_handle,
                                      &amp;amp;gatts_val);

    if ((err_code == NRF_SUCCESS)     &amp;amp;&amp;amp;
        (p_nus-&amp;gt;data_handler != NULL) &amp;amp;&amp;amp;
        ble_srv_is_notification_enabled(gatts_val.p_value))
    {
        if (p_client != NULL)
        {
            p_client-&amp;gt;is_notification_enabled = true;
        }&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I combined Bonding in the UART example, but after autoconnect, the notification is not enabled.</title><link>https://devzone.nordicsemi.com/thread/205773?ContentTypeID=1</link><pubDate>Fri, 23 Aug 2019 07:56:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9c1566c-14b0-4ae0-a289-c2bd2d1c6e33</guid><dc:creator>alan.kim</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the S110 SoftDevice.
 *
 * @param[in] p_nus     Nordic UART Service structure.
 * @param[in] p_ble_evt Pointer to the event received from BLE stack.
 */
static void on_connect(ble_nus_t * p_nus, ble_evt_t * p_ble_evt)
{
    p_nus-&amp;gt;conn_handle = p_ble_evt-&amp;gt;evt.gap_evt.conn_handle;
}


/**@brief Function for handling the @ref BLE_GAP_EVT_DISCONNECTED event from the S110 SoftDevice.
 *
 * @param[in] p_nus     Nordic UART Service structure.
 * @param[in] p_ble_evt Pointer to the event received from BLE stack.
 */
static void on_disconnect(ble_nus_t * p_nus, ble_evt_t * p_ble_evt)
{
    UNUSED_PARAMETER(p_ble_evt);
    p_nus-&amp;gt;conn_handle = BLE_CONN_HANDLE_INVALID;
}


/**@brief Function for handling the @ref BLE_GATTS_EVT_WRITE event from the S110 SoftDevice.
 *
 * @param[in] p_nus     Nordic UART Service structure.
 * @param[in] p_ble_evt Pointer to the event received from BLE stack.
 */
static void on_write(ble_nus_t * p_nus, 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_nus-&amp;gt;rx_handles.cccd_handle)
        &amp;amp;&amp;amp;
        (p_evt_write-&amp;gt;len == 2)
       )
    {
        if (ble_srv_is_notification_enabled(p_evt_write-&amp;gt;data))
        {
            p_nus-&amp;gt;is_notification_enabled = true;
        }
        else
        {
            p_nus-&amp;gt;is_notification_enabled = false;
        }
    }
    else if (
             (p_evt_write-&amp;gt;handle == p_nus-&amp;gt;rx_handles.value_handle)
             &amp;amp;&amp;amp;
             (p_nus-&amp;gt;data_handler != NULL)
            )
    {
        p_nus-&amp;gt;data_handler(p_nus, p_evt_write-&amp;gt;data, p_evt_write-&amp;gt;len);
    }
    else
    {
        // Do Nothing. This event is not relevant for this service.
    }
}


/**@brief Function for adding RX characteristic.
 *
 * @param[in] p_nus       Nordic UART Service structure.
 * @param[in] p_nus_init  Information needed to initialize the service.
 *
 * @return NRF_SUCCESS on success, otherwise an error code.
 */
static uint32_t rx_char_add(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init)
{
    /**@snippet [Adding proprietary characteristic to S110 SoftDevice] */
    ble_gatts_char_md_t char_md;
    ble_gatts_attr_md_t cccd_md;
    ble_gatts_attr_t    attr_char_value;
    ble_uuid_t          ble_uuid;
    ble_gatts_attr_md_t attr_md;

    memset(&amp;amp;cccd_md, 0, sizeof(cccd_md));

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;cccd_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;cccd_md.write_perm);
	//cccd_md.write_perm = p_nus_init-&amp;gt;nus_attr_md.cccd_write_perm;

    cccd_md.vloc = BLE_GATTS_VLOC_STACK;

    memset(&amp;amp;char_md, 0, sizeof(char_md));

	char_md.char_props.write  = 1;
	char_md.char_props.write_wo_resp = 1;
    char_md.char_props.notify = 1;
    char_md.p_char_user_desc  = NULL;
    char_md.p_char_pf         = NULL;
    char_md.p_user_desc_md    = NULL;
    char_md.p_cccd_md         = &amp;amp;cccd_md;
    char_md.p_sccd_md         = NULL;

    ble_uuid.type = p_nus-&amp;gt;uuid_type;
    ble_uuid.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC;

    memset(&amp;amp;attr_md, 0, sizeof(attr_md));

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;attr_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&amp;amp;attr_md.write_perm);

    attr_md.vloc    = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth = 0;
    attr_md.wr_auth = 0;
    attr_md.vlen    = 1;

    memset(&amp;amp;attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid    = &amp;amp;ble_uuid;
    attr_char_value.p_attr_md = &amp;amp;attr_md;
    attr_char_value.init_len  = sizeof(uint8_t);
    attr_char_value.init_offs = 0;
    attr_char_value.max_len   = BLE_NUS_MAX_RX_CHAR_LEN;

    return sd_ble_gatts_characteristic_add(p_nus-&amp;gt;service_handle,
                                           &amp;amp;char_md,
                                           &amp;amp;attr_char_value,
                                           &amp;amp;p_nus-&amp;gt;rx_handles);
    /**@snippet [Adding proprietary characteristic to S110 SoftDevice] */
}



void ble_nus_on_ble_evt(ble_nus_t * p_nus, ble_evt_t * p_ble_evt)
{
    if ((p_nus == NULL) || (p_ble_evt == NULL))
    {
        return;
    }

    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            on_connect(p_nus, p_ble_evt);
            break;

        case BLE_GAP_EVT_DISCONNECTED:
            on_disconnect(p_nus, p_ble_evt);
            break;

        case BLE_GATTS_EVT_WRITE:
            on_write(p_nus, p_ble_evt);
            break;

        default:
            // No implementation needed.
            break;
    }
}


uint32_t ble_nus_init(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_init)
{
    uint32_t      err_code;
    ble_uuid_t    ble_uuid;
    ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;

    VERIFY_PARAM_NOT_NULL(p_nus);
    VERIFY_PARAM_NOT_NULL(p_nus_init);

    // Initialize the service structure.
    p_nus-&amp;gt;conn_handle             = BLE_CONN_HANDLE_INVALID;
    p_nus-&amp;gt;data_handler            = p_nus_init-&amp;gt;data_handler;
    p_nus-&amp;gt;is_notification_enabled = false;

    /**@snippet [Adding proprietary Service to S110 SoftDevice] */
    // Add a custom base UUID.
    err_code = sd_ble_uuid_vs_add(&amp;amp;nus_base_uuid, &amp;amp;p_nus-&amp;gt;uuid_type);
    VERIFY_SUCCESS(err_code);

    ble_uuid.type = p_nus-&amp;gt;uuid_type;
    ble_uuid.uuid = BLE_UUID_NUS_SERVICE;

    // Add the service.
    err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
                                        &amp;amp;ble_uuid,
                                        &amp;amp;p_nus-&amp;gt;service_handle);
    /**@snippet [Adding proprietary Service to S110 SoftDevice] */
    VERIFY_SUCCESS(err_code);

    // Add the RX Characteristic.
    err_code = rx_char_add(p_nus, p_nus_init);
    VERIFY_SUCCESS(err_code);

    return NRF_SUCCESS;
}


uint32_t ble_nus_string_send(ble_nus_t * p_nus, uint8_t * p_string, uint16_t length)
{
    ble_gatts_hvx_params_t hvx_params;

    VERIFY_PARAM_NOT_NULL(p_nus);

    if ((p_nus-&amp;gt;conn_handle == BLE_CONN_HANDLE_INVALID) || (!p_nus-&amp;gt;is_notification_enabled))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if (length &amp;gt; BLE_NUS_MAX_DATA_LEN)
    {
        return NRF_ERROR_INVALID_PARAM;
    }

    memset(&amp;amp;hvx_params, 0, sizeof(hvx_params));

    hvx_params.handle = p_nus-&amp;gt;rx_handles.value_handle;
    hvx_params.p_data = p_string;
    hvx_params.p_len  = &amp;amp;length;
    hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;

    return sd_ble_gatts_hvx(p_nus-&amp;gt;conn_handle, &amp;amp;hvx_params);
}

#endif // NRF_MODULE_ENABLED(BLE_NUS)
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;This is my code in ble_nus.c&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>