I'm not sure if what I am trying to do is the proper way to send ble data, and I since I can't get it to work, I'm assuming I'm doing it wrong. What I'm trying to do is send data to a BLE central device (my phone) using a peripheral. after initializing everything I can send and receive data from a serial port monitor just fine. The code below is what grabs the serial port data and sends it to the central device.
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] == '\n') || (data_array[index - 1] == '\r') || (index >= m_ble_nus_max_data_len)) { if (index > 1) { printf("\n\n\n\rReady to send data over BLE NUS."); NRF_LOG_DEBUG("Ready to send data over BLE NUS"); printf("\n\rm_conn_handle = %d", m_conn_handle); NRF_LOG_HEXDUMP_DEBUG(data_array, index); do { uint16_t length = (uint16_t)index; err_code = ble_nus_data_send(&m_nus, data_array, &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); } } while (err_code == NRF_ERROR_RESOURCES); } index = 0; } 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; } }
I thought that I could take the same concept to send data from the main method. after initializing, I put the following code, which (I thought) sent "Hello\n" every 2.5 seconds. instead, "m_conn_handle" = 0xFFFF and it never transmits data. NOTE: I am connected to the device via my phone which I thought should change that value to 0x0000. I know the code is rough but I just wanted to make sure I could send data correctly before I went any further. And I can't.
while (1) { static uint8_t data_array[BLE_NUS_MAX_DATA_LEN]; data_array[0] = 'H'; data_array[1] = 'e'; data_array[2] = 'l'; data_array[3] = 'l'; data_array[4] = 'o'; data_array[5] = '\n'; ret_code_t err_code = 0; uint8_t index = 6; printf("\n\rm_conn_handle = %d", m_conn_handle); if ((data_array[index - 1] == '\n') || (data_array[index - 1] == '\r') || (index >= m_ble_nus_max_data_len)) { if (index > 1) { printf("\n\rGonna broadcast now!"); NRF_LOG_DEBUG("Gonna broadcast now?"); NRF_LOG_HEXDUMP_DEBUG(data_array, index); do { uint16_t length = (uint16_t)index; err_code = ble_nus_data_send(&m_nus, data_array, &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); } } while (err_code == NRF_ERROR_RESOURCES); } index = 0; } nrf_delay_ms(2500); }
If anyone knows what I need to do to get it to send data, that would be greatly appreciated.
On a similar note, I am able to process that data from the central that I am receiving on the peripheral using the static void nus_data_handler(ble_nus_evt_t *pevt) function as follows.
static void nus_data_handler(ble_nus_evt_t *p_evt) { if (p_evt->type == BLE_NUS_EVT_RX_DATA) { if(p_evt->params.rx_data.p_data[0] == 'V') { uint16_t value = p_evt->params.rx_data.p_data[1] << 8 | p_evt->params.rx_data.p_data[2]; printf("\n\rSetting Value to %d\n\r", value); write_value(value); } } }
Is this the proper way to read the value being received? It works fine, but that doesn't mean I'm doing it right.