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

How do I add a custom characteristic value UUID.

I try to add a custom characteristic value's UUID. I couldn't find any example or document that provide this information. All I can find is a function sd_ble_gatts_descriptor_add(), which is for adding custom characteristic description. Does the API has something similar to this function that you can add a custom Characteristic value? If not is there way for me too ad a custom value UUID to a characteristic.

Thanks!

  • Good morning,

    for me it works like this:

        static uint32_t custom_characteristic_char_add(ble_device_t * p_dev, const ble_device_init_t * p_dev_init)
      
    
    {
        ble_gatts_char_md_t char_md;
        ble_gatts_attr_t    attr_char_value;
        ble_uuid_t          ble_uuid;
        ble_gatts_attr_md_t attr_md;
    
        memset(&char_md, 0, sizeof(char_md));
    
        char_md.char_props.write         = 1;
        char_md.char_props.write_wo_resp = 1;
        char_md.p_char_user_desc         = NULL;
        char_md.p_char_pf                = NULL;
        char_md.p_user_desc_md           = NULL;
        char_md.p_cccd_md                = NULL;
        char_md.p_sccd_md                = NULL;
    
        ble_uuid.type = p_dev->uuid_type;
        ble_uuid.uuid = BLE_CUSTOM_UUID;
    
        memset(&attr_md, 0, sizeof(attr_md));
    
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
    
        attr_md.vloc    = BLE_GATTS_VLOC_STACK;
        attr_md.rd_auth = 0;
        attr_md.wr_auth = 0;
        attr_md.vlen    = 1;
    
        memset(&attr_char_value, 0, sizeof(attr_char_value));
    
        attr_char_value.p_uuid    = &ble_uuid;
        attr_char_value.p_attr_md = &attr_md;
        attr_char_value.init_len  = 1;
        attr_char_value.init_offs = 0;
        attr_char_value.max_len   = BLE_DEVICE_MAX_CUSTOM_UUID_LEN;
    
        return sd_ble_gatts_characteristic_add(p_dev->service_handle,
                                               &char_md,
                                               &attr_char_value,
                                               &p_dev->customUUID_handles);
    }
    

    You will get a custom characteristic with a custio defined UUID (#define BLE_CUSTOM_UUID 0x1234).

    I think this is done in the example nus_service... (UART over BLE).

    Regards!

  • Thanks!. This helps me to resolve the issue that I had.

Related