Sending Data as soon as Bluetooth Connection is Established

We have been working with the UART central and peripheral examples 'ble_app_uart_c_pca10056_s140' and 'ble_app_uart_pca10056_s140' from the SDK 17.0.1. The examples use Nordic's nrf serial terminal to communicate between the boards. In the serial terminal, the boards send data strings to each other, but we automated this with a fixed set of strings which the boards send to each other. We changed the p.data which stores the received string from serial terminal to a predefined string and are able to successfully test this. In order to automate the process we tried using app_uart_put('\n') in the ble_evt_handler() function to trigger the transmission of data.  The problem we are facing now is trying to automate the transmission of data strings without requiring an entry to be made on serial terminal by using app_uart_put('\n') when the Bluetooth connection is established. This is not working properly so is there any advice on how to trigger the transmission of data. 

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
uint32_t err_code;
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
flag = true;
NRF_LOG_INFO("Connected");
err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
APP_ERROR_CHECK(err_code);
m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
APP_ERROR_CHECK(err_code);
break;

case BLE_GAP_EVT_DISCONNECTED:
NRF_LOG_INFO("Disconnected");
// LED indication will be changed when advertising starts.
m_conn_handle = BLE_CONN_HANDLE_INVALID;
flag = false;
break;

case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
{
NRF_LOG_DEBUG("PHY update request.");
ble_gap_phys_t const phys =
{
.rx_phys = BLE_GAP_PHY_AUTO,
.tx_phys = BLE_GAP_PHY_AUTO,
};
err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
APP_ERROR_CHECK(err_code);
} break;

case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
// Pairing not supported
err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
APP_ERROR_CHECK(err_code);
break;

case BLE_GATTS_EVT_SYS_ATTR_MISSING:
// No system attributes have been stored.
err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
APP_ERROR_CHECK(err_code);
break;

case BLE_GATTC_EVT_TIMEOUT:
// Disconnect on GATT Client timeout event.
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
break;

case BLE_GATTS_EVT_TIMEOUT:
// Disconnect on GATT Server timeout event.
err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
break;

default:
// No implementation needed.
break;
}

if (flag == true && count == 0)
{
app_uart_put('\n');
count = 1;
}

}

void uart_event_handle(app_uart_evt_t * p_event)
{

static uint8_t index = 0;
uint32_t err_code;

if (count == 1)
{
send_fixed_string_over_ble();
count = 2;
}

switch (p_event->evt_type)
{
case APP_UART_DATA_READY:
break;

case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;

case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}

static void send_fixed_string_over_ble(void)
{
uint32_t err_code;
static uint8_t fixed_string[] = "ECE";
uint16_t string_length = sizeof(fixed_string) - 1; // Exclude null terminator

// Send the fixed string over BLE using NUS
//err_code = ble_nus_data_send(&m_nus, fixed_string, &string_length, m_conn_handle);
err_code = ble_nus_data_send(&m_nus, fixed_string, &string_length, m_conn_handle);

if ((err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_NOT_FOUND))
{
APP_ERROR_CHECK(err_code);
}

}

Related