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

Periodically switching into config mode on nRF51822 Beacon Kit

Hi,

I have implemented some code to periodically switching back and forth between the normal and the config mode. I can see that the handler function is being called, but the beacon does not switch into the config mode. The following is what I implemented:

beacon_mode_t beacon_mode = beacon_mode_normal;

static void config_mode_timeout_handler(void * p_context) {

	if(beacon_mode == beacon_mode_normal)
	{
		beacon_mode = beacon_mode_config;
	}
	else
	{
		beacon_mode = beacon_mode_normal;
	}
	
	advertising_init(beacon_mode);

}

Apparently calling advertising_init with beacon_config_mode is not enough to do this, but I don't know what other functions I should be calling for this. Thanks a lot in advance!

Parents
  • @Sarah:

    If you have a look at our code in beacon_setup() in main.c you can find that we need to init some parameter such as gap, services, connection parameter, security when we are in config mode.

    For your application, since you are switching back and forth a lot, I would suggest you to do this configuration at the begining regardless which mode you will be running. Good thing is that you can have this configuration when you running in both modes.

    The main difference that effectively tells apart which mode is currently running is when calling advertising_init(). If you have called the configuration init as mentioned above, you should be fine. Note that you should better call sd_ble_gap_adv_stop() and sd_ble_gap_adv_start() again after you have changed the advertising flags and data in the advertising_init() function.

Reply
  • @Sarah:

    If you have a look at our code in beacon_setup() in main.c you can find that we need to init some parameter such as gap, services, connection parameter, security when we are in config mode.

    For your application, since you are switching back and forth a lot, I would suggest you to do this configuration at the begining regardless which mode you will be running. Good thing is that you can have this configuration when you running in both modes.

    The main difference that effectively tells apart which mode is currently running is when calling advertising_init(). If you have called the configuration init as mentioned above, you should be fine. Note that you should better call sd_ble_gap_adv_stop() and sd_ble_gap_adv_start() again after you have changed the advertising flags and data in the advertising_init() function.

Children
  • Thanks very much Hung. Just to clarify what I'm trying to do, I want the beacon to act exactly like the sw2 button is pressed. The beacon becomes connectable and the led light changes to indicate this. So I use the following as my new beacon_setup function now, and I still do not see any change. Why could that be?

    static void beacon_setup(beacon_mode_t mode)

    {

    //if(mode == beacon_mode_config)
    //{
        gap_params_init();
        services_init();
       // advertising_init(beacon_mode_config);
        conn_params_init();
        sec_params_init();
        led_softblink_off_time_set(2000);
        led_softblink_start(APP_CONFIG_MODE_LED_MSK);
    //}
    //else
    //{
        advertising_init(beacon_mode_normal);
        
        if (p_beacon->data.led_state)
        {
            led_softblink_start(APP_BEACON_MODE_LED_MSK);
        }
    //}    
    

    }

    //My handler for switching between modes

    static void config_mode_timeout_handler(void * p_context) {

    	if(m_beacon_mode == beacon_mode_normal)
    	{
    		m_beacon_mode = beacon_mode_config;
    	}
    	else
    	{
    		m_beacon_mode = beacon_mode_normal;
    	}
    	
    	advertising_stop();
    	advertising_init(m_beacon_mode);
    	advertising_start();
    

    } Also, do you think it would work if I just called beacon_start function with both beacon_mode_config and then with beacon_mode_normal? (This doesn't seem to work. Beacon stops advertising after it starts up, I'm not sure why)

  • @Sarah: I don't see any reason it shouldn't work. The main difference between the 2 mode is the advertising type. When in normal mode it advertise with non connectable BLE_GAP_ADV_TYPE_ADV_NONCONN_IND type, when in configure more it's BLE_GAP_ADV_TYPE_ADV_IND type.

    I think you should check and make sure you have NRF_SUCCESS return when calling each of the functions when switching. To do that you can add a break point in the error assert handler, and look for the err_code, line and file name that cause the issue.

  • When I update the beacon_setup function like you mentioned, I discovered that the config_mode_timeout_handler function is never even called. I debugged the code and saw that app_timer_create function returns with NRF_ERROR_NO_MEM for some reason (hence I believe timer is never called and timer handler is never run). How can I fix this?

  • Hi Sarah,

    Please make sure you have increased the APP_TIMER_MAX_TIMERS define to match with the number of timer you used.

  • I thought I had fixed that, but apparently not. Thanks so much for your Help Hung! It works now :)

Related