This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Stop advertising permanantly in nRF52 SDK15.2

Hi, I am using nRF52 DK with SDK 15.2 and SD132 for my project and use "ble_app_uart" as a reference. In my project, I want to start and stop advertisment manually through UART.

When I received adv_stop command, I use "sd_ble_gap_adv_stop(m_advertising.adv_handle);" to stop advertising. But it's restart advertising from somewhere else.

Even if I put timeout, it automatically restarts advertising. What I want is that it should stop advertising until I run "advertising_start()" again. Please help me how to solve this.

Parents
  • I believe that this code restarts advertising on disconnection, connection terminated events and communication timeout event.

    The actual call to start advertising is sd_ble_gap_adv_start, which in the BLE UART app is called from ble_advertising_start in ble_advertising.c

    Check out calls to:

    on_terminated, on_disconnected and ble_advertising_restart_without_whitelist

  • Hi, really thanks for your prompt reply. I checked all these functions (on_terminated, on_disconnected and ble_advertising_restart_without_whitelist) and yes they put advertising start again.

        ret = ble_advertising_start(p_advertising, p_advertising->adv_mode_current);
        if ((ret != NRF_SUCCESS) && (p_advertising->error_handler != NULL))
        {
            p_advertising->error_handler(ret);
        }
    
        return NRF_SUCCESS;

    but how do I control these functions outside of "ble_advertising.c". 
    Is there any simple way to manually stop advertising? I tried using ble_advertising_start(BLE_ADV_MODE_IDLE) but since the event BLE_ADV_EVT_IDLE use "sleep_mode_enter()" function which I don't need, I am still struggling with this.

    And thanks again for your help.

  • I've had a quick look again and couldn't initially see where those even triggers are set-up, if at all, but I must ask how do you trigger advertising to start with and are you attempting to debug using breakpoints?

    I ask because if you automatically start advertising on power-up (as the example does) and you're using breakpoints for debugging then this will cause the Soft Device to trigger a reset due to BLE timeouts and advertising will restart.

Reply
  • I've had a quick look again and couldn't initially see where those even triggers are set-up, if at all, but I must ask how do you trigger advertising to start with and are you attempting to debug using breakpoints?

    I ask because if you automatically start advertising on power-up (as the example does) and you're using breakpoints for debugging then this will cause the Soft Device to trigger a reset due to BLE timeouts and advertising will restart.

Children
  • Hi cbd,

    I've had a quick look again and couldn't initially see where those even triggers are set-up, if at all, but I must ask how do you trigger advertising to start with and are you attempting to debug using breakpoints?

    I am waiting for the uart command and start advertising when I recieved the command.

    I ask because if you automatically start advertising on power-up (as the example does) and you're using breakpoints for debugging then this will cause the Soft Device to trigger a reset due to BLE timeouts and advertising will restart.

    No, I run the code in the release mode.

    Why the advertisement process seems quite complicated for me? I used ble_app_uart_c as a reference for the central role and I can manage to scan on and off successfully using the uart command. And I also tried to look for the code which calls the advertising_start function but I couldn't find. Please help. 

  • Hi,

    Here's some code from a current project that doesn't rely any library application code.

    ///############################################################################
    /// @fn              BLE_AdvertisingInit
    /// @remark          None
    /// @return          ret_code_t error code
    /// @see Description Initialises the advertising functionality.
    ///                  Encodes and passes to the BLE stack data that it uses both
    ///                  before and during advertising
    /// @warning
    ///#############################################################################
    ret_code_t BLE_AdvertisingInit(void)
    {
        ret_code_t err_code;
    
        ble_lms_t lms = LMS_GetServiceData();
    
        /* Data space is limited within the advertising packet. */
        /* Advertise custom service for easy pickup */
        // TODO: maybe replace the data with manufacturing data */
        ble_uuid_t adv_uuids[] = {{LMS_UUID_SERVICE, lms.uuid_type}};
    
    
        /* Build and set advertising data */
        ble_advdata_t advdata;
        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;
    
        err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
        APP_ERROR_CHECK(err_code);
    
    
        /* Build and set scan response data */
        ble_advdata_t srdata;
        memset(&srdata, 0, sizeof(srdata));
        srdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
        srdata.uuids_complete.p_uuids = adv_uuids;
    
        err_code =
            ble_advdata_encode(&srdata, m_adv_data.scan_rsp_data.p_data, &m_adv_data.scan_rsp_data.len);
        APP_ERROR_CHECK(err_code);
    
        /* Set advertising parameters */
        ble_gap_adv_params_t adv_params;
        memset(&adv_params, 0, sizeof(adv_params));
    
        adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
        adv_params.duration = APP_ADV_DURATION;
        adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
        adv_params.p_peer_addr = NULL;
        adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
        adv_params.interval = APP_ADV_INTERVAL;
    
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params);
        APP_ERROR_CHECK(err_code);
        return err_code;
    }
    
    ///############################################################################
    /// @fn              BLE_AdvertisingStart
    /// @remark          None
    /// @return          ret_code_t error code
    /// @see Description Triggers BLE advertising through the SoftDevice
    /// @warning
    ///#############################################################################
    ret_code_t BLE_AdvertisingStart(void)
    {
        ret_code_t err_code;
    
        err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
        APP_ERROR_CHECK(err_code);
        NRF_LOG_INFO("BLE Advertising started.");
        return err_code;
    }
    
    ///############################################################################
    /// @fn              BLE_AdvertisingStop
    /// @remark          None
    /// @return          ret_code_t error code
    /// @see Description Terminate BLE advertising through the SoftDevice
    /// @warning
    ///#############################################################################
    ret_code_t BLE_AdvertisingStop(void)
    {
        ret_code_t err_code = NRF_SUCCESS;
    
        if (m_adv_handle != BLE_GAP_ADV_SET_HANDLE_NOT_SET)
        {
            NRF_LOG_INFO("BLE Stop advertising.");
            err_code = sd_ble_gap_adv_stop(m_adv_handle);
            if (err_code != NRF_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }
            m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
        }
    
        return err_code;
    }
    

    I extracted library app code relating to Bluetooth into my own code file. The above code is what I'm using to set-up, start and stop advertising on my current project.

    I hope that this helps.

  • Hi cbd, thanks so much for your help, please let me test in my application and get back to you again.

  • Hi cbd, I fount out this which I think quite related to my issues. Here I found out ble_advertising_on_ble_evt() function and all the advertising event on termination, disconnection and connection implemented here.

    void ble_advertising_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ble_advertising_t * p_advertising = (ble_advertising_t *)p_context;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                on_connected(p_advertising, p_ble_evt);
                break;
    
            // Upon disconnection, whitelist will be activated and direct advertising is started.
            case BLE_GAP_EVT_DISCONNECTED:
                on_disconnected(p_advertising, p_ble_evt);
                break;
    
            // Upon terminated advertising (time-out), the next advertising mode is started.
            case BLE_GAP_EVT_ADV_SET_TERMINATED:
                on_terminated(p_advertising, p_ble_evt);
                break;
    
            default:
                break;
        }
    }

    But how can I control these functions not to advertise again? Please help me to solve this and thanks.

  • Hi,

    I don't know if you have solved your problem or not.

    on_connected, on_disconnected and on_terminated are just calls to functions to handle behaviour of the advertising when certain Bluetooth events occur.

    If you're only triggering advertising based on your UART input then I would be tempted to just comment out the calls to see if that helps.

    I suspect that on_connected will just disable advertising.

Related