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

BLE_NUS_EVT_COMM_STARTED not sent

Hi,

I have been using the NUS service via the ble_app_uart example on SDK 16 in conjunction with the nRF Connect iOS app and all works fine.

I connect via the iOS app and when I select the notify button on the iOS app corresponding to the UART TX Characteristic, I get the BLE_NUS_EVT_COMM_STARTED notification.

At this point, I can send and receive data and when I send data,  I get the BLE_NUS_EVT_TX_RDY notification to indicate the send completed.

However, when I port the example code into my own nRF52 app,  I do not get the BLE_NUS_EVT_COMM_STARTED notification.  I can receive data from the iOS app but when I try to send I get NRF_ERROR_INVALID_STATE from sd_ble_gatts_hvx() in ble_nus_data_send() which I guess is consistent with the NUS service not being ready.

The code appears identical but clearly I missing something - if anyone can suggest what I might have missed for the BLE_NUS_EVT_COMM_STARTED not to be sent, I would be very grateful...

static void nus_data_handler(ble_nus_evt_t * p_evt)
{

   char rxData[20] = {0};

   bleNUS_TX_RDY = false;

   //NRF_LOG_INFO("Received event from NUS %d\n",p_evt->type);

   switch(p_evt->type)
   {
      case BLE_NUS_EVT_COMM_STARTED:
          NRF_LOG_INFO("BLE_NUS_EVT_COMM_STARTED.\n");
          break;
      case BLE_NUS_EVT_COMM_STOPPED:
          NRF_LOG_INFO("BLE_NUS_EVT_COMM_STOPPED.\n");
          break;
      case BLE_NUS_EVT_TX_RDY:
          NRF_LOG_INFO("BLE_NUS_EVT_TX_RDY.\n");
          bleNUS_TX_RDY = true;
          break;
      case BLE_NUS_EVT_RX_DATA:
          NRF_LOG_INFO("BLE_NUS_EVT_RX_DATA.\n");
          NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
          memcpy(rxData,p_evt->params.rx_data.p_data,p_evt->params.rx_data.length);
          NRF_LOG_INFO("rxData >%s<\n",rxData);
          printf("rxData >%s<\n",rxData);          
          break;
      default:
          NRF_LOG_INFO("RX default %d\n",p_evt->type);
          break;
    }
}


Log from working example code:
app: Debug logging for UART over RTT started. app: Connected app: Data len is set to 0xB6(182) app: ATT MTU exchange completed. central 0xF7 peripheral 0xF7 app: PHY update request. app: ATT MTU exchange completed. central 0xF7 peripheral 0xF7 app: BLE_NUS_EVT_COMM_STARTED.

Log from same function ported to my app:
 app: Connection 0x1 established
 app: Data len is set to 0xB6(182)
 app: ATT MTU exchange completed. Central 0xF7 Peripheral 0xF7
 app: PHY update request.
 app: ATT MTU exchange completed. Central 0xF7 Peripheral 0xF7


Parents
  • Well, you can't send notifications because it is not enabled. I assume that you have enabled notifications from the central (phone?) when you use your custom app as well? What is your central? A phone or another nRF?

    Can you compare the on_write() function in ble_nus.c and see why the BLE_NUS_EVT_COMM_STARTED is not generated with your custom app, but it is with the ble_app_uart example?

    BR,

    Edvin

  • Interestingly, I am getting the events in the on_write() that correspond to BLE_NUS_EVT_COMM_STARTED and BLE_NUS_EVT_COMM_STOPPED but p_client == NULL for these events so the events are not registered correctly.

  • Ah, the issue is in blcm_link_ctx_get() I am getting a NRF_ERROR_NO_MEM which seems to be related to 

    p_link_ctx_storage->max_links_cnt

    I guess I need to increase some memory some place...
  • Does your on_write() function start like this?

    static void on_write(ble_nus_t * p_nus, ble_evt_t const * p_ble_evt)
    {
        ret_code_t                    err_code;
        ble_nus_evt_t                 evt;
        ble_nus_client_context_t    * p_client;
        ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    
        err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage,
                                     p_ble_evt->evt.gatts_evt.conn_handle,
                                     (void *) &p_client);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
                          p_ble_evt->evt.gatts_evt.conn_handle);
        }
    
        memset(&evt, 0, sizeof(ble_nus_evt_t));
        evt.p_nus       = p_nus;
        evt.conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
        evt.p_link_ctx  = p_client;
    
        if ((p_evt_write->handle == p_nus->tx_handles.cccd_handle) &&
            (p_evt_write->len == 2))
        {
            if (p_client != NULL)
            {

    And p_client is not set to something other than NULL after

        err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage,
                                     p_ble_evt->evt.gatts_evt.conn_handle,
                                     (void *) &p_client);

    ?

    What does blcm_link_ctx_get() return? That is, what is err_code after this call?

    Be aware that on_write is called in other events as well. Not necessarily only when enabling notifications, and not necessarily only for events in the ble_nus events, but if you write something to one of the characteristics from your central the on_write event will also be triggered. Make sure that you check the event when you actually enable notifications.

    BR,

    Edvin

  • Edvin,

    I'm pretty certain the event is the BLE_NUS_EVT_COMM_STARTED as it is triggered via nRF Connect app button and same action when connected to sample code ble_app_uart gives the BLE_NUS_EVT_COMM_STARTED event.

    My on_write() looks same as yours and the error code after call to blcm_link_ctx_get() is 4.

    I did try increasing NRF_CLI_BLE_UART_MAX_CLIENTS to 8 (other forum post) but this made no difference....

    Any suggestions?

    Nick

  • Debug Output on ble_app_uart:
     ble_nus: on_write: event received from BLE stack.
    
     ble_nus: on_write: BLE_NUS_EVT_COMM_STARTED event received.
    
     app: BLE_NUS_EVT_COMM_STARTED.
    
     ble_nus: on_write: event received from BLE stack.
    
     ble_nus: on_write: BLE_NUS_EVT_COMM_STOPPED event received.
    
     app: BLE_NUS_EVT_COMM_STOPPED.

    Debug on custom app:

     ble_nus: on_write: event from BlE stack
    
     ble_nus: Link context for 0x01 connection handle could not be fetched.
    
     ble_nus: Link context error 0x04
    
     ble_nus: on_write: event from BlE stack
    
     ble_nus: Link context for 0x01 connection handle could not be fetched.
    
     ble_nus: Link context error 0x04
    
    
Reply
  • Debug Output on ble_app_uart:
     ble_nus: on_write: event received from BLE stack.
    
     ble_nus: on_write: BLE_NUS_EVT_COMM_STARTED event received.
    
     app: BLE_NUS_EVT_COMM_STARTED.
    
     ble_nus: on_write: event received from BLE stack.
    
     ble_nus: on_write: BLE_NUS_EVT_COMM_STOPPED event received.
    
     app: BLE_NUS_EVT_COMM_STOPPED.

    Debug on custom app:

     ble_nus: on_write: event from BlE stack
    
     ble_nus: Link context for 0x01 connection handle could not be fetched.
    
     ble_nus: Link context error 0x04
    
     ble_nus: on_write: event from BlE stack
    
     ble_nus: Link context for 0x01 connection handle could not be fetched.
    
     ble_nus: Link context error 0x04
    
    
Children
Related