1480|2

83

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

【NRF52840】学习记录——扫描 [复制链接]

【NRF52840】学习记录——扫描

  • 扫描模式介绍

    有关蓝牙扫描模式的介绍,可以参考这篇文章:https://bbs.eeworld.com.cn/thread-1248731-1-1.html

  • 扫描初始化

    在扫描初始化中,最主要的函数是nrf_ble_scan_init函数,在这个函数中主要注意参数p_init和参数evt_handler,前一个是扫描初始化参数,后一个是扫描处理回调函数。

/**[url=home.php?mod=space&uid=159083]@brief[/url]  Function for initializing the Scanning Module.
 *
 * @param[out] p_scan_ctx   Pointer to the Scanning Module instance. This structure must be supplied by
 *                          the application. It is initialized by this function and is later used
 *                          to identify this particular module instance.
 * 
 * @param[in]  p_init       Can be initialized as NULL. If NULL, the parameters required to initialize
 *                          the module are loaded from static configuration.
 *                          If module is to establish the connection automatically, this must be initialized
 *                          with the relevant data.
 * @param[in]  evt_handler  Handler for the scanning events.
 *                          Can be initialized as NULL if no handling is implemented in the main application.
 *
 * @retval NRF_SUCCESS      If initialization was successful.
 * @retval NRF_ERROR_NULL   When the NULL pointer is passed as input.
 */
ret_code_t nrf_ble_scan_init(nrf_ble_scan_t            * const p_scan_ctx,
                             nrf_ble_scan_init_t const * const p_init,
                             nrf_ble_scan_evt_handler_t        evt_handler);

1.扫描初始化nrf_ble_scan_init_t结构体。在这个结构体中主要注意参数p_scan_param和参数p_conn_param,前一个是扫描参数,后一个是连接参数。

/**@brief Structure for Scanning Module initialization.
 */
typedef struct
{
    ble_gap_scan_params_t const * p_scan_param;     /**< BLE GAP scan parameters required to initialize the module. Can be initialized as NULL. If NULL, the parameters required to initialize the module are loaded from the static configuration. */
    bool                          connect_if_match; /**< If set to true, the module automatically connects after a filter match or successful identification of a device from the whitelist. */
    ble_gap_conn_params_t const * p_conn_param;     /**< Connection parameters. Can be initialized as NULL. If NULL, the default static configuration is used. */
    uint8_t                       conn_cfg_tag;     /**< Variable to keep track of what connection settings will be used if a filer match or a whitelist match results in a connection. */
} nrf_ble_scan_init_t;

扫描参数ble_gap_scan_params_t结构体

