ble_advertising_start() failed with errCode 0x8

I would like to start advertising after BLE connection disconnected. But ble_advertising_start() return errCode 0x8.

static void ble_evt_handler(ble_evt_t const* pBleEvt, void* pContext)
{
    switch (pBleEvt->header.evt_id)
    {
        case BLE_GAP_EVT_DISCONNECTED:
        {
            NRF_LOG_INFO("BLE_GAP_EVT_DISCONNECTED\n");
            advertisingStart();
        }    
        break;
    ...
    }
    ...
}

The following is log message of nRF52832 device.

00> <info> app: POWER_ON.
00> <info> app: BLE_ADV_EVT_FAST
00> <debug> nrf_ble_gatt: Requesting to update ATT MTU to 185 bytes on connection 0x0.
00> <info> app: PM_EVT_BONDED_PEER_CONNECTED
00> <info> app: BLE_GAP_EVT_CONNECTED
00> <info> app: PM_EVT_CONN_SEC_SUCCEEDED
00> <info> app: Connection secured: role: 1, conn_handle: 0x0, procedure: 0.
00> <debug> nrf_ble_gatt: ATT MTU updated to 185 bytes on connection 0x0 (response).
00> <debug> app: _securityRequestHandler
00> <info> app: PM_EVT_CONN_SEC_CONFIG_REQ
00> <info> app: BLE_GAP_EVT_SEC_PARAMS_REQUEST
00> <info> app: PM_EVT_CONN_SEC_SUCCEEDED
00> <info> app: Connection secured: role: 1, conn_handle: 0x0, procedure: 1.
00> <info> app: BLE_GAP_EVT_AUTH_STATUS
00> <info> app: BLE_ADV_EVT_FAST
00> <info> app: BLE_GAP_EVT_DISCONNECTED
00> <warning> app: ble_advertising_start() failed errCode=0x8

  • #include "sdk_config.h"
    #include "advertising.h"
    #include "ble_types.h"
    #include "ble_nus.h"
    #include "bsp.h"
    #include "bsp_btn_ble.h"
    #include "error.h"
    #include "nrf_log.h"
    #include "nrf_sdh_ble.h"
    
    #define APP_ADV_INTERVAL               800             /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
    #define APP_ADV_DURATION               1800            /**< The advertising duration (180 seconds) in units of 10 milliseconds. */         
    #define APP_ADV_FAST_DURATION          0//1800          /**< The advertising duration of fast advertising in units of 10 milliseconds. */
    #define APP_ADV_SLOW_DURATION          0//1800          /**< The advertising duration of slow advertising in units of 10 milliseconds. */
    #define APP_ADV_FAST_INTERVAL          MSEC_TO_UNITS(500, UNIT_0_625_MS)        /**< The advertising interval (in units of 0.625 ms). The default value corresponds to 25 ms. */
    #define APP_ADV_SLOW_INTERVAL          MSEC_TO_UNITS(500, UNIT_0_625_MS)        /**< Slow advertising interval (in units of 0.625 ms). The default value corresponds to 2 seconds. */
    #define NUS_SERVICE_UUID_TYPE          BLE_UUID_TYPE_VENDOR_BEGIN                  /**< UUID type for the Nordic UART Service (vendor specific). */
    
    BLE_ADVERTISING_DEF(_gAdvertising);  /**< Advertising module instance. */ 
    
    static ble_uuid_t _gAdvUuids[]          = {                                         /**< Universally unique service identifier. */ 
        {BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}
    };
    
    /**@brief Function for putting the chip into sleep mode.
     *
     * @note This function will not return.
     */
    #if 0 
    static void _sleepModeEnter(void) {
        ret_code_t errCode;
        errCode = bsp_indication_set(BSP_INDICATE_IDLE);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("bsp_indication_set() failed, errCode=0x%x\n", errCode);
            return;
        }
    
        // Prepare wakeup buttons.
        errCode = bsp_btn_ble_sleep_mode_prepare();
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("bsp_btn_ble_sleep_mode_prepare() failed, errCode=0x%x\n", errCode);
            return;
        }
    
        // Go to system-off mode (this function will not return; wakeup will cause a reset).
        errCode = sd_power_system_off();
    #ifndef DEBUG_NRF    
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("sd_power_system_off() failed, errCode=0x%x\n", errCode);
            return;
        }
    #endif
    }
    #endif
    
    /**@brief Function for handling advertising events.
     *
     * @details This function is called for advertising events that are passed to the application.
     *
     * @param[in] ble_adv_evt  Advertising event.
     */
    static void _advertisingEvtHandler(ble_adv_evt_t evt) {
        ret_code_t errCode;
    
        switch (evt) {
            case BLE_ADV_EVT_FAST:
            {    
                NRF_LOG_INFO("BLE_ADV_EVT_FAST");
                errCode = bsp_indication_set(BSP_INDICATE_ADVERTISING);
                if (errCode != NRF_SUCCESS) {
                    NRF_LOG_WARNING("bsp_indication_set() failed, errCode=0x%x\n", errCode);
                    return;
                }
            }
            break;
                        
            case BLE_ADV_EVT_IDLE:
            {   
                NRF_LOG_INFO("BLE_ADV_EVT_IDLE");
                //_sleepModeEnter();
                advertisingStart();
            }
            break;
    
            default:
            break;
        }
    }
    
    #if 0
    int advertisingInit(void) {
        ret_code_t errCode;
        ble_advertising_init_t init;
    
        memset(&init, 0, sizeof(init));
    
        init.advdata.name_type               = BLE_ADVDATA_FULL_NAME;
        init.advdata.include_appearance      = false;
        init.advdata.flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
        init.advdata.uuids_complete.uuid_cnt = sizeof(_gAdvUuids) / sizeof(_gAdvUuids[0]);
        init.advdata.uuids_complete.p_uuids  = _gAdvUuids;
    
        init.srdata.uuids_complete.uuid_cnt = sizeof(_gAdvUuids) / sizeof(_gAdvUuids[0]);
        init.srdata.uuids_complete.p_uuids  = _gAdvUuids;
    
        init.config.ble_adv_whitelist_enabled = false;
        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 = _advertisingEvtHandler;
    
        errCode = ble_advertising_init(&_gAdvertising, &init);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("ble_advertising_init() failed errCode=0x%x", errCode);
            return ERROR_ADVERTISING_INITIALIZE_FAIL;
        }
    
        ble_advertising_conn_cfg_tag_set(&_gAdvertising, APP_BLE_CONN_CFG_TAG);
    
        return 0;
    }
    #else
    int advertisingInit(void) {
        uint32_t               errCode;
        ble_advertising_init_t init;
    
        memset(&init, 0, sizeof(init));
    
        init.advdata.name_type          = BLE_ADVDATA_FULL_NAME;
        init.advdata.include_appearance = false;
        init.advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
    
        init.srdata.uuids_complete.uuid_cnt = sizeof(_gAdvUuids) / sizeof(_gAdvUuids[0]);
        init.srdata.uuids_complete.p_uuids  = _gAdvUuids;
    
        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.evt_handler = _advertisingEvtHandler;
    
        errCode = ble_advertising_init(&_gAdvertising, &init);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("ble_advertising_init() failed errCode=0x%x", errCode);
            return ERROR_ADVERTISING_INITIALIZE_FAIL;
        }
    
        ble_advertising_conn_cfg_tag_set(&_gAdvertising, APP_BLE_CONN_CFG_TAG);
    
        return 0;
    }
    
    #endif
    
    
    ble_adv_modes_config_t advertisingConfigGet(void) {
        ble_adv_modes_config_t config;
        memset(&config, 0, sizeof(ble_adv_modes_config_t));
    
        config.ble_adv_fast_enabled           = true;
        config.ble_adv_fast_interval          = APP_ADV_INTERVAL;
        config.ble_adv_fast_timeout           = APP_ADV_DURATION;
        config.ble_adv_on_disconnect_disabled = true;
    
        return config;
    }
    
    void advertisingModesConfigSet(void) {
        const ble_adv_modes_config_t config = advertisingConfigGet();
        ble_advertising_modes_config_set(&_gAdvertising, &config);
    }    
    
    
    /* Function for starting advertising. */
    int advertisingStart(void) {
    //        memset(m_whitelist_peers, PM_PEER_ID_INVALID, sizeof(m_whitelist_peers));
    //        m_whitelist_peer_cnt = (sizeof(m_whitelist_peers) / sizeof(pm_peer_id_t));
    
    //        peer_list_get(m_whitelist_peers, &m_whitelist_peer_cnt);
    
    //        ret = pm_whitelist_set(m_whitelist_peers, m_whitelist_peer_cnt);
    //        APP_ERROR_CHECK(ret);
    
    //        // Setup the device identies list.
    //        // Some SoftDevices do not support this feature.
    //        ret = pm_device_identities_list_set(m_whitelist_peers, m_whitelist_peer_cnt);
    //        if (ret != NRF_ERROR_NOT_SUPPORTED)
    //        {
    //            APP_ERROR_CHECK(ret);
    //        }
    
    
        const ret_code_t errCode = ble_advertising_start(&_gAdvertising, BLE_ADV_MODE_FAST);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("ble_advertising_start() failed errCode=0x%x", errCode);
            return ERROR_ADVERTISING_START_FAIL;
        }
        
        return 0;
    }
    
    

  • Hi there,

    Error code 8 from ble_advertising_start() is NRF_ERROR_INVALID_STATE and is return if the module is not initialized. 

    Which is strange, since you're using the module when you advertised in the first place.

    Can you share your whole project? 

    regards

    Jared 

  • Does advertising stop after connecting created?

  • Yes, advertising will stop after a connection has been created.  

  • Do you have suggested example for testing advertising? I am add my modification to that example and then share source code to you.

Related