This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to stop characteristic notifications?

Hi

I would like to be able to (force) stop characteristic notifications at some point in time.

Imagine a smartphone connects to peripheral and enables characteristic notification for specific characteristic. Everything works fine but but in order to save energy I want to stop notifications if enabled and disconnect.

Hence, At app layer I have this code to stop notifications

err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);

But then, on function that send notifications to smartphone:

...

// Send value if connected and notifying.
    if ((p_cus->conn_handle != BLE_CONN_HANDLE_INVALID))
    {
        ble_gatts_hvx_params_t hvx_params;

        memset(&hvx_params, 0, sizeof(hvx_params));

        hvx_params.handle = p_cus->recoverdata_handles.value_handle;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
        hvx_params.offset = gatts_value.offset;
        hvx_params.p_len  = &gatts_value.len;
        hvx_params.p_data = gatts_value.p_value;

        err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params);
    }
    else
    {
        err_code = NRF_ERROR_INVALID_STATE;       
    }

    return err_code;

}

Then the execution stream reaches the else above and the firmware stops.

So, what is the right way to peacefully stop notifications from app layer (main.c)?

Thanks

Alex

Related