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

ble advertising start probelm

In my application, I execute below code to start advertise when a button is pressed

                        err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
                        APP_ERROR_CHECK(err_code);

When I press the button for more than once, the system reset. It seems that when I execute above code when the advertising has already started, the system will reset. Can you tell me how to solve it. One of the possible ways is to check the advertise status, if it has already started, above code will not executed. Please tell me how to check the advertise is started or not. Please also advise other methods if you have.

Parents
  • Hi,

    As described in this answer, there is no way of checking the current state of advertising. You should implement a flag in your application where you keep track of whether you are advertising or not.

    If you have a connectable peripheral with two buttons, one for starting and one for stopping advertisingcould, an example could be like this:

    bool is_advertising = false;
    
    void button_1_handler(void) // Start advertising
    {
    	if(!is_advertising)
    	{
    		err_code = sd_ble_gap_adv_start();
    		if (err_code == NRF_SUCCESS){
    			is_advertising = true;
    		}
    		else{
    			APP_ERROR_CHECK(err_code);
    		}
    	}
    }
    
    void button_2_handler(void) // Stop advertising
    {
    	if(is_advertising)
    	{
    		err_code = sd_ble_gap_adv_stop();
    		if (err_code == NRF_SUCCESS){
    			is_advertising = false;
    		}
    		else{
    			APP_ERROR_CHECK(err_code);
    		}
    	}
    }
    
    static void on_ble_evt(ble_evt_t * p_ble_evt)
    {
        uint32_t err_code;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
    			is_advertising = false; // Softdevice stops advertising when in a connection
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                if(!is_advertising)
    			{
    				err_code = sd_ble_gap_adv_start(); // Start advertising when no longer connected
    				if(err_code == NRF_SUCCESS){
    					is_advertising = true;
    				}
    				else{
    					APP_ERROR_CHECK(err_code);
    				}
    			}
                break;
    
            case BLE_GAP_EVT_TIMEOUT:
                if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
    			{
    				is_advertising = false; // If advertising times out
    			}
                break;
    	}
    }
    

    Remember that you need to keep track of the advertising state if you start or stop advertising elsewhere in your code.

    Best regards,

    Jørgen

  • I'm sorry, there was a typo in the code. It should be: p_ble_evt->evt.gap_evt.params.timeout.src. I updated the code after posting the original reply (you can click the updated [time] on the answer to see changelog), maybe you did not get an update mail.

Reply Children
No Data
Related