This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

When is NUS ready?

I'm using SDK 14.2 (not really relevant I guess) on an nRF52832. The application requires a greeting to be sent to the remote through NUS as soon as a connection is made. The first tranfers are always lost.

My code is as follows:

void hello_world( ble_nus_t *nus, char * str )
{
  uint16_t len = strlen( str );
  if ( nus->is_notification_enabled )
  {
    ble_nus_string_send( nus, (uint8_t *)str, (uint16_t *)&len );
  }
}

In main, conn_handle is defined and used to indicate a valid BLE_GAT_EVT_CONNECTED event was seen:

if ( m_conn_handle != BLE_CONN_HANDLE_INVALID )
{
  hello_world( &m_nus, "Hello, world\n" );
}

The "is_notification_enabled" field is maintained from ble_nus_on_ble_evt() in ble_nus.c, it should be reliable.

So, what is boils down to: how can I check if the remote is ready to receive data through NUS?

Parents
  • Hi

    As mentioned you can not send notifications until they are enabled by the client.

    When the notifications are enabled the service module (ble_nus.c) will alert the application to this by sending the BLE_NUS_EVT_COMM_STARTED event.

    This event is not handled by the example, but you can extend the nus_data_handler(ble_nus_evt_t * p_evt) function to handle it:

    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {    
        if (p_evt->type == BLE_NUS_EVT_RX_DATA)
        {
           ...
        }
        else if(p_evt->type == BLE_NUS_EVT_COMM_STARTED)
        {
          // Send welcome message here
        }
    }
    

    Best regards
    Torbjørn

  • That seems to do the trick, thanks!

Reply Children
No Data
Related