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

  • You may add on the end words to make sure your data length or define your data length in your first byte format. 

  • Hi, sorry I didn't quite understand. What are 'end words' and first byte format?

  • It's a usual technique for some case in char. format..

    Your case :

     end words method ( as \r\n 0x0d 0x0a) :

    00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f

    10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f

    20 21 22 0d 0a ff  ff ff ff ff

    First byte for length

     23  00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e

     0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e

    1f 20 21 22 ff ff ff ff ff ff

  • 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?

  • 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

Related