记录下NRF52832断开连接后自动开始广播的方法

1、在advertising_init函数里关闭断开连接后开记广播标志:init.config.ble_adv_on_disconnect_disabled = false; // 自动广播
static void advertising_init(void)
{
ret_code_terr_code;
ble_advertising_init_t init;
memset(&init, 0, sizeof(init));
init.advdata.name_type= BLE_ADVDATA_FULL_NAME; //广播名称
init.advdata.include_appearance= true;
init.advdata.flags= BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE; //有限广播模式,不支持传统蓝牙

//init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]); // 广播UUID设置
//init.advdata.uuids_complete.p_uuids= m_adv_uuids;
init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]); // 广播UUID设置
init.srdata.uuids_complete.p_uuids= m_adv_uuids;
【记录下NRF52832断开连接后自动开始广播的方法】init.config.ble_adv_fast_enabled= true; //
init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
init.config.ble_adv_fast_timeout= APP_ADV_DURATION;
//init.config.ble_adv_slow_enabled= true; //
//init.config.ble_adv_slow_interval = APP_ADV_INTERVAL;
//init.config.ble_adv_slow_timeout= APP_ADV_DURATION;
init.config.ble_adv_on_disconnect_disabled = true; // 禁止自动广播
init.evt_handler = on_adv_evt; // 广播事件回调函数
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);
}

2.实现断开连接后自动广播的代码:ret = ble_advertising_start(p_advertising, BLE_ADV_MODE_DIRECTED_HIGH_DUTY);
static void on_disconnected(ble_advertising_t * const p_advertising, ble_evt_t const * p_ble_evt)
{
uint32_t ret;
p_advertising->whitelist_temporarily_disabled = false;
if (p_ble_evt->evt.gap_evt.conn_handle == p_advertising->current_slave_link_conn_handle &&
p_advertising->adv_modes_config.ble_adv_on_disconnect_disabled == false)
{
ret = ble_advertising_start(p_advertising, BLE_ADV_MODE_DIRECTED_HIGH_DUTY);
if ((ret != NRF_SUCCESS) && (p_advertising->error_handler != NULL))
{
p_advertising->error_handler(ret);
}
}
}

    推荐阅读