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?

  • If you use BLE UART Service on GATT Server side (which sends GATT HVX notifications to transport data asynchronously) then Notifications must be enabled prior to send any data through. Connection handle just indicates if connection is established on LL.

    On BLE_GAP_EVT_CONNECTED the GATT notifications on NUS can be enabled only if you use pairing/bonding. the it means that packet should really get through BLE link and if the other side isn't ready (which can be problem of lower BT stack - especially on buggy systems such as Android devices or various Windows PCs) there is no way to find out on any layer managed by BLE stack (= bellow APP layer). So the debug sequence could look like this:

    1. Make sure if you use pairing/bonding.
    2. Take BLE sniffer and verify in trace that Handle Value Notification gets through when connection is established.
    3. If so then you probably need to wait for some signal on APP layer, in other words you should start whole sequence from GAP Central/GATT Client side by sending some applicative message which arrives in GATT Write command and once your embedded app on top of nRF5x HW and Nordic Soft Device stack receives it you are safe to send data out.
  • 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!

Related