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

Adding custom UUID in the advertisement packet

Board: nRF52810

SoftDevice: s112

SDK version: 17.0.2

I'm trying to add a custom UUID in the advertisement packet and scan for the custom UUID in the central device to connect with it. I've followed the steps as given in the Tutorial. But my application gets asserted when entering the advertising_start() function. 

Is it necessary to add my custom UUID in service init because I am only using it for connection purposes?

How can solve this problem?

In central, how can I scan for the custom UUID?

Thanks in advance.

Parents Reply Children
  • Hi,

    This is my ble_cus.h file.

    #include <stdint.h>
    #include <stdbool.h>
    #include "ble.h"
    #include "ble_srv_common.h"
    
    #define CUSTOM_SERVICE_UUID_BASE         {0xBC, 0x8A, 0xBF, 0x45, 0xCA, 0x05, 0x50, 0xBA, \
                                              0x40, 0x42, 0xB0, 0x00, 0xC9, 0xAD, 0x64, 0xF3}
    
    #define CUSTOM_SERVICE_UUID               0x1400
    
    /**@brief   Macro for defining a ble_cus instance.
     *
     * @param   _name   Name of the instance.
     * @hideinitializer
     */
    #define BLE_CUS_DEF(_name)                                                                          \
    static ble_cus_t _name;                                                                             \
    
    /**@brief Custom Service init structure. This contains all options and data needed for
     *        initialization of the service.*/
    typedef struct
    {
        uint8_t                       initial_custom_value;           /**< Initial custom value */
        ble_srv_cccd_security_mode_t  custom_value_char_attr_md;     /**< Initial security level for Custom characteristics attribute */
    } ble_cus_init_t;
    
    /**@brief Custom Service structure. This contains various status information for the service. */
    struct ble_cus_s
    {
        uint16_t                      service_handle;                 /**< Handle of Custom Service (as provided by the BLE stack). */
        ble_gatts_char_handles_t      custom_value_handles;           /**< Handles related to the Custom Value characteristic. */
        uint16_t                      conn_handle;                    /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
        uint8_t                       uuid_type; 
    };
    
    // Forward declaration of the ble_cus_t type.
    typedef struct ble_cus_s ble_cus_t;
    
    /**@brief Function for initializing the Custom Service.
     *
     * @param[out]  p_cus       Custom Service structure. This structure will have to be supplied by
     *                          the application. It will be initialized by this function, and will later
     *                          be used to identify this particular service instance.
     * @param[in]   p_cus_init  Information needed to initialize the service.
     *
     * @return      NRF_SUCCESS on successful initialization of service, otherwise an error code.
     */
    uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init);
    

    This is my ble_cus.c file

    #include "sdk_common.h"
    #include "ble_srv_common.h"
    #include "ble_cus.h"
    #include <string.h>
    #include "nrf_gpio.h"
    #include "boards.h"
    #include "nrf_log.h"
    
    uint32_t ble_cus_init(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
    {
        if (p_cus == NULL || p_cus_init == NULL)
        {
            return NRF_ERROR_NULL;
        }
    
        uint32_t   err_code;
        ble_uuid_t ble_uuid;
    
        // Initialize service structure
        p_cus->conn_handle               = BLE_CONN_HANDLE_INVALID;
    
        // Add Custom Service UUID
        ble_uuid128_t base_uuid = {CUSTOM_SERVICE_UUID_BASE};
        err_code =  sd_ble_uuid_vs_add(&base_uuid, &p_cus->uuid_type);
        VERIFY_SUCCESS(err_code);
        
        ble_uuid.type = p_cus->uuid_type;
        ble_uuid.uuid = CUSTOM_SERVICE_UUID;
    
        // Add the Custom Service
        err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_cus->service_handle);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
        return err_code;
    }

    I've added all these in my main.c

    #include "ble_cus.h"
    
    BLE_CUS_DEF(m_cus);
    
    ble_uuid_t m_adv_uuids[] = {{CUSTOM_SERVICE_UUID, BLE_UUID_TYPE_VENDOR_BEGIN }}; /**< Universally unique service identifiers. */
    
    /**@brief Function for initializing the Advertising functionality.
     */
    static void advertising_init(void)
    {
        uint32_t               err_code;
        ble_advertising_init_t init;
        ble_cus_init_t     cus_init;
        
        err_code = ble_cus_init(&m_cus, &cus_init);
        APP_ERROR_CHECK(err_code);
    
        memset(&init, 0, sizeof(init));
    
        init.advdata.name_type          = BLE_ADVDATA_FULL_NAME;
        init.advdata.include_appearance = false;
        init.advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
    
        init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
        init.srdata.uuids_complete.p_uuids  = m_adv_uuids;
    
        init.config.ble_adv_fast_enabled  = true;
        init.config.ble_adv_fast_interval = APP_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);
    }
     

    I didn't add it to the services because I only need it to advertise it.

  • Hi,

    You need to replace 'BLE_UUID_TYPE_VENDOR_BEGIN' with the 'p_uuid_type' value returned by sd_ble_uuid_vs_add() in ble_cus.c to ensure you're referencing the correct based UUID.

  • I've changed the service and advertising initialization order in main.c. I initialize advertising first and then service. Now it works fine. 

    In central, how can I scan for the custom UUID?

    Should I need to add the base uuid in ble stack in central too??

  • Yes, you need to specify the base UUID on the client side as well.

Related