2 Replies Latest reply on Dec 31, 2019 8:04 AM by LePo_1062026

    What is the official way to change the Advertising Interval at run-time?

    LePo_1062026

      I'm new to the BLE ecosystem so I'm not familiar with all the many API calls to control BLE operations.

       

      I've created a BLE in Broadcast mode and I want to change the advertising interval in the App at run-time depending on conditions (such as battery voltage depletion).

       

      I've been successful in doing this by using the following code.  Howewver, it requires I modify the config parameters before a Cy_BLE_Start() or Cy_BLE_Stop() then modify the interval parameters along with setting cy_ble_initVar = 0 then performing a Cy_BLE_Start().

      #define BLE_APP_fastAdvIntervalMin    33.33                         // desired value in ms
      #define BLE_APP_fastAdvIntervalMax    BLE_APP_fastAdvIntervalMin    // desired value in ms
      
      #define BLE_APP_Adv_Interval_res    1.6
      #define BLE_APP_FASTADVINTERVALMIN    (uint16)(BLE_APP_fastAdvIntervalMin*BLE_APP_Adv_Interval_res)    // This is the value used in cy_stc_ble_gapp_adv_params_t
      #define BLE_APP_FASTADVINTERVALMAX    (uint16)(BLE_APP_fastAdvIntervalMax*BLE_APP_Adv_Interval_res)    // This is the value used in cy_stc_ble_gapp_adv_params_t
      
      /* Change some of the initialization parameters.
        These changes can only take effect if cy_ble_initVar = 0 and then the Cy_BLE_Start() or Cy_BLE_Init() is called */
      cy_ble_config.gappAdvParams->fastAdvIntervalMax = BLE_APP_FASTADVINTERVALMAX;
      cy_ble_config.gappAdvParams->fastAdvIntervalMin = BLE_APP_FASTADVINTERVALMIN;
      
      /* Start Host of BLE Component and register generic event handler */
          Cy_BLE_Start(StackEventHandler);
      

       

      I've looked at the PDL docs for the BLE and the actual middleware .c files and couldn't find a API call to perform this function.

       

      Am I doing this the "official' way or is there a correct API call I'm missing?

       

      Len

        • 1. Re: What is the official way to change the Advertising Interval at run-time?
          NazarP_56

          Hi Len,

           

          There are no addition API to do this. Your way is almost correct, but please looks my comments below:

           

           

          You can do this in the following ways:

           

          1. Update existing Broadcast configuration: 

          a. Stop current advertising:    

          Cy_BLE_GAPP_StopAdvertisement()

           

          b. Update Broadcast configuration:

          cy_ble_config.gappAdvParams[CY_BLE_BROADCASTER_CONFIGURATION_0_INDEX].fastAdvIntervalMax =    BLE_APP_FASTADVINTERVALMAX;

           

          cy_ble_config.gappAdvParams[CY_BLE_BROADCASTER_CONFIGURATION_0_INDEX].fastAdvIntervalMax = BLE_APP_FASTADVINTERVALMAX;

           

          NOTE1:  gappAdvParams is a pointer to an array of structures, you need to use index to choose correct one (CY_BLE_BROADCASTER_CONFIGURATION_0_INDEX - this define is generated by BLE configurator)

          NOTE2:  you do not need to call Cy_BLE_Stop/Cy_BLE_Start after gappAdvParams update...

           

          c. Start advertising with updated configuration:

          Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST, CY_BLE_BROADCASTER_CONFIGURATION_0_INDEX );

           

          2. Also one more way: create different configurations in BLE Customizer (GAP Settings, ADD->Brodcaster configurations).
          in application code use Cy_BLE_GAPP_StartAdvertisement with selected configuration:
          Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST, XXXX); , where XXXX is one of defined configurations:

          - CY_BLE_BROADCASTER_CONFIGURATION_0_INDEX
          - CY_BLE_BROADCASTER_CONFIGURATION_1_INDEX

          #1 - It looks more suitable for yours case.

           


          Regards,
          Nazar

          3 of 3 people found this helpful
          • 2. Re: What is the official way to change the Advertising Interval at run-time?
            LePo_1062026

            Nazar,

             

            Once again.  Your information was spot-on!

             

            To those monitoring this discussion:  I have some code frags of each of the two suggestions from Nazar and one additional one.

            In each case, the code uses  Cy_BLE_GAPP_StopAdvertisement() and then Cy_BLE_GAPP_StartAdvertisement() .

             

            Suggestion 1:  Change the Advertisement parameters.

            #define BLE_APP_fastAdvIntervalMin    33.33   // desired value in ms
            #define BLE_APP_fastAdvIntervalMax    BLE_APP_fastAdvIntervalMin    // desired value in ms
            #define BLE_APP_slowAdvIntervalMin    500.0   // desired value in ms 
            #define BLE_APP_slowAdvIntervalMax    BLE_APP_slowAdvIntervalMin    // desired value in ms
            
            #define BLE_APP_Adv_Interval_res    1.6
            #define BLE_APP_FASTADVINTERVALMIN    (uint16)(BLE_APP_fastAdvIntervalMin*BLE_APP_Adv_Interval_res)    // This is the value used in cy_stc_ble_gapp_adv_params_t
            #define BLE_APP_FASTADVINTERVALMAX    (uint16)(BLE_APP_fastAdvIntervalMax*BLE_APP_Adv_Interval_res)    // This is the value used in cy_stc_ble_gapp_adv_params_t
            #define BLE_APP_SLOWADVINTERVALMIN    (uint16)(BLE_APP_slowAdvIntervalMin*BLE_APP_Adv_Interval_res)    // This is the value used in cy_stc_ble_gapp_adv_params_t
            #define BLE_APP_SLOWADVINTERVALMAX    (uint16)(BLE_APP_slowAdvIntervalMax*BLE_APP_Adv_Interval_res)    // This is the value used in cy_stc_ble_gapp_adv_params_t
            
            void BLE_Adv_SetInterval(_Bool slow_adv)
            {
                Cy_BLE_GAPP_StopAdvertisement();
                while(Cy_BLE_GetAdvertisementState() != CY_BLE_ADV_STATE_STOPPED){Cy_BLE_ProcessEvents();}    // wait until advertising stops.
                if(slow_adv == false)
                {    // Fast Advert
                    cy_ble_config.gappAdvParams[0].fastAdvIntervalMax = BLE_APP_FASTADVINTERVALMAX;
                    cy_ble_config.gappAdvParams[0].fastAdvIntervalMin = BLE_APP_FASTADVINTERVALMIN;
                }
                else
                {    // Slow Advert
                    cy_ble_config.gappAdvParams[0].fastAdvIntervalMax = BLE_APP_SLOWADVINTERVALMAX;
                    cy_ble_config.gappAdvParams[0].fastAdvIntervalMin = BLE_APP_SLOWADVINTERVALMIN;
                }
                 Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_SLOW, CY_BLE_BROADCASTER_CONFIGURATION_0_INDEX);
            }
            

             

            Suggestion 2:  Use of multiple GAP configuration.

            Set the "configuration 0/Advertisement settings/Advertising interval/Fast advertising interval:/Minimum" and ".../Maximum" values if not already set.

            Next "Add" a new configuration (1).  Set the "configuration 1/Advertisement settings/Advertising interval/Fast advertising interval:/Minimum" and ".../Maximum" values.

            void BLE_Adv_SetInterval(_Bool slow_adv)
            {
                Cy_BLE_GAPP_StopAdvertisement();
                while(Cy_BLE_GetAdvertisementState() != CY_BLE_ADV_STATE_STOPPED){Cy_BLE_ProcessEvents();}    // wait until advertising stops.
                if(slow_adv == false)
                {    // Fast Advert
                    Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST, CY_BLE_BROADCASTER_CONFIGURATION_0_INDEX);
                }
                else
                {    // Slow Advert
                    Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST, CY_BLE_BROADCASTER_CONFIGURATION_1_INDEX);
                }
            }
            

             

            New Idea 3:  Use the FAST and SLOW Advertisement parameters

            This is useful if you only have two advertisement intervals.

            First set both FAST and SLOW intervals in the BLE configration on the GAP tab.

            If you don't need to let the FAST interval to default down to the SLOW interval, you can deselect the Fast advertising interval Timeout(s) after setting the SLOW intervals.

            void BLE_Adv_SetInterval(_Bool slow_adv)
            {
                Cy_BLE_GAPP_StopAdvertisement();
                while(Cy_BLE_GetAdvertisementState() != CY_BLE_ADV_STATE_STOPPED){Cy_BLE_ProcessEvents();}    // wait until advertising stops.
                if(slow_adv == false)
                {    // Fast Advert
                    Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_FAST, CY_BLE_BROADCASTER_CONFIGURATION_0_INDEX);    }
                else
                {    // Slow Advert
                    Cy_BLE_GAPP_StartAdvertisement(CY_BLE_ADVERTISING_SLOW, CY_BLE_BROADCASTER_CONFIGURATION_0_INDEX);    }
            }
            

            All these methods are working for me.

             

            I hope this helps someone.  Nazar helped me find a more elegant way to do what I needed.

             

            Len

            1 of 1 people found this helpful