cancel
Showing results for 
Search instead for 
Did you mean: 

Advertising timeout and low power advertising with latest CubeMX

crwper
Senior

I'm having an oddly difficult time seeing how to do something quite simple. In older CubeMX projects, the `Adv_Request` function looked like this:

 

static void Adv_Request(APP_BLE_ConnStatus_t NewStatus)
{
  tBleStatus ret = BLE_STATUS_INVALID_PARAMS;
  uint16_t Min_Inter, Max_Inter;

  if (NewStatus == APP_BLE_FAST_ADV)
  {
    Min_Inter = AdvIntervalMin;
    Max_Inter = AdvIntervalMax;
  }
  else
  {
    Min_Inter = CFG_LP_CONN_ADV_INTERVAL_MIN;
    Max_Inter = CFG_LP_CONN_ADV_INTERVAL_MAX;
  }

  /**
   * Stop the timer, it will be restarted for a new shot
   * It does not hurt if the timer was not running
   */
  HW_TS_Stop(BleApplicationContext.Advertising_mgr_timer_Id);

  if ((NewStatus == APP_BLE_LP_ADV)
      && ((BleApplicationContext.Device_Connection_Status == APP_BLE_FAST_ADV)
          || (BleApplicationContext.Device_Connection_Status == APP_BLE_LP_ADV)))
  {
    /* Connection in ADVERTISE mode have to stop the current advertising */
    ret = aci_gap_set_non_discoverable();
    if (ret != BLE_STATUS_SUCCESS)
    {
      APP_DBG_MSG("==>> aci_gap_set_non_discoverable - Stop Advertising Failed , result: %d \n", ret);
    }
    else
    {
      APP_DBG_MSG("==>> aci_gap_set_non_discoverable - Successfully Stopped Advertising \n");
    }
  }

  BleApplicationContext.Device_Connection_Status = NewStatus;
  /* Start Fast or Low Power Advertising */
  ret = aci_gap_set_discoverable(ADV_IND,
                                 Min_Inter,
                                 Max_Inter,
                                 CFG_BLE_ADDRESS_TYPE,
                                 NO_WHITE_LIST_USE, /* use white list */
                                 sizeof(a_LocalName),
                                 (uint8_t*) &a_LocalName,
                                 BleApplicationContext.BleApplicationContext_legacy.advtServUUIDlen,
                                 BleApplicationContext.BleApplicationContext_legacy.advtServUUID,
                                 0,
                                 0);
  if (ret != BLE_STATUS_SUCCESS)
  {
    APP_DBG_MSG("==>> aci_gap_set_discoverable - fail, result: 0x%x \n", ret);
  }
  else
  {
    APP_DBG_MSG("==>> aci_gap_set_discoverable - Success\n");
  }

  /* Update Advertising data */
  ret = aci_gap_update_adv_data(sizeof(a_ManufData), (uint8_t*) a_ManufData);
  if (ret != BLE_STATUS_SUCCESS)
  {
    if (NewStatus == APP_BLE_FAST_ADV)
    {
      APP_DBG_MSG("==>> Start Fast Advertising Failed , result: %d \n\r", ret);
    }
    else
    {
      APP_DBG_MSG("==>> Start Low Power Advertising Failed , result: %d \n\r", ret);
    }
  }
  else
  {
    if (NewStatus == APP_BLE_FAST_ADV)
    {
      APP_DBG_MSG("==>> Success: Start Fast Advertising \n\r");
      /* Start Timer to STOP ADV - TIMEOUT - and next Restart Low Power Advertising */
      HW_TS_Start(BleApplicationContext.Advertising_mgr_timer_Id, INITIAL_ADV_TIMEOUT);
    }
    else
    {
      APP_DBG_MSG("==>> Success: Start Low Power Advertising \n\r");
    }
  }

  return;
}

 

However, in the latest code generated by CubeMX, the function now looks like this:

 

static void Adv_Request(APP_BLE_ConnStatus_t NewStatus)
{
  tBleStatus ret = BLE_STATUS_INVALID_PARAMS;

  BleApplicationContext.Device_Connection_Status = NewStatus;
  /* Start Fast or Low Power Advertising */
  ret = aci_gap_set_discoverable(ADV_TYPE,
                                 CFG_FAST_CONN_ADV_INTERVAL_MIN,
                                 CFG_FAST_CONN_ADV_INTERVAL_MAX,
                                 CFG_BLE_ADDRESS_TYPE,
                                 ADV_FILTER,
                                 0,
                                 0,
                                 0,
                                 0,
                                 0,
                                 0);
  if (ret != BLE_STATUS_SUCCESS)
  {
    APP_DBG_MSG("==>> aci_gap_set_discoverable - fail, result: 0x%x \n", ret);
  }
  else
  {
    APP_DBG_MSG("==>> aci_gap_set_discoverable - Success\n");
  }

/* USER CODE BEGIN Adv_Request_1*/

/* USER CODE END Adv_Request_1*/

  /* Update Advertising data */
  ret = aci_gap_update_adv_data(sizeof(a_AdvData), (uint8_t*) a_AdvData);
  if (ret != BLE_STATUS_SUCCESS)
  {
      APP_DBG_MSG("==>> Start Fast Advertising Failed , result: %d \n\r", ret);
  }
  else
  {
      APP_DBG_MSG("==>> Success: Start Fast Advertising \n\r");
  }

  return;
}

 

In my application, I would like to have the device start in fast advertising mode, and after 30 seconds, change to low power advertising. This is what examples like the `BLE_HeartRate` example do, but these examples all seem to use the older version of `Adv_Request`.

I could certainly hack this in myself, but I would prefer to see an example from ST showing how the new function should be used to accommodate the same functionality. I assume ST had something in mind when this change was made, and rather than trying to work against that vision, I would prefer to work within it where possible.

Is such an example available?

1 ACCEPTED SOLUTION

Accepted Solutions
Remy ISSALYS
ST Employee

Hello,

Indeed, the adv_request function implementation is a bit different in BLE Custom template than in BLE HeartRate. You can implement this mechanism in BLE Custom template by adding a timer and a task to manage this different advertising interval as it's done in BLE HeartRate example. All the code can be added in user section. There is not specific example on that but it's very similar to BLE HeartRate. 

Best Regards 

View solution in original post

2 REPLIES 2
Remy ISSALYS
ST Employee

Hello,

Indeed, the adv_request function implementation is a bit different in BLE Custom template than in BLE HeartRate. You can implement this mechanism in BLE Custom template by adding a timer and a task to manage this different advertising interval as it's done in BLE HeartRate example. All the code can be added in user section. There is not specific example on that but it's very similar to BLE HeartRate. 

Best Regards 

Thank you! I thought it might wind up there, but wanted to make sure that I wasn't missing a more elegant solution.