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自动连接

在nrf52832 学习笔记(四)蓝牙主机扫描中,扫描初始化时如果开启了软件过滤器,可以配置 init_scan.connect_if_match = true;开启自动连接功能.

/**@brief 扫描初始化 */
void scan_init(void)
{ret_code_t          err_code;nrf_ble_scan_init_t init_scan;memset(&init_scan, 0, sizeof(init_scan));init_scan.connect_if_match = true;   //如果开启过滤器,遇到匹配项是否直接进行连接init_scan.conn_cfg_tag     = APP_BLE_CONN_CFG_TAG;init_scan.p_scan_param     = &gap_scan;err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);APP_ERROR_CHECK(err_code);
}

当扫描到广播包后,在nrf_ble_scan.c 的观察者回调函数中,使用软件滤波器对广播包进行过滤,如果广播包通过过滤,则调用 sd_ble_gap_connect 函数连接从机.


手动连接

SDK中的软件滤波器可以适用于大部分情况,有时要连接的从机比较特殊,无法使用自动连接时就需要手动连接从机了.
在主机扫描到广播包后,协议栈观察者回调函数 BLE_GAP_EVT_ADV_REPORT 事件处理中,对广播包进行自定义分析处理,分析处理结束后,就可以调用 sd_ble_gap_connect 函数进行连接处理了.连接函数使用的参数均可以在扫描初始化部分找到.

