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

Characteristic value as an array of floats

Hi,

Iam on SDK 15.2 would like to have a Characteristic value as an array of floats. 

I have done it but it prints it like some bytes on the Nrf Connect app. So i have implemented a Presentation Format descriptor, but I don't find the array format. I have seen this topic, where there is the same question but there is no response. 

How can I do that ?

Thanks,

  • On the Nrf Connect app, in byte I can see my three values cdcc8c3f cdcc0c40 33335340, which correspond to {1.1, 2.2, 3.3}. My problem is, i would like to print the values as float, with the cpf,but there is no array format. When I use the Float32 format, i canonly see 1.1. 

  • Hello,

    Beldramma said:
    in byte I can see my three values cdcc8c3f cdcc0c40 33335340, which correspond to {1.1, 2.2, 3.3}. My problem is, i would like to print the values as float

    You can use the function bds_uint32_decode to reverse the encoding applied before the transfer.

    Beldramma said:
    When I use the Float32 format, i canonly see 1.1. 

    Are you saying that you then do not receive the first 4 bytes? I do not understand what you mean by this, please elaborate.

    Looking forward to resolving this issue together,

    Best regards,
    Karl

  • Ok Karl.

    Here is my code to implement the characteristic:

    //Add a random float array characteristic
        static float values_t[] = {1.1,2.2,3.3};
        static ble_gatts_char_handles_t float_array_char;
        ble_gatts_char_pf_t char_pf;
        // Add Presentation format to the Characteristic
        memset(&char_pf, 0, sizeof(char_pf));
        //char_pf.unit = BLE_GATT_UNIT_CELSIUS_TEMPERATURE_DEGREE_CELSIUS;
        char_pf.format = BLE_GATT_CPF_FORMAT_FLOAT32;
        char_pf.name_space = BLE_GATT_CPF_NAMESPACE_BTSIG;
        char_pf.desc = BLE_GATT_CPF_NAMESPACE_DESCRIPTION_UNKNOWN;
    
    
        err_code = char_add(p_cus,
                            &char_pf,
                            CUSTOM_VALUE_CHAR_UUID+1,
                            (uint8_t*)values_t,
                            3*sizeof(float),
                            SEC_OPEN,
                            &float_array_char);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }

    Here is the code of char_add function :

    /**@brief Function for adding the Characteristic.
     *
     * @param[in]   uuid           UUID of characteristic to be added.
     * @param[in]   p_char_value   Initial value of characteristic to be added.
     * @param[in]   char_len       Length of initial value. This will also be the maximum value.
     * @param[in]   rd_sec         Security requirement for reading characteristic value.
     * @param[out]  p_handles      Handles of new characteristic.
     *
     * @return      NRF_SUCCESS on success, otherwise an error code.
     */
    static uint32_t char_add(ble_cus_t                     * p_cus,
                             ble_gatts_char_pf_t           * presentation_format,
                             uint16_t                        uuid,
                             uint8_t                       * p_char_value,
                             uint16_t                        char_len,
                             security_req_t const            rd_sec,
                             ble_gatts_char_handles_t      * p_handles)
    {
        ble_add_char_params_t add_char_params;
    
        APP_ERROR_CHECK_BOOL(p_char_value != NULL);
        APP_ERROR_CHECK_BOOL(char_len > 0);
    
        memset(&add_char_params, 0, sizeof(add_char_params));
    
        add_char_params.uuid_type       = p_cus->uuid_type;
        add_char_params.uuid            = uuid;
        add_char_params.max_len         = char_len;
        add_char_params.init_len        = char_len;
        add_char_params.p_init_value    = p_char_value;
        add_char_params.char_props.read = 1;
        add_char_params.read_access     = rd_sec;
        add_char_params.p_presentation_format = presentation_format;
    
        return characteristic_add(p_cus->service_handle, &add_char_params, p_handles);
    }

    Here is the picture of the Nrf app, and I have read the characteristic, but I haven't read the cpf descriptor :

    https://pasteboard.co/Jl6x75F.jpg

    Here is the picture of my char when I have read the cpf.

    https://pasteboard.co/Jl6yk3X.jpg

    You can see that with the cpf i have only one value.

  • Hello again,

    It seems I misunderstood your previous comment, my mistake.
    The Characteristic Presentation Format does not support an array of floats. This is why you are only seeing the first float in your array.
    If you are to use the CPF, then you will need to create multiple characteristics holding one float each. Alternatively you could create a custom characteristic that is parsed as an array of floats.

    Furthermore, it might be helpful to see Awneil's answer in this ticket.

    Best regards,
    Karl

Related