[nrf51][nrf52]设置快速广播和慢速广播
原创
©著作权归作者所有:来自51CTO博客作者xuejianqiang的原创作品,请联系作者获取转载授权,否则将追究法律责任
nrf的广播参数介绍:
/**@defgroup BLE_GAP_ADV_FLAGS GAP Advertisement Flags
* @{ */
#define BLE_GAP_ADV_FLAG_LE_LIMITED_DISC_MODE (0x01) /**< LE Limited Discoverable Mode. */
#define BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE (0x02) /**< LE General Discoverable Mode. */
#define BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED (0x04) /**< BR/EDR not supported. */
#define BLE_GAP_ADV_FLAG_LE_BR_EDR_CONTROLLER (0x08) /**< Simultaneous LE and BR/EDR, Controller. */
#define BLE_GAP_ADV_FLAG_LE_BR_EDR_HOST (0x10) /**< Simultaneous LE and BR/EDR, Host. */
#define BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE (BLE_GAP_ADV_FLAG_LE_LIMITED_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED) /**< LE Limited Discoverable Mode, BR/EDR not supported. */
#define BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE (BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE | BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED) /**< LE General Discoverable Mode, BR/EDR not supported. */
/**@} */
BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE:有限可发现模式,不支持BR/EDR
LE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE:一般可发现模式,不支持BR/EDR
BR/EDR:蓝牙基本数率/增强数据率()
有限可发现模式广播的间隔比一般可发现模式小。 从时间的限制上,我们可以看出有限可发现模式对连接的迫切性和目的性比一般可发现模式高,一个处于有限可发现模式的设备正在广播,那么他一定是刚被用户操作过并且极希望被连接。
一般情况下,设备首次开机、按下连接按钮,设备进入有限可发现模式比较合适。如果在有限可发现模式时间内没有被连接,可以转入一般可发现模式。
如果我们希望设备在没有被连接时一直保持广播,那么应该使用一般可发现模式,因为一般可发现模式是没有时间限制的。
若想设置时间为无穷大 #define APP_ADV_DURATION 0 【注意】需要配置为一般可发现模式
下面代码举例:先快速广播10s,然后无限慢广播。
#define APP_ADV_FAST_INTERVAL 0x0028 /**< Fast advertising interval (in units of 0.625 ms. This value corresponds to 25 ms.). */
#define APP_ADV_SLOW_INTERVAL 0x00A0 /**< Slow advertising interval (in units of 0.625 ms. This value corresponds to 100 ms.). */
#define APP_ADV_FAST_DURATION 1000 /**< The advertising duration of fast advertising in units of 10 milliseconds. */
#define APP_ADV_SLOW_DURATION 0 /**< The advertising duration of slow advertising in units of 10 milliseconds. */
/**@brief Function for initializing the Advertising functionality.
*/
static void advertising_init(void)
{
ret_code_t err_code;
uint8_t adv_flags;
ble_advertising_init_t init;
memset(&init, 0, sizeof(init));
adv_flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;//BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
init.advdata.include_appearance = true;
init.advdata.flags = adv_flags;
init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
init.advdata.uuids_complete.p_uuids = m_adv_uuids;
#if SWIFT_PAIR_SUPPORTED == 1
init.advdata.p_manuf_specific_data = &m_sp_manuf_advdata;
memcpy(&m_sp_advdata, &init.advdata, sizeof(m_sp_advdata));
#endif
init.config.ble_adv_whitelist_enabled = true;
init.config.ble_adv_directed_high_duty_enabled = true;
init.config.ble_adv_directed_enabled = false;
init.config.ble_adv_directed_interval = 0;
init.config.ble_adv_directed_timeout = 0;
init.config.ble_adv_fast_enabled = true;
init.config.ble_adv_fast_interval = APP_ADV_FAST_INTERVAL;
init.config.ble_adv_fast_timeout = APP_ADV_FAST_DURATION;
init.config.ble_adv_slow_enabled = true;
init.config.ble_adv_slow_interval = APP_ADV_SLOW_INTERVAL;
init.config.ble_adv_slow_timeout = APP_ADV_SLOW_DURATION;
init.evt_handler = on_adv_evt;
init.error_handler = ble_advertising_error_handler;
err_code = ble_advertising_init(&m_advertising, &init);
APP_ERROR_CHECK(err_code);
ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
}