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

How Can I limit Gatt Data bytes that I write? (Not With Zero-Fill)

I created an characteristic that has MAX_Length = 20 Bytes characteristic.

And When I'm trying to write less than 20bytes on this Characteristic, It Always send with 20Bytes with Zero filled data bytes.

For example, I'd like to write 123456 (6 Bytes) It keeps Send with like 12345600000000000000(20Bytes).

How Can I Cut off the left Zero Filled Bytes and only send with the Real data?
(with SD130, SDK v12.2.0, nrf51422, nrf51-dk)

OR does this Zero Fill send is normal Behaviour?

  • make the characteristic variable length and each time you change it, write the actual length. It looks to me like you're using a characterisic without the variable length flag set.

  • Thanks for the reply!

    Sorry for my misunderstanding, but I don't get it.

    The variable length flag that you mean is ble_gattc_write_params_t write_params?
    Parameter with options such as write_op, flags, handle, offset, len, p_value when I send data with Central(the one who send Data, writes on Other side's characteristic data),??

  • You set this in the Attribute metadata(ble_gatts_attr_md_t), which is a part of the GATT Attribute(ble_gatts_attr_t). This is then used when you add a characteristic declaration.

    Code snippet:

    ble_gatts_attr_md_t attr_md;
    
    attr_md.vlen    = 1;
    

    You can see it used in e.g. the Nordic UART Service when adding the RX characteristic:

    static uint32_t rx_char_add(ble_nus_t * p_nus, const ble_nus_init_t * p_nus_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_nus->uuid_type;
        ble_uuid.uuid = BLE_UUID_NUS_RX_CHARACTERISTIC;
    
        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_NUS_MAX_RX_CHAR_LEN;
    
        return sd_ble_gatts_characteristic_add(p_nus->service_handle,
                                               &char_md,
                                               &attr_char_value,
                                               &p_nus->rx_handles);
    }
    
Related