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

Stop modify starting a connectable advertisement not restarting

Hi,

I have some code which is based on the ble_app_gatts_c example sdk 15. I am using a Nordic NRF52840-dk pca10056 s140. Coding in Segger.

I want to each second change the live data advertisement that is being transmitted from the Nordic board. I have this code working without a Gatt connection but, want to merge the two projects together.

I have live data coming in each second via a UART connection containing sensor data which I then need to advertise.

However, when I create a loop in the main containing the advertisement mod (custom function) I makes a infinite loop for advertisement being stopped and, no advert appears.

My understanding to update and advert is as follows:

  • Encode new data my case manufacturer data with sensor values in it
  • Stop advertising
  • Configure the data to the advert
  • Restart the advert

This is how it works in my code that is not connectable.

My code runs a advertising_init then starts the advert then goes into the main loop where, the advert is to be updated but, crashes upon attempt to restart.

Below is my code with the problem.

void advertising_init()
{
    ret_code_t             err_code;
    ble_advertising_init_t init;


    ble_advdata_manuf_data_t manuf_specific_data;


    memset(&init, 0, sizeof(init));
    //Manufacturer data
    ble_advdata_manuf_data_t                  manuf_data; //Variable to hold manufacturer specific data   
    manuf_data.company_identifier             =  0x0487; //Centricas company ID
    manuf_data.data.p_data                    = m_beacon_info;
    manuf_data.data.size                      = 15;//Was 11
    
    init.advdata.p_manuf_specific_data = &manuf_data;

    init.advdata.name_type            = BLE_ADVDATA_FULL_NAME;
    init.advdata.include_appearance   = true;
    init.advdata.flags                = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    

    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;

    init.evt_handler = on_adv_evt;

    err_code = ble_advertising_init(&m_advertising, &init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);

    err_code = ble_advertising_restart_without_whitelist(&m_advertising);
    APP_ERROR_CHECK(err_code);
}
//Mod code

static void advertising_mod(int count, int T1_value,int T2_value,
                                       int T3_value,int T4_value, float Pdiff_value)
{
    ret_code_t             err_code;
    ble_advdata_t            advdata;
    ble_advdata_manuf_data_t manuf_data;
    ble_advertising_init_t init;

    int pdiff_value_1 = 0;
    int pdiff_value_2 = 0;
    int pdiff_value_3 = 0;
    float pressure_to_convert = 0;
    int value_of_pressure_data = 0;
    int t1_value_1 = (T1_value >>8) & 0XFF;//MSB shifting
    int t1_value_2 = T1_value & 0XFF;//LSB

    int t2_value_1 = (T2_value >> 8) & 0XFF;//MSB
    int t2_value_2 = T2_value & 0XFF;//LSB

    int t3_value_1 = (T3_value >> 8) & 0XFF;//MSB
    int t3_value_2 = T3_value & 0XFF;//LSB
    
    int t4_value_1 = (T4_value >> 8) & 0XFF;//MSB
    int t4_value_2 = T4_value & 0XFF;//LSB
    //Handling the pressure value

    if(Pdiff_value < 0)//Result is less than 0
    {
        pdiff_value_1 = 0;//For a negative number aka negative pressure
        pressure_to_convert = -Pdiff_value;//Make a positive value
        pressure_to_convert = pressure_to_convert*100;
    }
    else
    {
        pdiff_value_1 = 1;//For a positive pressure
        pressure_to_convert = Pdiff_value;//Keep a positive value
        pressure_to_convert = pressure_to_convert*100;
    }
    value_of_pressure_data = (int)pressure_to_convert;//Convert to an int
    pdiff_value_2 = (value_of_pressure_data >>  8) & 0XFF;
    pdiff_value_3 = (value_of_pressure_data & 0xFF);//LSB

    ble_advdata_manuf_data_t manuf_specific_data;
    m_beacon_info[0]= (unsigned char) count;
    m_beacon_info[1]= (unsigned char) t1_value_1;
    m_beacon_info[2]= (unsigned char) t1_value_2;
    m_beacon_info[3]= (unsigned char) t2_value_1;
    m_beacon_info[4]= (unsigned char) t2_value_2;
    m_beacon_info[5]= (unsigned char) t3_value_1;
    m_beacon_info[6]= (unsigned char) t3_value_2;
    m_beacon_info[7]= (unsigned char) t4_value_1;
    m_beacon_info[8]= (unsigned char) t4_value_2;
    m_beacon_info[9]= (unsigned char) pdiff_value_1;//Was sign bit
    m_beacon_info[10]=(unsigned char) pdiff_value_2;
    m_beacon_info[11]=(unsigned char) pdiff_value_3;
    m_beacon_info[12]=(unsigned char) SD_CARD_PRESENT;

    // Build and set advertising data
    memset(&advdata, 0, sizeof(advdata));
    //Manufacturer data
    manuf_data.company_identifier             =  0x0487; //Centricas company ID
    manuf_data.data.p_data                    = m_beacon_info;
    manuf_data.data.size                      = 15;//Was 11
    
    advdata.p_manuf_specific_data = &manuf_data;

    advdata.name_type            = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance   = true;
    advdata.flags                = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    


    memset(&init, 0, sizeof(init));
    //Manufacturer data
    manuf_data.company_identifier             =  0x0487; //Centricas company ID
    manuf_data.data.p_data                    = m_beacon_info;
    manuf_data.data.size                      = 15;//Was 11
    
    init.advdata.p_manuf_specific_data = &manuf_data;

    init.advdata.name_type            = BLE_ADVDATA_FULL_NAME;
    init.advdata.include_appearance   = true;
    init.advdata.flags                = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    

    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;

    init.evt_handler = on_adv_evt;


    ble_advertising_encode(&m_advertising, &init);//Encode the data
    ble_advertising_stop(&m_advertising);//Custom stop function
  
    
    ble_advertising_configure(&m_advertising, &init);//Configure the data
    
    err_code =ble_advertising_start_mod(&m_advertising);//Custom start function                       
    APP_ERROR_CHECK(err_code);
}

