I have a central device (nRF52832/Soft device s132) that connects to two peripherals. Both peripherals send data to the central via NUS. I have incorporated the required changes for the NUS_C by refering to the LBS_C service from multi-link central example.
Declaration:
BLE_NUS_C_ARRAY_DEF(m_ble_nus_c, NRF_SDH_BLE_CENTRAL_LINK_COUNT); BLE_DB_DISCOVERY_ARRAY_DEF(m_db_disc, NRF_SDH_BLE_CENTRAL_LINK_COUNT);
Initialization:
static void nus_c_init(void) {
ret_code_t err_code;
ble_nus_c_init_t init;
init.evt_handler = ble_nus_c_evt_handler;
for (uint32_t i = 0; i < NRF_SDH_BLE_CENTRAL_LINK_COUNT; i++)
{
err_code = ble_nus_c_init(&m_ble_nus_c[i], &init);
APP_ERROR_CHECK(err_code);
}
}
static void db_disc_handler(ble_db_discovery_evt_t *p_evt) {
ble_nus_c_on_db_disc_evt(&m_ble_nus_c[p_evt->conn_handle], p_evt);
}
Handler function:
static void ble_nus_c_evt_handler(ble_nus_c_t *p_ble_nus_c, ble_nus_c_evt_t const *p_ble_nus_evt) {
ret_code_t err_code;
switch (p_ble_nus_evt->evt_type) {
case BLE_NUS_C_EVT_DISCOVERY_COMPLETE:
err_code = ble_nus_c_handles_assign(p_ble_nus_c, p_ble_nus_evt->conn_handle, &p_ble_nus_evt->handles);
APP_ERROR_CHECK(err_code);
err_code = ble_nus_c_tx_notif_enable(p_ble_nus_c);
APP_ERROR_CHECK(err_code);
break;
case BLE_NUS_C_EVT_NUS_TX_EVT:
ble_nus_chars_received_uart_print(p_ble_nus_evt->p_data, p_ble_nus_evt->data_len);
break;
case BLE_NUS_C_EVT_DISCONNECTED:
NRF_LOG_INFO("Disconnected.");
break;
default:
break;
}
}
The snippet from the ble_evt_handler:
case BLE_GAP_EVT_CONNECTED:
// Discover peer's services.
err_code = ble_nus_c_handles_assign(&m_ble_nus_c[p_gap_evt->conn_handle], p_ble_evt->evt.gap_evt.conn_handle, NULL);
APP_ERROR_CHECK(err_code);
err_code = ble_db_discovery_start(&m_db_disc[p_gap_evt->conn_handle], p_ble_evt->evt.gap_evt.conn_handle);
APP_ERROR_CHECK(err_code);
break;
}
I can see both the peripherals getting connected. Both peripherals stream data from on-board sensors distinguishable from the sensor Id. The issue is, I see the data only from the peripheral that connects first. If peripheral 1 connects first, i only see the data from peripheral 1. If peripheral 2 connects first then I see the data only from peripheral 2. In the function:
static void ble_nus_c_evt_handler(ble_nus_c_t *p_ble_nus_c, ble_nus_c_evt_t const *p_ble_nus_evt)
I am printing the address of the p_ble_nus_c using:
NRF_LOG_INFO("nus instance = %u.", p_ble_nus_c);
When the nus handler runs for the case BLE_NUS_C_EVT_DISCOVERY_COMPLETE, I see two different instances, like these -
nus instance = 536900620 - from peripheral1, and
nus instance = 536900636 - from peripheral2
But when the nus handler runs for the case BLE_NUS_C_EVT_NUS_TX_EVT, I only see
nus instance = 536900620 - which is the peripheral that was connected first. Is it possible to stream data from both the peripherals simultaneously? Is there something I am missing?