【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);