我是嵌入式系统新手。我正在尝试增加 ble 外围链接的数量。
static uint16_t m_connection_handle[NRF_SDH_BLE_PERIPHERAL_LINK_COUNT] = {BLE_CONN_HANDLE_INVALID};
static uint8_t m_num_connections = 0;
static void on_advertising_events(ble_adv_evt_t advertising_event)
{
NRF_LOG_DEBUG("on_advertising_events - event: %d", advertising_event);
ret_code_t err_code;
switch (advertising_event)
{
case BLE_ADV_EVT_FAST:
NRF_LOG_INFO("Fast advertising.");
#ifdef BOARD_PCA10040
err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
APP_ERROR_CHECK(err_code);
#endif
break;
case BLE_ADV_EVT_IDLE:
if (m_num_connections < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT)
{
err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
}
break;
default:
break;
}
}
static void on_ble_events(ble_evt_t const * p_ble_event, void * p_context)
{
NRF_LOG_DEBUG("on_ble_events - event: %d", p_ble_event->header.evt_id);
ret_code_t err_code = NRF_SUCCESS;
switch (p_ble_event->header.evt_id)
{
case BLE_GAP_EVT_DISCONNECTED:
NRF_LOG_INFO("Disconnected - Reason: %d", p_ble_event->evt.gap_evt.params.disconnected.reason);
m_mtu_exchanged = false;
break;
case BLE_GAP_EVT_CONNECTED:
{
NRF_LOG_INFO("Connected.");
#if BOARD_PCA10040
err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
APP_ERROR_CHECK(err_code);
#endif
if (m_num_connections < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT){
m_connection_handle[m_num_connections++] = p_ble_event->evt.gap_evt.conn_handle;
}
// Trigger the MTU exchange timer
NRF_LOG_INFO("Trigger MTU exchange timer.");
if (pdPASS != xTimerStart(m_mtu_exchange_timer, OSTIMER_WAIT_FOR_QUEUE))
{
NRF_LOG_WARNING("Failed to start MTU exchange timer");
}
NRF_LOG_DEBUG("### Configuration:");
NRF_LOG_DEBUG("### Connection Interval: %d (ms)", MAX_CONN_INTERVAL * 1.25);
NRF_LOG_DEBUG("### Connection Event Length: %d (ms)", NRF_SDH_BLE_GAP_EVENT_LENGTH * 1.25);
NRF_LOG_DEBUG("### Connection Event Length Extension: %d", 0);
NRF_LOG_DEBUG("### ATT MTU: %d", NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
NRF_LOG_DEBUG("### Data Length: %d", NRF_SDH_BLE_GAP_DATA_LENGTH);
} 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_event->evt.gap_evt.conn_handle, &phys);
APP_ERROR_CHECK(err_code);
} break;
case BLE_GATTC_EVT_TIMEOUT:
// Disconnect on GATT Client timeout event.
NRF_LOG_DEBUG("GATT Client Timeout.");
err_code = sd_ble_gap_disconnect(p_ble_event->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.
NRF_LOG_DEBUG("GATT Server Timeout.");
err_code = sd_ble_gap_disconnect(p_ble_event->evt.gatts_evt.conn_handle,
BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
break;
case BLE_GAP_EVT_CONN_PARAM_UPDATE:
{
NRF_LOG_DEBUG("BLE_GAP_EVT_CONN_PARAM_UPDATE");
ble_gap_conn_params_t const * p_connection = &p_ble_event->evt.gap_evt.params.conn_param_update.conn_params;
NRF_LOG_DEBUG("max_conn_interval: %d ms", p_connection->max_conn_interval * 1.25);
NRF_LOG_DEBUG("min_conn_interval: %d ms", p_connection->min_conn_interval * 1.25);
NRF_LOG_DEBUG("slave_latency: %d", p_connection->slave_latency);
NRF_LOG_DEBUG("conn_sup_timeout: %d ms", p_connection->conn_sup_timeout * 10);
} break;
default:
// No implementation needed.
break;
}
}
static void disconnect(uint16_t connection_handle, void * p_context)
{
UNUSED_PARAMETER(p_context);
ret_code_t err_code = sd_ble_gap_disconnect(connection_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (err_code != NRF_SUCCESS)
{
NRF_LOG_WARNING("Failed to disconnect connection. Connection handle: %d Error: %d", connection_handle, err_code);
}
else
{
NRF_LOG_DEBUG("Disconnected connection handle %d", connection_handle);
// Find and remove the connection handle from the array
for (int i = 0; i < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT; ++i)
{
if (m_connection_handle[i] == connection_handle)
{
m_connection_handle[i] = BLE_CONN_HANDLE_INVALID;
m_num_connections--;
break;
}
}
// Restart advertising if necessary
if (m_num_connections < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT)
{
start_ble_advertising(NULL); // Adjust parameters as needed
}
}
}
我增加了北欧站点上可用的 sdk.config 文件中的 nrf_sdh_ble_peripheral_link_count ,但这会导致我的设备在数量增加时根本不做广告。我还需要注意哪些其他事项?
我可以看到您正在使用旧版本的软件设备。对于这个旧版本,需要多个步骤来增加外围设备的数量。首先,您需要按照您所说的那样增加 SDK_config 文件中的 NRF_SDH_BLE_PERIPHERAL_LINK_COUNT 定义。然后,您需要通过 sd_enable() 函数增加对软设备的内存分配,如此处所示。
如果问题仍然存在,我建议联系 DevZone 上的 Nordic 支持人员,因为他们能够提供帮助。另外,如果可能的话,我建议将您的项目迁移到 Zephyr/nRF Connect SDK,因为 Nordic Softdevice 不再开发。
一些相关链接:-