Changing Advertising Interval from my Android App

Hi guys,

I am currently working on a project where the client requires to dynamically change the advertising interval of the nrf52832 device from the mobile app. Below is my implementation, However the advertising interval is remaining the same as initially set by the application after running this function which I call from the on_write event of the service.

 

static uint32_t ble_cus_adv_update_interval(int8_t adv_interval)
{
  uint32_t err_code;
  
  if (conn_handle != BLE_CONN_HANDLE_INVALID)
  {
    err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
    if (err_code != NRF_ERROR_INVALID_STATE)
    {
        APP_ERROR_CHECK(err_code);
    }
  }

  err_code = sd_ble_gap_adv_stop(_p_advertising->adv_handle);
  if (err_code != NRF_ERROR_INVALID_STATE)
  {
    APP_ERROR_CHECK(err_code);
  }


  _p_advertising->adv_params.interval = adv_interval;

  err_code = sd_ble_gap_adv_set_configure(&_p_advertising->adv_handle, NULL, &_p_advertising->adv_params);
  VERIFY_SUCCESS(err_code);

  err_code = sd_ble_gap_adv_start(&_p_advertising->adv_handle, &_p_advertising->conn_cfg_tag);
  VERIFY_SUCCESS(err_code);
}

Any pointers on how to do it or where I am getting it wrong?

Parents
  • I do not think you can configure the peripheral (nRF) device's advertising parameters from the Android app. You most likely are configuring the advertising interval of your phone which can act both as scanner and as an advertiser. So changing the advertising interval on your phone changes the advertising interval of the advertising role of your phone and not the nRF advertiser.

Reply
  • I do not think you can configure the peripheral (nRF) device's advertising parameters from the Android app. You most likely are configuring the advertising interval of your phone which can act both as scanner and as an advertiser. So changing the advertising interval on your phone changes the advertising interval of the advertising role of your phone and not the nRF advertiser.

Children
  • Hi, 

    Thank you for the answer, but I guess my question was not clear enough. I developed a custom service on the nrf52832, in this service, I want to be able to advertise the current advertising interval of the nrf device, in which then anyone using the nrf connect app can change the current advertising interval of the nrf device to what ever value they want to.

    I have managed to mostly get it working, however when I write the advertising interval, I realize that it is not updated on the nrf device, even though a read of the characteristic confirms it to have changed.

    How can I get the value written from the nrf connect app to then be updated to be the current advertising interval? My Implementation is above.

  • I think what you are doing in ble_cus_adv_update_interval should update the advertising interval if _p_advertising is a static global pointer which is still pointing to a valid memory. Also are you sure if ble_cus_adv_update_interval  is being called?

  • Hi, so _p_advertising is a pointer assigned to the same address as m_advertising in the main application.

    I finally figured out that to update the advertising interval, I should change the following parameter

    _p_advertising->adv_modes_config.ble_adv_fast_interval that is found in the ble_advertising_t structure, so the value written from the nrfConnect app will be assigned to the adv_modes_config.ble_adv_fast_interval, which in turn is then used to update the adv_params.interval when the set_adv_mode_fast(p_advertising, &p_advertising->adv_params); is called in the ble_advertising_start function.

    So the final implementation that worked for me is as below

    static uint32_t ble_cus_adv_update_interval(int8_t adv_interval)
    {
      uint32_t err_code;
      
      if (conn_handle != BLE_CONN_HANDLE_INVALID)
      {
        err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
        if (err_code != NRF_ERROR_INVALID_STATE)
        {
            APP_ERROR_CHECK(err_code);
        }
      }
    
      err_code = sd_ble_gap_adv_stop(_p_advertising->adv_handle);
      if (err_code != NRF_ERROR_INVALID_STATE)
      {
        APP_ERROR_CHECK(err_code);
      }
    
    
      _p_advertising->adv_modes_config.ble_adv_fast_interval = adv_interval;
     
    
      err_code = sd_ble_gap_adv_set_configure(&_p_advertising->adv_handle, NULL, &_p_advertising->adv_params);
      VERIFY_SUCCESS(err_code);
    
      err_code = ble_advertising_start(_p_advertising, BLE_ADV_MODE_FAST);
      
      VERIFY_SUCCESS(err_code);
    }
    

Related