/**@brief GAP scanning parameters. */
typedef struct
{
  uint8_t               extended               : 1; /**< If 1, the scanner will accept extended advertising packets.
                                                         If set to 0, the scanner will not receive advertising packets
                                                         on secondary advertising channels, and will not be able
                                                         to receive long advertising PDUs. */
  uint8_t               report_incomplete_evts : 1; /**< If 1, events of type [url=home.php?mod=space&uid=1064992]@ref[/url]  ble_gap_evt_adv_report_t may have
                                                         @ref ble_gap_adv_report_type_t::status set to
                                                         @ref BLE_GAP_ADV_DATA_STATUS_INCOMPLETE_MORE_DATA.
                                                         This parameter is ignored when used with @ref sd_ble_gap_connect
                                                         @note This may be used to abort receiving more packets from an extended
                                                               advertising event, and is only available for extended
                                                               scanning, see @ref sd_ble_gap_scan_start.
                                                         @note This feature is not supported by this SoftDevice. */
  uint8_t               active                 : 1; /**< If 1, perform active scanning by sending scan requests.
                                                         This parameter is ignored when used with @ref sd_ble_gap_connect. */
  uint8_t               filter_policy          : 2; /**< Scanning filter policy. @sa BLE_GAP_SCAN_FILTER_POLICIES.
                                                         @note Only @ref BLE_GAP_SCAN_FP_ACCEPT_ALL and
                                                               @ref BLE_GAP_SCAN_FP_WHITELIST are valid when used with
                                                               @ref sd_ble_gap_connect */
  uint8_t               scan_phys;                  /**< Bitfield of PHYs to scan on. If set to @ref BLE_GAP_PHY_AUTO,
                                                         scan_phys will default to @ref BLE_GAP_PHY_1MBPS.
                                                         - If @ref ble_gap_scan_params_t::extended is set to 0, the only
                                                           supported PHY is @ref BLE_GAP_PHY_1MBPS.
                                                         - When used with @ref sd_ble_gap_scan_start,
                                                           the bitfield indicates the PHYs the scanner will use for scanning
                                                           on primary advertising channels. The scanner will accept
                                                           @ref BLE_GAP_PHYS_SUPPORTED as secondary advertising channel PHYs.
                                                         - When used with @ref sd_ble_gap_connect, the bitfield indicates
                                                           the PHYs the initiator will use for scanning on primary advertising
                                                           channels. The initiator will accept connections initiated on either
                                                           of the @ref BLE_GAP_PHYS_SUPPORTED PHYs.
                                                           If scan_phys contains @ref BLE_GAP_PHY_1MBPS and/or @ref BLE_GAP_PHY_2MBPS,
                                                           the primary scan PHY is @ref BLE_GAP_PHY_1MBPS.
                                                           If scan_phys also contains @ref BLE_GAP_PHY_CODED, the primary scan
                                                           PHY will also contain @ref BLE_GAP_PHY_CODED. If the only scan PHY is
                                                           @ref BLE_GAP_PHY_CODED, the primary scan PHY is
                                                           @ref BLE_GAP_PHY_CODED only. */
  uint16_t              interval;                   /**< Scan interval in 625 us units. @sa BLE_GAP_SCAN_INTERVALS. */
  uint16_t              window;                     /**< Scan window in 625 us units. @sa BLE_GAP_SCAN_WINDOW.
                                                         If scan_phys contains both @ref BLE_GAP_PHY_1MBPS and
                                                         @ref BLE_GAP_PHY_CODED interval shall be larger than or
                                                         equal to twice the scan window. */
  uint16_t              timeout;                    /**< Scan timeout in 10 ms units. @sa BLE_GAP_SCAN_TIMEOUT. */
  ble_gap_ch_mask_t     channel_mask;               /**< Channel mask for primary and secondary advertising channels.
                                                         At least one of the primary channels, that is channel index 37-39, must be
                                                         set to 0.
                                                         Masking away secondary channels is not supported. */
} ble_gap_scan_params_t;

连接参数ble_gap_conn_params_t结构体

/**@brief GAP connection parameters.
 *
 * @note  When ble_conn_params_t is received in an event, both min_conn_interval and
 *        max_conn_interval will be equal to the connection interval set by the central.
 *
 * @note  If both conn_sup_timeout and max_conn_interval are specified, then the following constraint applies:
 *        conn_sup_timeout * 4 > (1 + slave_latency) * max_conn_interval
 *        that corresponds to the following Bluetooth Spec requirement:
 *        The Supervision_Timeout in milliseconds shall be larger than
 *        (1 + Conn_Latency) * Conn_Interval_Max * 2, where Conn_Interval_Max is given in milliseconds.
 */
typedef struct
{
  uint16_t min_conn_interval;         /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
  uint16_t max_conn_interval;         /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
  uint16_t slave_latency;             /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
  uint16_t conn_sup_timeout;          /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
} ble_gap_conn_params_t;

2.扫描处理回调函数函数类型,这个函数就一个参数p_scan_evt扫描事件

/**@brief BLE scanning event handler type.
 */
typedef void (*nrf_ble_scan_evt_handler_t)(scan_evt_t const * p_scan_evt);

扫描事件scan_evt_t结构体,这个结构体的第一个成员是扫描事件类型scan_evt_id

/**@brief Structure for Scanning Module event data.
 *
 * @details This structure is used to send module event data to the main application when an event occurs.
 */
typedef struct
{
    nrf_ble_scan_evt_t scan_evt_id; /**< Type of event propagated to the main application. */
    union
    {
        nrf_ble_scan_evt_filter_match_t   filter_match;           /**< Scan filter match. */
        ble_gap_evt_timeout_t             timeout;                /**< Timeout event parameters. */
        ble_gap_evt_adv_report_t const  * p_whitelist_adv_report; /**< Advertising report event parameters for whitelist. */
        ble_gap_evt_adv_report_t const  * p_not_found;            /**< Advertising report event parameters when filter is not found. */
        nrf_ble_scan_evt_connected_t      connected;              /**< Connected event parameters. */
        nrf_ble_scan_evt_connecting_err_t connecting_err;         /**< Error event when connecting. Propagates the error code returned by the SoftDevice API @ref sd_ble_gap_scan_start. */
    } params;
    ble_gap_scan_params_t const * p_scan_params;                  /**< GAP scanning parameters. These parameters are needed to establish connection. */
} scan_evt_t;

