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

nrf51822 BLE disconnection problem

Hi, I'm using nrf51822 and ble_app_uart(SDK 12.3.0)

What I want to do is connect my board with nrf51822 and send data over UART

and send data to smartphone over BLE

UART data comes about 50bytes/sec

My problem is BLE connection is keep disconnected.

Actually, Hard to connect BLE at first. Sometimes do, Sometimes don't.

When it connected fortunately, It will disconnected soon.

Disconnecting time is unpredictable.

Sometimes smartphone receive data, sometimes don't.

I didn't changed sample code.

I'm new BLE, please help me..

Thanks!

void uart_event_handle(app_uart_evt_t * p_event){
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
static uint8_t index = 0;
uint32_t       err_code;

switch (p_event->evt_type)
{
    case APP_UART_DATA_READY:
        UNUSED_VARIABLE(app_uart_get(&data_array[index]));
        index++;
        if ((data_array[index - 1] == '\r') || (index >= (BLE_NUS_MAX_DATA_LEN)))
        {
            err_code = ble_nus_string_send(&m_nus, data_array, index);
					
            if (err_code != NRF_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }

image description

  • And I changed pca10028.h Pin number for wire UART CONNECTION

    #define RX_PIN_NUMBER  11
    #define TX_PIN_NUMBER  9
    
    to
    
    #define RX_PIN_NUMBER  5
    #define TX_PIN_NUMBER  6
    

    after I changed pin number to use wired UART, BLE disconnection problem happened. Before that, I used just keyboard input over USB-UART. It works fine.

  • So before you change the pins everything worked fine? Is this a custom board or a nRF51 DK? Are you sure that you don't get any error code somewhere? That resets the chip?

  • I changed pins to connect with my custom board and nrf51822(+ble400). Before that, I tested with my PC and nrf51822 using USB and BLE with my smartphone(android), and it worked fine. UART data comes every seconds. It can affect BLE connection? Or it could be android side problem? I'm using android app nRF UART v2.0 and nRF Toolbox but both have problem.

  • I found some details about this problem.

    When BLE disconnected, at this code

    err_code = ble_nus_string_send(&m_nus, data_array, index);

    NRF_ERROR_INVALID_STATE occurs.

    When it occurs, data didn't come to my android phone.

    When it didn't occur, data comes to my phone fine.

    Before NRF_ERROR_INVALID_STATE occurs,

    app_uart_init() function in app_uart_fifo.c called couple of times

    and soon BLE disconnected

  • In addition, in function ble_nus_string_send()

        if ((p_nus->conn_handle == BLE_CONN_HANDLE_INVALID) || (!p_nus->is_notification_enabled))
    {
    		SEGGER_RTT_WriteString(0, "NRF_ERROR_INVALID_STATE\n");
        return NRF_ERROR_INVALID_STATE;
    }
    

    NRF_ERROR_INVALID_STATE occurs because of BLE_CONN_HANDLE_INVALID

    And when BLE connected with my phone, at on_ble_evt() in main.c

    BLE_GAP_EVT_CONNECTED occurs.

    When BLE connection is fine, If I press disconnect button in android app to disconnect on purpose, BLE_GAP_EVT_DISCONNECTED occurs.

    But When BLE disconnected abnormally. BLE_GAP_EVT_DISCONNECTED didn't occurs.

Related