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

Send data with BLE

Hi everyone,

I'm working with nrf52832 and i'm trying to send data over BLE for communicate with a mobile app.
I saw that we could pass data with the function ble_nus_string_send but I can not send data of more than 61 bytes, it is limited automatically...

Do you have a solution ?

Thanks

Parents
  • Hi,

    The maximum length of data (in bytes) that can be transmitted, is limited by ATT MTU - 3(header). This is by default 20, but can be negotiated higher if the phone support it. The desired ATT MTU is set to 64 in the gatt_init() function in main.c. The maximum length will therefore be 61 bytes(if the phone support it).

    If you want to send more data than this, you will need to split the data up, and call ble_nus_string_send() several times. This is how it is done in the uart_event_handle() function in main.c, where the data is sent when it exceed the maximum length(m_ble_nus_max_data_len), or if a new-line character is received.

    Snippet from uart_event_handle() :

    case APP_UART_DATA_READY:
    	UNUSED_VARIABLE(app_uart_get(&data_array[index]));
    	index++;
    
    	if ((data_array[index - 1] == '\n') || (index >= (m_ble_nus_max_data_len)))
    	{
    		NRF_LOG_DEBUG("Ready to send data over BLE NUS");
    		NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
    		do
    		{
    			uint16_t length = (uint16_t)index;
    			err_code = ble_nus_string_send(&m_nus, data_array, &length);
    			if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) )
    			{
    				APP_ERROR_CHECK(err_code);
    			}
    		} while (err_code == NRF_ERROR_BUSY);
    
    		index = 0;
    	}
    	break;

Reply
  • Hi,

    The maximum length of data (in bytes) that can be transmitted, is limited by ATT MTU - 3(header). This is by default 20, but can be negotiated higher if the phone support it. The desired ATT MTU is set to 64 in the gatt_init() function in main.c. The maximum length will therefore be 61 bytes(if the phone support it).

    If you want to send more data than this, you will need to split the data up, and call ble_nus_string_send() several times. This is how it is done in the uart_event_handle() function in main.c, where the data is sent when it exceed the maximum length(m_ble_nus_max_data_len), or if a new-line character is received.

    Snippet from uart_event_handle() :

    case APP_UART_DATA_READY:
    	UNUSED_VARIABLE(app_uart_get(&data_array[index]));
    	index++;
    
    	if ((data_array[index - 1] == '\n') || (index >= (m_ble_nus_max_data_len)))
    	{
    		NRF_LOG_DEBUG("Ready to send data over BLE NUS");
    		NRF_LOG_HEXDUMP_DEBUG(data_array, index);
    
    		do
    		{
    			uint16_t length = (uint16_t)index;
    			err_code = ble_nus_string_send(&m_nus, data_array, &length);
    			if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) )
    			{
    				APP_ERROR_CHECK(err_code);
    			}
    		} while (err_code == NRF_ERROR_BUSY);
    
    		index = 0;
    	}
    	break;

Children
Related