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

Maximum number of bytes transmitted from own service and characteristic

Hi,

I am currently working with the nrf52840, ble_app_uart example under the ble_peripheral folder. Using the tutorial below:

https://devzone.nordicsemi.com/nordic/short-range-guides/b/bluetooth-low-energy/posts/ble-characteristics-a-beginners-tutorial

I was able to add my own service and characteristic. However, the issue I am facing is that, when I transmit data from my own characteristic over bluetooth, I am getting unnecessary characters at the end. Under step 2.H the parameter  'attr_char_value.max_len = 42' in  my code. (I arbitrarily chose this value, to make sure its large enough)

Then, under step 3E I call, our_temperature_characteristic_update() and pass in the desired length of my actual data. I set that value to p_len. In my case, this value is set to 35.

So, ideally I should be observing only 35 bytes displayed but I see extra data displayed which I don't want.

Would appreciate any suggestions to resolve this.

Thanks

Parents
  • Hi,

    Then, under step 3E I call, our_temperature_characteristic_update() and pass in the desired length of my actual data. I set that value to p_len. In my case, this value is set to 35.

    So, ideally I should be observing only 35 bytes displayed but I see extra data displayed which I don't want.

    I think we need to see some code in order to understand the issue. Do you take into account the received data length when you print the data buffer, or do you always print the maximum length?

Reply
  • Hi,

    Then, under step 3E I call, our_temperature_characteristic_update() and pass in the desired length of my actual data. I set that value to p_len. In my case, this value is set to 35.

    So, ideally I should be observing only 35 bytes displayed but I see extra data displayed which I don't want.

    I think we need to see some code in order to understand the issue. Do you take into account the received data length when you print the data buffer, or do you always print the maximum length?

Children
  • Hi, below is snippets of my code.

    //This is found in the function our_char_add() - based on the tutorial attached above.
    //In this case my max_len is set to 42. 
    //This code is called only ONCE, when we initialize our service.
        
        
        // OUR_JOB: Step 2.H, Set characteristic length in number of bytes
        attr_char_value.max_len     = 42;                                       
        attr_char_value.init_len    = 4;
        uint8_t value[4]            = {0x12,0x34,0x56,0x78};
        attr_char_value.p_value     = value;

    Then, when I am updating the characteristic this is the function I call. Here, I am passing in the actual length of my data, which I have calculated using pointers. That length is then set to the parameter 'len'. Generally this value comes to around 35.

    void our_temperature_characteristic_update(ble_os_t *p_our_service, char* temperature_value, int length)
    {
        // OUR_JOB: Step 3.E, Update characteristic value
        if (p_our_service->conn_handle != BLE_CONN_HANDLE_INVALID){//if the connection is valid. Earlier: p_our_service->conn_handle
    
            uint16_t               len = (uint16_t)length;
            ble_gatts_hvx_params_t hvx_params;
            memset(&hvx_params, 0, sizeof(hvx_params));
    
            hvx_params.handle = p_our_service->char_handles.value_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = 0;
            hvx_params.p_len  = &len;
            hvx_params.p_data = (uint8_t*)temperature_value;  
    
            sd_ble_gatts_hvx(p_our_service->conn_handle, &hvx_params);
    
    
    
        }
    }
    
    

    As you can see, the function gets the parameter 'length' which is then assigned to hvx_params.p_len. According to the nordic tutorial, this number specifies how many bytes should be displayed.

    Please let me know if further clarification is needed.

    Thanks

  • Hi, 

    Ananye said:
    As you can see, the function gets the parameter 'length' which is then assigned to hvx_params.p_len. According to the nordic tutorial, this number specifies how many bytes should be displayed.

    this looks good so far. To me this seems like just an issue with printing the data. How do you display this on the peer device? If it is a nRF, can you share the code that prints it? If it is some other tool, are you sure that tool correctly uses that value and not the maximum value? 

  • Hi Einar,

    Sorry for my late reply. I have been testing this feature again recently. I am actually showing the data in the nRF connect app. In the app, when I monitor the characteristic value, I see that it prints upto the maximum length instead of the length value in the update_characteristic function.

    The remaining left bytes are just shown up as random bytes.

    Thanks

    Ananye

  • Hi,

    What you are seeing is expected, then. You should only print the actual length of the data. Anything after that would be whatever happens to be in that memory from before.

Related