nrf52 - 快速广播与慢速广播

许多应用都对功耗有严格要求,而且会要求一直处于广播模式,这时就要考虑降低广播频率来降低功耗,此时可以称为慢速广播,但是广播频率越快越利于手机连接,所以为了快速发现和连接,往往需要快速广播,一般的处理方式是快速广播30s然后进入慢速广播。

nordic考虑到了这种应用场景,所以用nordic的sdk很容易实现上述方案:


首先介绍一下广播参数的结构体:

typedef struct
{
    bool     ble_adv_whitelist_enabled;       /**< Enable or disable use of the whitelist. */
    bool     ble_adv_directed_enabled;        /**< Enable or disable direct advertising mode. */
    bool     ble_adv_directed_slow_enabled;   /**< Enable or disable direct advertising mode. */
    uint32_t ble_adv_directed_slow_interval;  /**< Advertising interval for directed advertising. */
    uint32_t ble_adv_directed_slow_timeout;   /**< Time-out (number of tries) for direct advertising. */
    bool     ble_adv_fast_enabled;            /**< Enable or disable fast advertising mode. */
    uint32_t ble_adv_fast_interval;           /**< Advertising interval for fast advertising. */
    uint32_t ble_adv_fast_timeout;            /**< Time-out (in seconds) for fast advertising. */
    bool     ble_adv_slow_enabled;            /**< Enable or disable slow advertising mode. */
    uint32_t ble_adv_slow_interval;           /**< Advertising interval for slow advertising. */
    uint32_t ble_adv_slow_timeout;            /**< Time-out (in seconds) for slow advertising. */
}ble_adv_modes_config_t;

注释写的很清楚,前面有广播白名单、定向广播的设置,我们一般应用都是普通广播模式,所以重点看以下几个结构体成员

    bool     ble_adv_fast_enabled;            /**< Enable or disable fast advertising mode. */
    uint32_t ble_adv_fast_interval;           /**< Advertising interval for fast advertising. */
    uint32_t ble_adv_fast_timeout;            /**< Time-out (in seconds) for fast advertising. */
    bool     ble_adv_slow_enabled;            /**< Enable or disable slow advertising mode. */
    uint32_t ble_adv_slow_interval;           /**< Advertising interval for slow advertising. */
    uint32_t ble_adv_slow_timeout;            /**< Time-out (in seconds) for slow advertising. */

前面三个是快速广播使能,以及广播间隔和广播时间,后面三个是慢速广播的

快速广播使能后,开始广播的时候选择快速模式就会进入快速广播模式,如果timeout设置非0,则时间到了timeout后退出快速广播模式,

此时如果慢速广播使能了,则进入慢速广播模式,如果timeout设置非0,则时间到了timeout后不再进行广播,

timeout设置为0则不退出当前广播模式,持续广播


了解了这个结构体,想必怎么实现开头说的方案就很简单了,下面贴一下具体实现代码:


1、相关宏定义

#define APP_ADV_FAST_INTERVAL            0x0028    /**< Fast advertising interval (in units of 0.625 ms.). */
#define APP_ADV_SLOW_INTERVAL            0x0C80    /**< Slow advertising interval (in units of 0.625 ms. ). */
#define APP_ADV_FAST_TIMEOUT             30        /**< The duration of the fast advertising period (in seconds). */
#define APP_ADV_SLOW_TIMEOUT             0  

2、广播初始化函数

static void advertising_init(void)
{
    uint32_t      err_code;
    ble_advdata_t advdata;

    // Build advertising data struct to pass into @ref ble_advertising_init.
    memset(&advdata, 0, sizeof(advdata));

    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = true;
    advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    advdata.uuids_complete.p_uuids  = m_adv_uuids;

    //ble_adv_modes_config_t options = {0};
    //options.ble_adv_fast_enabled  = BLE_ADV_FAST_ENABLED;
    //options.ble_adv_fast_interval = APP_ADV_INTERVAL;
    //options.ble_adv_fast_timeout  = APP_ADV_TIMEOUT_IN_SECONDS;

	 ble_adv_modes_config_t options =
    {
        BLE_ADV_WHITELIST_DISABLED,
        BLE_ADV_DIRECTED_DISABLED,
        BLE_ADV_DIRECTED_SLOW_DISABLED, 0,0,
        BLE_ADV_FAST_ENABLED, APP_ADV_FAST_INTERVAL, APP_ADV_FAST_TIMEOUT,
        BLE_ADV_SLOW_ENABLED, APP_ADV_SLOW_INTERVAL, APP_ADV_SLOW_TIMEOUT
    };

    err_code = ble_advertising_init(&advdata, NULL, &options, on_adv_evt, NULL);
    APP_ERROR_CHECK(err_code);
}



3、在main函数中开启快速广播模式

ble_advertising_start(BLE_ADV_MODE_FAST);




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