/**@brief Function for establishing the connection with a device.** @details Connection is established if @ref NRF_BLE_SCAN_EVT_FILTER_MATCH*          or @ref NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT occurs and the module was*          initialized in the automatic connection mode. This function can generate an event*          to the main application when @ref sd_ble_gap_connect is used inside the function and it returns value*          that is different than @ref NRF_SUCCESS.** @param[in] p_scan_ctx   Pointer to the Scanning Module instance.* @param[in] p_adv_report Advertising data.*/
static void nrf_ble_scan_connect_with_target(nrf_ble_scan_t           const * const p_scan_ctx,ble_gap_evt_adv_report_t const * const p_adv_report)
{ret_code_t err_code;scan_evt_t scan_evt;// For readability.ble_gap_addr_t const        * p_addr        = &p_adv_report->peer_addr;ble_gap_scan_params_t const * p_scan_params = &p_scan_ctx->scan_params;ble_gap_conn_params_t const * p_conn_params = &p_scan_ctx->conn_params;uint8_t                       con_cfg_tag   = p_scan_ctx->conn_cfg_tag;// Return if the automatic connection is disabled.if (!p_scan_ctx->connect_if_match){return;}// Stop scanning.nrf_ble_scan_stop();memset(&scan_evt, 0, sizeof(scan_evt));// Establish connection.err_code = sd_ble_gap_connect(p_addr,p_scan_params,p_conn_params,con_cfg_tag);NRF_LOG_DEBUG("Connecting");scan_evt.scan_evt_id                    = NRF_BLE_SCAN_EVT_CONNECTING_ERROR;scan_evt.params.connecting_err.err_code = err_code;NRF_LOG_DEBUG("Connection status: %d", err_code);// If an error occurred, send an event to the event handler.if ((err_code != NRF_SUCCESS) && (p_scan_ctx->evt_handler != NULL)){p_scan_ctx->evt_handler(&scan_evt);}}
/**@brief Function for handling BLE events.** @param[in]   p_ble_evt   Bluetooth stack event.* @param[in]   p_context   Unused.*/
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{switch (p_ble_evt->header.evt_id){case BLE_GAP_EVT_ADV_REPORT://接收到广播{ble_gap_evt_adv_report_t const * p_adv_report = &p_ble_evt->evt.gap_evt.params.adv_report;{//                NRF_LOG_INFO("mac:"MACSTR, MAC2STR(p_adv_report->peer_addr.addr));
//                NRF_LOG_INFO(" rssi %d", p_adv_report->rssi);
//                NRF_LOG_INFO(" data len: %d", p_adv_report->data.len);}//自定义处理//......//连接从机nrf_ble_scan_connect_with_target(&m_scan, p_adv_report);break;}default:break;}
}

MTU交换

MTU全称为 Maximum Transmission Unit(最大传输单元),指在一个PDU(Protocol Data Unit 协议数据单元)中能传输的最大数据量.BLE4.0时 MTU为固定的23个字节,BLE4.2以后, MTU为可变的 23-247字节. 为了兼容之前的BLE4.0,蓝牙在连接后会进行MTU交换,主机告诉从机,主机的MTU是多少,从机再回复主机,从机的MTU是多少.然后主从机选择使用较小的MTU值.

由于蓝牙在通信时必须保证MTU一致,因此蓝牙连接后必须要先进行MTU交换. nrf52832 SDK中会自动完成MTU交换工作.
在GATT初始化时会注册一个GATT 观察者回调函数, 发生连接事件后会在该回调函数中进行MTU交换

连接参数更新

从机

在从机连接参数初始化时,从机会创建软件定时器

当连接到主机时,在观察者回调函数中会开启软件定时器



在软件定时器超时回调函数中,进行参数更新

主机

接收到从机的连接参数更新请求后,会触发连接参数请求更新事件,如果同意从机的连接参数,则在连接参数更新事件里面更新主机的连接参数.

PHY 物理层参数更新

蓝牙5.0 以后,蓝牙的物理层由原来的只支持1M 变为了 可选的1M , 2M ,500K, 125K等速率. 需要高速数据通信时可以在主从机连接后将物理层参数更新为2M,当然物理层速率越高,传输距离越近.nrf52832 支持 1M 和 2M速率的物理层.

从机广播初始化和主机扫描初始化时均可以设置物理层信息,如果没有设置则默认为1M, 连接后主机和从机都可以发出物理层更新请求,收到物理层更新请求后进行物理层参数更新.物理层更新请求和物理层参数更新均使用协议栈函数 sd_ble_gap_phy_update

/**@brief Initiate or respond to a PHY Update Procedure** @details   This function is used to initiate or respond to a PHY Update Procedure. It will always*            generate a @ref BLE_GAP_EVT_PHY_UPDATE event if successfully executed.*            If this function is used to initiate a PHY Update procedure and the only option*            provided in @ref ble_gap_phys_t::tx_phys and @ref ble_gap_phys_t::rx_phys is the*            currently active PHYs in the respective directions, the SoftDevice will generate a*            @ref BLE_GAP_EVT_PHY_UPDATE with the current PHYs set and will not initiate the*            procedure in the Link Layer.**            If @ref ble_gap_phys_t::tx_phys or @ref ble_gap_phys_t::rx_phys is @ref BLE_GAP_PHY_AUTO,*            then the stack will select PHYs based on the peer's PHY preferences and the local link*            configuration. The PHY Update procedure will for this case result in a PHY combination*            that respects the time constraints configured with @ref sd_ble_cfg_set and the current*            link layer data length.**            When acting as a central, the SoftDevice will select the fastest common PHY in each direction.**            If the peer does not support the PHY Update Procedure, then the resulting*            @ref BLE_GAP_EVT_PHY_UPDATE event will have a status set to*            @ref BLE_HCI_UNSUPPORTED_REMOTE_FEATURE.**            If the PHY Update procedure was rejected by the peer due to a procedure collision, the status*            will be @ref BLE_HCI_STATUS_CODE_LMP_ERROR_TRANSACTION_COLLISION or*            @ref BLE_HCI_DIFFERENT_TRANSACTION_COLLISION.*            If the peer responds to the PHY Update procedure with invalid parameters, the status*            will be @ref BLE_HCI_STATUS_CODE_INVALID_LMP_PARAMETERS.*            If the PHY Update procedure was rejected by the peer for a different reason, the status will*            contain the reason as specified by the peer.** @events* @event{@ref BLE_GAP_EVT_PHY_UPDATE, Result of the PHY Update Procedure.}* @endevents** @mscs* @mmsc{@ref BLE_GAP_CENTRAL_PHY_UPDATE}* @mmsc{@ref BLE_GAP_PERIPHERAL_PHY_UPDATE}* @endmscs** @param[in] conn_handle   Connection handle to indicate the connection for which the PHY Update is requested.* @param[in] p_gap_phys    Pointer to PHY structure.** @retval ::NRF_SUCCESS Successfully requested a PHY Update.* @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.* @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle supplied.* @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied.* @retval ::NRF_ERROR_NOT_SUPPORTED Unsupported PHYs supplied to the call.* @retval ::NRF_ERROR_INVALID_STATE No link has been established.* @retval ::NRF_ERROR_BUSY Procedure is already in progress or not allowed at this time. Process pending events and wait for the pending procedure to complete and retry.**/
SVCALL(SD_BLE_GAP_PHY_UPDATE, uint32_t, sd_ble_gap_phy_update(uint16_t conn_handle, ble_gap_phys_t const *p_gap_phys));

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

  1. nrf52832学习笔记(1)蓝牙心电例程分析

    对于我这种之前完全不懂蓝牙,接触学习nordic的蓝牙感觉学起来有困难,他那api讲解文档竟然网页版的,而且链接一层又一层,网速又慢,协议栈版本又多= .= 但还是要学啊,就拿着他的例字代码看吧... ...

  2. nrf52832 学习笔记(三)蓝牙从机广播

    nrf52832 学习笔记(三)蓝牙从机广播 蓝牙从机要想被主机连接,首先需要发送广播信息,周围主机通过扫描广播信号,根据从机的广播信息,判断是否连接. 蓝牙协议栈初始化 不管是主机还是从机,要想使用 ...

  3. nrf52832 学习笔记(七)蓝牙协议层级理解

    nrf52832 学习笔记(七)蓝牙协议层级理解 本文主要由一下几篇文档摘录汇总而成 ,如有错误欢迎斧正 da14531 蓝牙协议文档 深入浅出低功耗蓝牙(BLE)协议栈 低功耗蓝牙ATT/GATT/ ...

  4. nrf52832 学习笔记(九)蓝牙主机发现服务

    nrf52832 学习笔记(九)蓝牙主机发现服务 服务发现流程 数据如同下表一样存储在服务端,客户端首先要获取表中的Handle和Type列,从而知道服务端中存在哪些数据,以便后面读.写.通知等操作. ...

  5. nrf52832 学习笔记(四)蓝牙主机扫描

    nrf52832 学习笔记(四)蓝牙主机扫描 从机发出广播后就需要主机进行扫描了,主机扫描之前和从机一样,也需要协议栈初始化.GAP初始化.GATT初始化,这些和从机类似,参考 nrf52832 学习 ...

  6. nrf52832 学习笔记(六)配对和绑定

    nrf52832 学习笔记(六)配对和绑定 配对绑定推荐博客低功耗蓝牙配对绑定解读和实践 蓝牙在配对之前都是明文通信的,也就是说主从机之间传输的数据包可以被第三方抓取分析逆向,而且如果没有配对,谁都可 ...

  7. nrf52832 学习笔记(二)SDK框架分析

    nrf52832 学习笔记(二)SDK框架分析 个人对SDK框架的一些理解,如有错误欢迎斧正. flash 分区 在不包含DFU的情况下,nrf52832 flash划分为: MBR 0x000000 ...

  8. NRF52832学习笔记(38)——修改发射功率

    一.背景 蓝牙接收信号强度 RSSI 的直接影响因素就是蓝牙信号的发射功率.发射功率就是你所使用的设备(开发板.手机)所发射出来给主机或从机设备的信号强度.同时在实际应用当中,时常也需要修改蓝牙的发射 ...

  9. StackExchange.Redis学习笔记(五) 发布和订阅

    StackExchange.Redis学习笔记(五) 发布和订阅 原文:StackExchange.Redis学习笔记(五) 发布和订阅 Redis命令中的Pub/Sub Redis在 2.0之后的版 ...

最新文章

  1. UVA 10041 Vito's Family
  2. 惠普ilo管理界面远程安装系统
  3. DB2的日志理解难点
  4. 12月16日课程安排
  5. JS前台页面验证文本框非空
  6. django-后台管理-编辑页的选项
  7. html怎样设置图片的位置不变,CSS 如何定位图片保持位置不变?
  8. Dart 5-Day
  9. 基础 | 这波编程基础绝了!快来学习!
  10. verilog中的定点数、浮点数、定点小数、定点整数的表示及运算
  11. CDOJ 483 Data Structure Problem DFS
  12. 20155201 2016-2017-2 《Java程序设计》第五周学习总结
  13. 49.QComboBox
  14. 视频云服务的技术现状与发展探讨
  15. 【无标题】工商银行科技菁英岗笔经面经
  16. C#调用obs studio 二次开发 源码分析 编译
  17. 原神 android 手柄,如何在手机上流畅体验《原神》,推荐机型和手柄缺一不可
  18. 读《时间管理:如何充分利用你的24小时》笔记
  19. 百度智能云 x 联通在线丨智能助理,“智”理你的通话
  20. 转:网络虚拟(包括overlay、underlay介绍)

热门文章

  1. 同花顺股票交易接口定义被类实现
  2. 例题9-27 方块消除 UVa10559
  3. html简单下拉菜单
  4. 自建CA给内部网站颁发SSL证书
  5. git:SSL证书问题:无法获取本地颁发者证书
  6. vue尚品汇商城项目-day00【项目介绍:此项目是基于vue2的前台电商项目和后台管理系统】
  7. 遍历目录下的所有文件和文件夹
  8. nginx静态文件缓存
  9. layui之图片上传
  10. 无憾,2019!加油,2020!