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?