This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I want to conditionally switch the advertised UUID to another UUID.

Hello.
It is developed using nrf52832 (S132 v7.0.1, SDK v17.0.0) as a peripheral device.

I want to conditionally switch the advertised UUID to another UUID.
There is "sd_ble_uuid_vs_add" as an API to set the advertisement UUID, but I don't know how to change the advertised UUID once set. Is there a way to change it?

Best regards.

Parents
  • Hello,

    I want to conditionally switch the advertised UUID to another UUID.
    There is "sd_ble_uuid_vs_add" as an API to set the advertisement UUID

    The contents of the advertising payload and the added vendor specific UUID are independent of each other. The sd_ble_uuid_vs_add function adds the vendor specific UUID to the SoftDevice table - it does not populate the advertising payload.
    The advertising payload will have to be populated separately, where you can add which ever valid data field to the payload as you would like. You could see this done in the advertising_init function of the BLE peripheral examples in the SDK.
    If you are using the BLE advertising library you could also take a look at this blogpost to see how you may dynamically update your advertising payload, along with an example of this.

    Best regards,
    Karl

  • Hello.

    I misunderstood that if I did sd_ble_uuid_vs_add at the very beginning, it would set the advertised UUID. I have listed the current advertising_init code.

    #define BLE_BASE_UUID  {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15}
    #define BLE_ADV_UUID   0xFFFF
    BLE_ADVERTISING_DEF(m_advertising);
    
    // Bluetooth Low Energy UUID type, encapsulates both 16-bit and 128-bit UUIDs.
    static ble_uuid_t m_adv_uuids[] = 
    {
        {BLE_ADV_UUID, BLE_UUID_TYPE_VENDOR_BEGIN},
    };
    
    // Advertising data structure. This structure contains all options and data needed for encoding and setting the advertising data.
    static ble_advdata_t m_adv_data = 
    {
        .flags                        = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE,
        .uuids_complete.uuid_cnt      = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]),
        .uuids_complete.p_uuids       = m_adv_uuids,
    };
    
    // Options for the different advertisement modes.
    static ble_adv_modes_config_t m_adv_init = 
    {
        .ble_adv_whitelist_enabled      = true,
        .ble_adv_on_disconnect_disabled = true,
        .ble_adv_fast_enabled           = true,
        .ble_adv_fast_interval          = APP_ADV_INTERVAL,
        .ble_adv_fast_timeout           = APP_ADV_DURATION,
    };
    
    // Function for the GAP initialization.
    static void gap_params_init(void)
    {
        ret_code_t    err_code;
        ble_uuid_t    uuid_type;
        ble_uuid128_t base_uuid = {BLE_BASE_UUID};
    
        err_code = sd_ble_uuid_vs_add(&base_uuid, uuid_type);
        APP_ERROR_CHECK(err_code);
    
        return;
    }
    
    // Initialize the advertisement.
    static void advertising_init(void)
    {
        ret_code_t err_code;
        ble_advertising_init_t adv_init;
    
        memset(&adv_init, 0, sizeof(adv_init));
    
        adv_init.advdata     = m_adv_data;
        adv_init.config      = m_adv_init;
        adv_init.evt_handler = on_adv_evt;
    
        err_code = ble_advertising_init(&m_advertising, &adv_init);
        APP_ERROR_CHECK(err_code);
    
        ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    
        return;
    }

    I checked advertising_init in the SDK's BLE peripheral example, but how do I set the 128-bit base UUID?

    I stopped considering the method of dynamically changing the advertisement payload because the advertisement packet switching was not successful before. However, I will consider it again with reference to the blog you taught me.

    Best regards.

  • Hello again,

    sdi_kei said:
    I don't understand what is shown in the uart example of the ble app, so please tell me what it is.

    What is it that you would like me to elaborate or explain? Apologies, but I do not think I am understanding you correctly here.

    sdi_kei said:
    I have interpreted the 128-bit base UUID in my own way, but is it correct?

    You may generate your own base UUID for custom services, yes.
    Please see this BLE Course github repository for a detailed explanation of how to create and add a custom service to the SoftDevice.

    sdi_kei said:
    I'm checking how to update the dynamic advertisement data and creating a process, but if I dynamically set m_adv_uuids to uuids_complete.p_uuids, it will be advertised with a different base UUID.

    Please provide examples of the code you run when this happens, what the output of this code is, and how it diverges from what you would have expected the code to do.
    Please also note that the UUID might be given in Big-Endian, while we use small-endian in our SDK. This means that the byte-ordering of the UUID should be put in reverse, to conform to the SDK standard.

    Best regards,
    Karl

  • Hello.

    Using this link as a reference, I was able to create the following code that dynamically changes the UUID.

    #define BLE_BASE_UUID        {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15}
    #define BLE_ADV_UUID         0xFFFF
    #define COMPANY_IDENTIFIER   0xFFFF                                /**< Company identifier for Nordic Semiconductor ASA as per www.bluetooth.org. */
    #define BLE_UUID_TYPE_ALPHA  BLE_UUID_TYPE_VENDOR_BEGIN + 18
    #define BLE_UUID_TYPE_BETA   BLE_UUID_TYPE_VENDOR_BEGIN + 19
    
    
    BLE_ADVERTISING_DEF(m_advertising);
    
    // Bluetooth Low Energy UUID type, encapsulates both 16-bit and 128-bit UUIDs.
    static ble_uuid_t m_adv_uuids[2];
    
    static uint8_t ble_update_cnt = 0x00;
    
    // Advertising data structure. This structure contains all options and data needed for encoding and setting the advertising data.
    static ble_advdata_t m_adv_data = 
    {
        .flags                   = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE,
        .p_tx_power_level        = NULL;
        .uuids_complete.uuid_cnt = NULL,
        .uuids_complete.p_uuids  = NULL,
        .p_manuf_specific_data   = NULL;
    };
    
    // Options for the different advertisement modes.
    static ble_adv_modes_config_t m_adv_init = 
    {
        .ble_adv_whitelist_enabled      = true,
        .ble_adv_on_disconnect_disabled = true,
        .ble_adv_fast_enabled           = true,
        .ble_adv_fast_interval          = APP_ADV_INTERVAL,
        .ble_adv_fast_timeout           = APP_ADV_DURATION,
    };
    
    // Initialize the advertisement.
    static void advertising_init(void)
    {
        ret_code_t               err_code;
        ble_uuid_t               ble_uuid;
        ble_uuid128_t            base_uuid = {BLE_BASE_UUID};
        ble_advertising_init_t   adv_init;
        ble_advdata_manuf_data_t manuf_data;
        
        int8_t tx_power_level;
    
        memset(&adv_init, 0, sizeof(adv_init));
        memset(&manuf_data, 0, sizeof(manuf_data));
    
        // UUID Setting.
        m_adv_uuids[0].uuid = BLE_ADV_UUID;
        m_adv_uuids[0].type = BLE_UUID_TYPE_ALPHA;
    
        m_adv_data.uuids_complete.uuid_cnt = 0x01;
        m_adv_data.uuids_complete.p_uuids  = &m_adv_uuids[0];
    
        err_code = sd_ble_uuid_vs_add(&base_uuid, &ble_uuid.type);
        APP_ERROR_CHECK(err_code);
    
        // Tx Power setting.
        tx_power_level = 4;
        m_adv_data.p_tx_power_level = &tx_power_level;
    
        // Manufacturer Specific Data Setting.
        manuf_data.company_identifier    = COMPANY_IDENTIFIER;
        manuf_data.data.size             = NULL;
        manuf_data.data.p_data           = NULL;
        m_adv_data.p_manuf_specific_data = &manuf_data;
    
        adv_init.advdata     = m_adv_data;
        adv_init.config      = m_adv_init;
        adv_init.evt_handler = on_adv_evt;
    
        err_code = ble_advertising_init(&m_advertising, &adv_init);
        APP_ERROR_CHECK(err_code);
    
        ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    
        return;
    }
    
    // Updating advertising data.
    static void advertising_update(void)
    {
        ret_code_t               err_code;
        ble_advdata_t            adv_data;
        ble_uuid_t               ble_uuid;
        ble_uuid128_t            base_uuid;
        ble_advdata_manuf_data_t manuf_data;
        uint8_t                  ble_adv_uuid_update[] = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x2A, 0x2B, 0x2C, 0x2D};
    
        memset(&adv_data, 0, sizeof(adv_data));
    
        // UUID Setting.
        if (ble_update_cnt <= 0x0A) {
            ble_update_cnt+;
        } else {
            ble_update_cnt = 0x00;
        }
        ble_adv_uuid_update[0] = ble_update_cnt;
    
        memcpy(base_uuid.uuid128, ble_adv_uuid_update, sizeof(ble_adv_uuid_update));
        m_adv_uuids[1].uuid = BLE_ADV_UUID;
        m_adv_uuids[1].type = BLE_UUID_TYPE_BETA;
    
        adv_data.uuids_complete.uuid_cnt = 0x01;
        adv_data.uuids_complete.p_uuids  = &m_adv_uuids[1];
    
        err_code = sd_ble_uuid_vs_add(&base_uuid, &ble_uuid.type);
    
        // Flags Setting.
        adv_data.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    
        // Tx power Setting.
        adv_data.p_tx_power_level = NULL;
    
        // Manufacturer Specific Data Setting.
        manuf_data.company_identifier    = COMPANY_IDENTIFIER;
        manuf_data.data.size             = NULL;
        manuf_data.data.p_data           = NULL;
        adv_data.p_manuf_specific_data = &manuf_data;
    
        err_code = ble_advertising_advdata_update(&m_advertising, &adv_data, NULL);
        APP_ERROR_CHECK(err_code);
    
        return;
    }

    Please provide examples of the code you run when this happens, what the output of this code is, and how it diverges from what you would have expected the code to do.

    The reason why it was advertised with another UUID was that it was out of the SoftDevice table by that amount because the service and characteristic were set. The problem was solved by adding the number of deviations to "BLE_UUID_TYPE_VENDOR_BEGIN".

    However, a question arose here.
    Since sd_ble_uuid_vs_add is performed every time advertising_update is called, the number to be added to "BLE_UUID_TYPE_VENDOR_BEGIN" will change. Also, I think that sd_ble_uuid_vs_add will put pressure on the memory space every time. Is there a way to prevent pressure?

    Also, "ble_adv_whitelist_enabled" is set to true in m_adv_init, but I want to be able to select whether to use whitelist or not every time I call advertising_update. Is there a way to choose?
    I thought about "ble_advertising_restart_without_whitelist" to temporarily disable it, but how long will this temporary period last? Can it be used for selection?

    Best regards.

  • Hello,

    sdi_kei said:
    The reason why it was advertised with another UUID was that it was out of the SoftDevice table by that amount because the service and characteristic were set. The problem was solved by adding the number of deviations to "BLE_UUID_TYPE_VENDOR_BEGIN".

    I am not exactly sure about what you mean when you say this, since which UUID is advertised is independent of the SoftDevice table - it only depends on which UUID's you populate the advertising packet with. Perhaps I am misunderstanding you here?

    sdi_kei said:
    Since sd_ble_uuid_vs_add is performed every time advertising_update is called, the number to be added to "BLE_UUID_TYPE_VENDOR_BEGIN" will change. Also, I think that sd_ble_uuid_vs_add will put pressure on the memory space every time. Is there a way to prevent pressure?

    Yes, adding more and more UUID's to the SoftDevice table will eat up the memory over time - you can use the sd_ble_vs_remove function to free this memory as according to the function API.

    sdi_kei said:
    Also, "ble_adv_whitelist_enabled" is set to true in m_adv_init, but I want to be able to select whether to use whitelist or not every time I call advertising_update. Is there a way to choose?

    You may use the ble_advertising_restart_without_whitelist function to restart the current advertising mode without whitelist enabled.
    In general, the 
    ble_advertising_advdata_update function does only update advertising data payload - not the configuration - so if you need to make changes to the advertising configuration you will instead need to stop the advertising, reconfigure, and then restart the advertising.

    sdi_kei said:
    I thought about "ble_advertising_restart_without_whitelist" to temporarily disable it, but how long will this temporary period last? Can it be used for selection?

    The ble_advertising_restart_without_whitelist function will restart the current advertising mode without whitelist, so it will have the same duration / timeout as the advertising mode you are in when this function is called.

    Best regards,
    Karl

  • Hello.

    I am not exactly sure about what you mean when you say this, since which UUID is advertised is independent of the SoftDevice table - it only depends on which UUID's you populate the advertising packet with. Perhaps I am misunderstanding you here?

    Here it means setting the UUID from the SoftDevice table in the advertisement payload.

    I'm using sd_ble_uuid_vs_add to add a UUID to the SoftDevice table.
    However, there is other data in the SoftDevice table, so if you do not specify the added number correctly, you will refer to other data.
    The definition indicating the number is "BLE_UUID_TYPE_VENDOR_BEGIN".
    In my case, it was off by 18, so I was able to correctly set the UUID of the SoftDevice table in the advertisement payload by doing "BLE_UUID_TYPE_VENDOR_BEGIN + 18".

    Yes, adding more and more UUID's to the SoftDevice table will eat up the memory over time - you can use the sd_ble_vs_remove function to free this memory as according to the function API.

    Thank you. Before adding the UUID, I would like to use it to remove the previously added UUID.

    In general, the ble_advertising_advdata_update function does only update advertising data payload - not the configuration - so if you need to make changes to the advertising configuration you will instead need to stop the advertising, reconfigure, and then restart the advertising.

    You can't enable or disable the whitelist at the same time.
    It is possible to disable it with "ble_advertising_restart_without_whitelist", but is there an API to enable it?

    Best regards.

  • Hello,

    sdi_kei said:
    I'm using sd_ble_uuid_vs_add to add a UUID to the SoftDevice table.
    However, there is other data in the SoftDevice table, so if you do not specify the added number correctly, you will refer to other data.
    The definition indicating the number is "BLE_UUID_TYPE_VENDOR_BEGIN".
    In my case, it was off by 18, so I was able to correctly set the UUID of the SoftDevice table in the advertisement payload by doing "BLE_UUID_TYPE_VENDOR_BEGIN + 18".

    Hm.. I am not entirely sure that I follow.. How did you find these offsets?
    The UUID type field is indeed the index relative to BLE_UUID_TYPE_VENDOR_BEGIN like described in the sd_ble_uuid_vs_add API Reference documentation, and it will depend on how many UUID's and other information you have populated the SoftDevice table with already.

    sdi_kei said:
    Thank you. Before adding the UUID, I would like to use it to remove the previously added UUID.

    Great! This should keep your memory from overflowing.

    sdi_kei said:
    You can't enable or disable the whitelist at the same time.
    It is possible to disable it with "ble_advertising_restart_without_whitelist", but is there an API to enable it?

    No, you can not advertise both with and without whitelist - this would effectively mean without whitelist.
    It is also correct that there is no specific function to enable whitelist, instead you can enable whitelist in the advertising configuration p_advertising->adv_modes_config.ble_adv_whitelist_enabled and then restart without whitelist or temporarily disable whitelist if needed.

    Best regards,
    Karl

