nrf52832 学习笔记(五)蓝牙主从机连接和连接参数更新

本文详细介绍了nRF52832芯片在蓝牙连接过程中的操作,包括如何作为主机使用sd_ble_gap_connect函数发起连接,以及SDK中的自动和手动连接功能。同时,讨论了MTU交换和连接参数更新的重要性,以及蓝牙5.0后的PHY物理层参数更新流程。内容涵盖了从连接初始化到连接维护的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nrf52832 学习笔记(五)蓝牙主从机连接和连接参数更新

主机连接

nrf52832 SDK中主机连接从机需要使用 sd_ble_gap_connect(ble_gap_addr_t const *p_peer_addr, ble_gap_scan_params_t const *p_scan_params, ble_gap_conn_params_t const *p_conn_params, uint8_t conn_cfg_tag)函数. 参数为目标MAC地址, 扫描参数, 连接参数, 连接配置标签, 这些参数均可以在扫描初始化参数部分获取.

/**@brief Create a connection (GAP Link Establishment).
 *
 * @note If a scanning procedure is currently in progress it will be automatically stopped when calling this function.
 *       The scanning procedure will be stopped even if the function returns an error.
 *
 * @events
 * @event{@ref BLE_GAP_EVT_CONNECTED, A connection was established.}
 * @event{@ref BLE_GAP_EVT_TIMEOUT, Failed to establish a connection.}
 * @endevents
 *
 * @mscs
 * @mmsc{@ref BLE_GAP_WL_SHARE_MSC}
 * @mmsc{@ref BLE_GAP_CENTRAL_CONN_PRIV_MSC}
 * @mmsc{@ref BLE_GAP_CENTRAL_CONN_MSC}
 * @endmscs
 *
 * @param[in] p_peer_addr   Pointer to peer identity address. If @ref ble_gap_scan_params_t::filter_policy is set to use
 *                          whitelist, then p_peer_addr is ignored.
 * @param[in] p_scan_params Pointer to scan parameters structure.
 * @param[in] p_conn_params Pointer to desired connection parameters.
 * @param[in] conn_cfg_tag  Tag identifying a configuration set by @ref sd_ble_cfg_set or
 *                          @ref BLE_CONN_CFG_TAG_DEFAULT to use the default connection configuration.
 *
 * @retval ::NRF_SUCCESS Successfully initiated connection procedure.
 * @retval ::NRF_ERROR_INVALID_ADDR Invalid parameter(s) pointer supplied.
 * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied.
 *                                   - Invalid parameter(s) in p_scan_params or p_conn_params.
 *                                   - Use of whitelist requested but whitelist has not been set, see @ref sd_ble_gap_whitelist_set.
 *                                   - Peer address was not present in the device identity list, see @ref sd_ble_gap_device_identities_set.
 * @retval ::NRF_ERROR_NOT_FOUND conn_cfg_tag not found.
 * @retval ::NRF_ERROR_INVALID_STATE The SoftDevice is in an invalid state to perform this operation. This may be due to an
 *                                   existing locally initiated connect procedure, which must complete before initiating again.
 * @retval ::BLE_ERROR_GAP_INVALID_BLE_ADDR Invalid Peer address.
 * @retval ::NRF_ERROR_CONN_COUNT The limit of available connections for this connection configuration tag has been reached.
 *                                To increase the number of available connections,
 *                                use @ref sd_ble_cfg_set with @ref BLE_GAP_CFG_ROLE_COUNT or @ref BLE_CONN_CFG_GAP.
 * @retval ::NRF_ERROR_RESOURCES Either:
 *                                 - Not enough BLE role slots available.
 *                                   Stop one or more currently active roles (Central, Peripheral or Observer) and try again.
 *                                 - The event_length parameter associated with conn_cfg_tag is too small to be able to
 *                                   establish a connection on the selected @ref ble_gap_scan_params_t::scan_phys.
 *                                   Use @ref sd_ble_cfg_set to increase the event length.
 * @retval ::NRF_ERROR_NOT_SUPPORTED Unsupported PHYs supplied to the call.
 */
SVCALL(SD_BLE_GAP_CONNECT, uint32_t, sd_ble_gap_connect(ble_gap_addr_t const *p_peer_addr, ble_gap_scan_params_t const *p_scan_params, ble_gap_conn_params_t const *p_conn_params, uint8_t conn_cfg_tag));

SDK自动连接

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值