This code is the modify code for my other application which works however the init and mod are slightly different.

Please could you explain where I am going wrong thanks

static void advertising_init(void)
{
    ret_code_t               err_code;
    ble_advdata_t            advdata;
    ble_advdata_manuf_data_t manuf_data;
    uint8_t                  flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;

    APP_ERROR_CHECK_BOOL(sizeof(flags) == ADV_FLAGS_LEN);  // Assert that these two values of the same.

    // Build and set advertising data
    memset(&advdata, 0, sizeof(advdata));

    manuf_data.company_identifier = COMPANY_IDENTIFIER;
    manuf_data.data.size          = APP_BEACON_INFO_LENGTH;
    manuf_data.data.p_data  = (uint8_t *) m_beacon_info;
    advdata.flags                 = flags;
    advdata.p_manuf_specific_data = &manuf_data;

    err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
    APP_ERROR_CHECK(err_code);

    if(m_is_non_connectable_mode)
    {
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, NULL, &m_adv_params);
        APP_ERROR_CHECK(err_code);
    }
    else
    {
        err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
        APP_ERROR_CHECK(err_code);
    }
}
static void advertising_mod(int count, int T1_value,int T2_value,
                                       int T3_value,int T4_value, float Pdiff_value)
{
    uint32_t      err_code;
    ble_advdata_t advdata;
    uint8_t       flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;

 
    int pdiff_value_1 = 0;
    int pdiff_value_2 = 0;
    int pdiff_value_3 = 0;
    float pressure_to_convert = 0;
    int value_of_pressure_data = 0;
    int t1_value_1 = (T1_value >>8) & 0XFF;//MSB shifting
    int t1_value_2 = T1_value & 0XFF;//LSB

    int t2_value_1 = (T2_value >> 8) & 0XFF;//MSB
    int t2_value_2 = T2_value & 0XFF;//LSB

    int t3_value_1 = (T3_value >> 8) & 0XFF;//MSB
    int t3_value_2 = T3_value & 0XFF;//LSB
    
    int t4_value_1 = (T4_value >> 8) & 0XFF;//MSB
    int t4_value_2 = T4_value & 0XFF;//LSB
    //Handling the pressure value

    if(Pdiff_value < 0)//Result is less than 0
    {
        pdiff_value_1 = 0;//For a negative number aka negative pressure
        pressure_to_convert = -Pdiff_value;//Make a positive value
        pressure_to_convert = pressure_to_convert*100;
    }
    else
    {
        pdiff_value_1 = 1;//For a positive pressure
        pressure_to_convert = Pdiff_value;//Keep a positive value
        pressure_to_convert = pressure_to_convert*100;
    }
    value_of_pressure_data = (int)pressure_to_convert;//Convert to an int
    pdiff_value_2 = (value_of_pressure_data >>  8) & 0XFF;
    pdiff_value_3 = (value_of_pressure_data & 0xFF);//LSB

    int Time_stamp_id_value_1 = (Time_stamp_id >> 24) & 0XFF;//MSB
    int Time_stamp_id_value_2 = (Time_stamp_id >> 16) & 0XFF;//MSB
    int Time_stamp_id_value_3 = (Time_stamp_id >> 8) & 0XFF;//MSB
    int Time_stamp_id_value_4 = (Time_stamp_id) & 0XFF;//MSB


    ble_advdata_manuf_data_t manuf_specific_data;
    m_beacon_info[0]= (unsigned char) count;
    m_beacon_info[1]= (unsigned char) t1_value_1;
    m_beacon_info[2]= (unsigned char) t1_value_2;
    m_beacon_info[3]= (unsigned char) t2_value_1;
    m_beacon_info[4]= (unsigned char) t2_value_2;
    m_beacon_info[5]= (unsigned char) t3_value_1;
    m_beacon_info[6]= (unsigned char) t3_value_2;
    m_beacon_info[7]= (unsigned char) t4_value_1;
    m_beacon_info[8]= (unsigned char) t4_value_2;
    m_beacon_info[9]= (unsigned char) pdiff_value_1;//Was sign bit
    m_beacon_info[10]=(unsigned char) pdiff_value_2;
    m_beacon_info[11]=(unsigned char) pdiff_value_3;
    m_beacon_info[12]=(unsigned char) SD_CARD_PRESENT;
    m_beacon_info[13]=(unsigned char) Time_stamp_id_value_1;
    m_beacon_info[14]=(unsigned char) Time_stamp_id_value_2;
    m_beacon_info[15]=(unsigned char) Time_stamp_id_value_3;
    m_beacon_info[16]=(unsigned char) Time_stamp_id_value_4;
    //SD_CARD_PRESENT
   
    manuf_specific_data.company_identifier = COMPANY_IDENTIFIER;
    manuf_specific_data.data.p_data = (uint8_t *) m_beacon_info;
    manuf_specific_data.data.size   = APP_BEACON_INFO_LENGTH;

    // Build and set advertising data.
    memset(&advdata, 0, sizeof(advdata));

    advdata.name_type             = BLE_ADVDATA_NO_NAME;
    advdata.flags                 = flags;
    advdata.p_manuf_specific_data = &manuf_specific_data;

    // Initialize advertising parameters (used when starting advertising).
    memset(&m_adv_params, 0, sizeof(m_adv_params));

    
    m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
    m_adv_params.p_peer_addr     = NULL;    // Undirected advertisement.
    m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
    m_adv_params.interval        = CONNECTABLE_ADV_INTERVAL;
    m_adv_params.duration        = APP_ADV_DURATION;       // 30 Second adverts
    m_adv_params.primary_phy     = BLE_GAP_PHY_1MBPS;

    err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
    APP_ERROR_CHECK(err_code);

    err_code = sd_ble_gap_adv_stop(m_adv_handle);
    APP_ERROR_CHECK(err_code);

    err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
    APP_ERROR_CHECK(err_code);

    err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
    APP_ERROR_CHECK(err_code);
}

Parents Reply Children
No Data
Related