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

How to send a byte array by battery level notification

Dear Sir/Madam,

Normally battery level notification is only one byte. But I need send byte array of 58 bytes through battery level notifications in my project.  I have to do this because one obsolete product in my company does this and I have to keep the same behavior.   I am working on SDK 8, S110 with 51822. I don't know how to make battery-level notification send a byte array instead of 1-byte variable. Could you tell me how to achieve this purpose?  Do I need split the 58 bytes into two 20-byte array and one 18-byte array to fit the GATT packet?

Thanks,

Jacky

Parents
  • Hi Jacky, 

    Yes, you will need to split it into 20-byte chuncks. 

    BLE characteristic only deals with octets or arrays of octets, i.e. byte-streams. If you're sending something bigger than a byte, then you need to format the data so that it can be placed in a byte array.

    So if you  for instance want to sent a 32-bit unsigned integer, then you will have to set the size of the characteristic to 4*sizeof(uint8_t), i.e. attr_char_value.init_len/max_len =  4*sizeof(uint8_t) in the function that adds the characteristic and then divide the 32-bit integer into four 8-bit usigned integers, e.g.

    uint32_t large_number = 64000;
    uint8_t ble_data[4] = {0};
    uint16_t ble_data_len = sizeof(
    ble_data);
    for(int i = 0; i < sizeof(ble_data);i++) { ble_data[i] = large_number >> (i*8); } 

    Then in your function that calls sd_ble_gatts_hvx you simply point to the byte array and specify the length of the array.

            ble_gatts_hvx_params_t hvx_params;
    
            memset(&hvx_params, 0, sizeof(hvx_params));
    
            hvx_params.handle = p_cus->custom_value_handles.value_handle;
            hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
            hvx_params.offset = 0;
            hvx_params.p_len  = &ble_data_len;
            hvx_params.p_data = ble_data
    
            err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params);
    

  • Hi Bjorn,

    I have tried a lot times, and the code simply failed to work when I tried to send 20-byte packet through the notification of battery-level characteristic. So I guess the Nordic layerstack might have certain internal restrict to this Well-known service and characteristics.   Could you verify it?  And If possible, could you send me a sample code that successfully sending out 20 byte packets through Battery_Level characteristic? 

    Thanks,

    Jacky

  • "simply failed to work" - not very much information - failed to work how, what error message, what happened?

    "So I guess the Nordic layerstack might have certain internal restrict .. " - wrong guess, the stack doesn't care one jot about the data you're sending out and has no knowledge of well-known services. 

    Bjorn gave you code, just make a 20 byte buffer, put data in it, pass it to the sending routine along with the length and it will be sent. If that doesn't happen then post clearly what doesn't work and what you're seeing. 

  • : Have you set the size of the characteristic to 20 bytes? Please post the code where you add the battery_level characteristic. 

Reply Children
No Data
Related