扫描事件类型nrf_ble_scan_evt_t枚举

/**@brief Enumeration for scanning events.
 *
 * @details These events are propagated to the main application if a handler is provided during
 *          the initialization of the Scanning Module. @ref NRF_BLE_SCAN_EVT_WHITELIST_REQUEST cannot be
 *          ignored if whitelist is used.
 */
typedef enum
{
    NRF_BLE_SCAN_EVT_FILTER_MATCH,         /**< A filter is matched or all filters are matched in the multifilter mode. */
    NRF_BLE_SCAN_EVT_WHITELIST_REQUEST,    /**< Request the whitelist from the main application. For whitelist scanning to work, the whitelist must be set when this event occurs. */
    NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT, /**< Send notification to the main application when a device from the whitelist is found. */
    NRF_BLE_SCAN_EVT_NOT_FOUND,            /**< The filter was not matched for the scan data. */
    NRF_BLE_SCAN_EVT_SCAN_TIMEOUT,         /**< Scan timeout. */
    NRF_BLE_SCAN_EVT_CONNECTING_ERROR,     /**< Error occurred when establishing the connection. In this event, an error is passed from the function call @ref sd_ble_gap_connect. */
    NRF_BLE_SCAN_EVT_CONNECTED             /**< Connected to device. */
} nrf_ble_scan_evt_t;
  • 扫描开始与停止

    扫描开始函数nrf_ble_scan_start

/**@brief Function for starting scanning.
 *
 * @details This function starts the scanning according to the configuration set during the initialization.
 *
 * @param[in] p_scan_ctx       Pointer to the Scanning Module instance.
 *
 * @retval    NRF_SUCCESS      If scanning started. Otherwise, an error code is returned.
 * @retval    NRF_ERROR_NULL   If NULL pointer is passed as input.
 *
 * [url=home.php?mod=space&uid=784970]@return[/url]                     This API propagates the error code returned by the
 *                             SoftDevice API @ref sd_ble_gap_scan_start.
 */
ret_code_t nrf_ble_scan_start(nrf_ble_scan_t const * const p_scan_ctx);

    扫描停止函数nrf_ble_scan_stop

/**@brief Function for stopping scanning.
 */
void nrf_ble_scan_stop(void);

 

最新回复

掌握使用这个nrf_ble_scan_init函数有什么秘诀   详情 回复 发表于 2023-7-6 07:25
点赞 关注
 
 

回复
举报

1703

帖子

0

TA的资源

五彩晶圆(初级)

沙发
 

掌握使用这个nrf_ble_scan_init函数有什么秘诀

 
 
 

回复

83

帖子

0

TA的资源

一粒金砂(中级)

板凳
 
火辣西米秀 发表于 2023-7-6 07:25 掌握使用这个nrf_ble_scan_init函数有什么秘诀

主要是主要两个参数,一个是扫描初始化参数,另一个是扫描处理回调函数。

在扫描初始化参数,主要是扫描参数。

扫描参数主要包含:active:是否主动扫描,配置为1则是主动扫描,0则是被动扫描。interval:扫描间隔,控制器间隔多长时间扫描一次,也就是两个连续的扫描窗口开始时间的时间间隔。window:扫描窗口,每次扫描所持续的时间,在持续时间内,扫描设备一直在广播信道上运行。filter_policy:扫描筛选策略,也就是说接受任何广播数据或者仅仅接受白名单设备的广播数据包。实际上就是决定是否使用白名单过滤广播数据包。这里注意一点,如果定向广播数据包中的目的地址并非是自己的,那么该数据必须抛弃,即使广播数据包的发送者在自己的白名单中。timeout:扫描超时,超过指定的时间后,没有扫描到设备将停止扫描。scan_phys:扫描的物理层速度

在扫描处理回调函数,主要是扫描事件类型。

根据不同的扫描事件执行不同的任务,包含:NRF_BLE_SCAN_EVT_FILTER_MATCH:过滤器匹配事件、NRF_BLE_SCAN_EVT_WHITELIST_REQUEST:请求白名单、NRF_BLE_SCAN_EVT_WHITELIST_ADV_REPORT:发现白名单中设备、NRF_BLE_SCAN_EVT_NOT_FOUND:过滤器不匹配事件、NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:扫描超时、NRF_BLE_SCAN_EVT_CONNECTING_ERROR:连接错误、NRF_BLE_SCAN_EVT_CONNECTED:连接。

 

 

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表