Reply
  • Hello,

    sdi_kei said:
    I'm using sd_ble_uuid_vs_add to add a UUID to the SoftDevice table.
    However, there is other data in the SoftDevice table, so if you do not specify the added number correctly, you will refer to other data.
    The definition indicating the number is "BLE_UUID_TYPE_VENDOR_BEGIN".
    In my case, it was off by 18, so I was able to correctly set the UUID of the SoftDevice table in the advertisement payload by doing "BLE_UUID_TYPE_VENDOR_BEGIN + 18".

    Hm.. I am not entirely sure that I follow.. How did you find these offsets?
    The UUID type field is indeed the index relative to BLE_UUID_TYPE_VENDOR_BEGIN like described in the sd_ble_uuid_vs_add API Reference documentation, and it will depend on how many UUID's and other information you have populated the SoftDevice table with already.

    sdi_kei said:
    Thank you. Before adding the UUID, I would like to use it to remove the previously added UUID.

    Great! This should keep your memory from overflowing.

    sdi_kei said:
    You can't enable or disable the whitelist at the same time.
    It is possible to disable it with "ble_advertising_restart_without_whitelist", but is there an API to enable it?

    No, you can not advertise both with and without whitelist - this would effectively mean without whitelist.
    It is also correct that there is no specific function to enable whitelist, instead you can enable whitelist in the advertising configuration p_advertising->adv_modes_config.ble_adv_whitelist_enabled and then restart without whitelist or temporarily disable whitelist if needed.

    Best regards,
    Karl

Children
  • Hello.

    How did you find these offsets?

    I referred to this ticket for the setting method.
    Since "BLE_UUID_TYPE_VENDOR_BEGIN + 1" is set for the second advertisement, I interpreted it as being used in a staggered manner.

    ble_adv_whitelist_enabled and then restart without whitelist or temporarily disable whitelist if needed.

    It was said that the whitelist could not be set, so as you told me, I took the method of stopping it, resetting it, and then starting advertising.

    Best regards.

  • sdi_kei said:
    I referred to this ticket for the setting method.
    Since "BLE_UUID_TYPE_VENDOR_BEGIN + 1" is set for the second advertisement, I interpreted it as being used in a staggered manner.

    Aha, I see - thank you for clarifying.

    sdi_kei said:
    It was said that the whitelist could not be set, so as you told me, I took the method of stopping it, resetting it, and then starting advertising.

    Alright, great - I am glad to hear that this is working as intended now!

    Best regards,
    Karl

Related