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

periodic advertising and sleep mode

link text from this link i want to advertise for 10 second then i want to stop advertise for 20 second then again advertise for 10 second. i want this continuously. for that i have defined timer

static void timers1_init(void)
{
    uint32_t err_code;

    // Initialize timer module.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);

    // Create timers.
    err_code = app_timer_create(&abc,
                               APP_TIMER_MODE_SINGLE_SHOT,
                                timeout_handler);

//APP_TIMER_MODE_SINGLE_SHOT-The timer will expire only once.
//APP_TIMER_MODE_REPEATED-The timer will restart each time it expires.

    APP_ERROR_CHECK(err_code);
}

static void application_timers_start(void)
{
    uint32_t err_code;

    // Start application timers.
    err_code = app_timer_start(abc, INTERVAL, NULL);
    APP_ERROR_CHECK(err_code);
}


static void timeout_handler(void * p_context)
{
    UNUSED_PARAMETER(p_context);
	sd_ble_gap_adv_stop ();
	//power_manage();
    
}
  • i have edited code for first timer i define period for two timers

    #define INTERVAL     APP_TIMER_TICKS(10000, APP_TIMER_PRESCALER) 
    #define INTERVAL1     APP_TIMER_TICKS(20000, APP_TIMER_PRESCALER)
    

    created second timer

    APP_TIMER_DEF(abc1);
    
    static void timeout_handler1(void * p_context)
    {
    	uint32_t err_code;
        UNUSED_PARAMETER(p_context);
    	advertising_start();
    	//err_code=sd_ble_gap_adv_start(BLE_ADV_MODE_FAST);
    //	 err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    	//power_manage();
        
    }
    
  • static void timers2_init(void) { uint32_t err_code;

        // Initialize timer module.
        APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    
        // Create timers.
        err_code = app_timer_create(&abc1,
                                   APP_TIMER_MODE_SINGLE_SHOT,
                                    timeout_handler1);
    
    //APP_TIMER_MODE_SINGLE_SHOT-The timer will expire only once.
    //APP_TIMER_MODE_REPEATED-The timer will restart each time it expires.
    
        APP_ERROR_CHECK(err_code);
    }
    

    static void application_timers1_start(void) { uint32_t err_code;

    // Start application timers.
    err_code = app_timer_start(abc1, INTERVAL1, NULL);
    APP_ERROR_CHECK(err_code);
    

    }

  • i called these two timers in main code.

    int main(void)
    {
        // Initialize.
        leds_init();
        timers_init();
        buttons_init();
        ble_stack_init();
        gap_params_init();
        services_init();
        advertising_init();
    	timers1_init();
    	timers2_init();
        conn_params_init();
    
        // Start execution.
        advertising_start();
    	application_timers_start();
    	application_timers1_start();
    //advertising_start();
        // Enter main loop.
        for (;;)
        {
           // power_manage();
        }
    }
    
  • static void advertising_init(void) { uint32_t err_code; ble_advdata_t advdata; ble_advdata_t scanrsp;

        ble_uuid_t adv_uuids[] = {{LBS_UUID_SERVICE, m_lbs.uuid_type}};
    
        // Build and set advertising data
        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;
    
    
        memset(&scanrsp, 0, sizeof(scanrsp));
        scanrsp.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
        scanrsp.uuids_complete.p_uuids  = adv_uuids;
    
        err_code = ble_advdata_set(&advdata, &scanrsp);
        APP_ERROR_CHECK(err_code);
    }
    static void advertising_start(void)
    {
        uint32_t             err_code;
        ble_gap_adv_params_t adv_params;
    
        // Start advertising
        memset(&adv_params, 0, sizeof(adv_params));
    
        adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
        adv_params.p_peer_addr = NULL;
        adv_params.fp          = BLE_GAP_ADV_FP_ANY;
        adv_params.interval    = APP_ADV_INTERVAL;
        adv_params.timeout     = APP_ADV_TIMEOUT_IN_SECONDS;
    
        err_code = sd_ble_gap_adv_start(&adv_params);
        APP_ERROR_CHECK(err_code);
        LEDS_ON(ADVERTISING_LED_PIN);
    }
    

    these are my advertising function

  • after i run this code on nrf51 dk. my nrf51 advertising for 10 s. then it off for 10 s. then it continuously advertising. you gave me solution in last comment. but i can't understand it. can you tell me how to do it??

Related