nrf52832 之 广播信道设置

       ble4.0共有3个广播信道(37/38/39)和37个数据信道,3个广播信道分散在ISM频段的不同区域,如果他们集中在某个频段,则可能因为这个频段的深度衰弱而造成所有广播无法进行。因 此,各个广播信道直接至少相差24mhz。

       一般的应用为了稳定性都会使用默认设置,三个默认广播信道全部打开,但是在研发过程中,可能会有抓包分析的场景,此时如果三个信道全部打开,则抓包难度会提升,因此,我们需要固定广播在某一个特定信道。


       下面介绍nrf52832的广播信道设置方法:


1、在ble_gap.h中有广播信道开关的结构体定义:

/**@brief Channel mask for RF channels used in advertising and scanning. */ 
typedef struct
{
  uint8_t ch_37_off : 1;  /**< Setting this bit to 1 will turn off advertising on channel 37 */
  uint8_t ch_38_off : 1;  /**< Setting this bit to 1 will turn off advertising on channel 38 */
  uint8_t ch_39_off : 1;  /**< Setting this bit to 1 will turn off advertising on channel 39 */
} ble_gap_adv_ch_mask_t;


      2、广播信道开关作为广播参数结构体的一个参数出现:

/**@brief GAP advertising parameters.*/
typedef struct
{
  uint8_t               type;                 /**< See @ref BLE_GAP_ADV_TYPES. */
  ble_gap_addr_t       *p_peer_addr;          /**< For @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND mode only, known peer address. */
  uint8_t               fp;                   /**< Filter Policy, see @ref BLE_GAP_ADV_FILTER_POLICIES. */
  ble_gap_whitelist_t  *p_whitelist;          /**< Pointer to whitelist, NULL if no whitelist or the current active whitelist is to be used. */
  uint16_t              interval;             /**< Advertising interval between 0x0020 and 0x4000 in 0.625 ms units (20ms to 10.24s), see @ref BLE_GAP_ADV_INTERVALS.
                                                   - If type equals @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND, this parameter must be set to 0 for high duty cycle directed advertising.
                                                   - If type equals @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND, set @ref BLE_GAP_ADV_INTERVAL_MIN <= interval <= @ref BLE_GAP_ADV_INTERVAL_MAX for low duty cycle advertising.*/
  uint16_t              timeout;              /**< Advertising timeout between 0x0001 and 0x3FFF in seconds, 0x0000 disables timeout. See also @ref BLE_GAP_ADV_TIMEOUT_VALUES. If type equals @ref BLE_GAP_ADV_TYPE_ADV_DIRECT_IND, this parameter must be set to 0 for High duty cycle directed advertising. */
  ble_gap_adv_ch_mask_t channel_mask;         /**< Advertising channel mask. @see ble_gap_channel_mask_t for documentation. */
} ble_gap_adv_params_t;

3、广播参数设置在ble_advertising.c文件中的函数uint32_t ble_advertising_start(ble_adv_mode_t advertising_mode)中:

uint32_t ble_advertising_start(ble_adv_mode_t advertising_mode)
{
	uint32_t			 err_code;
	ble_gap_adv_params_t adv_params;
	
	m_adv_mode_current = advertising_mode;
	//……省略
	// Initialize advertising parameters with default values.
	memset(&adv_params, 0, sizeof(adv_params));
	
	//……省略



4、上述函数中对广播参数进行了清零操作,之后并没有对channel_mask进行设置,所以就是三个信道全部使能。那么关掉其中两个信道,只保留37信道广播的方法为在上述
uint32_t ble_advertising_start(ble_adv_mode_t advertising_mode)函数中添加如下代码:

adv_params.channel_mask.ch_38_off=1;
adv_params.channel_mask.ch_39_off=1;

这样就能保证广播只在一个信道进行,方便抓包分析。





你可能感兴趣的:(低功耗蓝牙)