This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Unable to send more than 4 bytes of data over BLE

Hi,

I am trying to create a custom BLE service.

Following is the main code:

            err_code = ble_cus_flash_update(&m_cus, m_rx_buf, m_conn_handle);
            if (err_code != NRF_SUCCESS &&
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                err_code != NRF_ERROR_INVALID_STATE &&
                err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)

            {
                APP_ERROR_CHECK(err_code);
            }

These are my BLE write functions:

    uint8_t flash_init_value [6] = {0};

    memset(&add_char_params, 0, sizeof(add_char_params));
    add_char_params.uuid              = FLASH_UUID;
    add_char_params.uuid_type         = p_cus->uuid_type;

    add_char_params.init_len          = 6;// (in bytes)
    add_char_params.max_len           = 6;
    add_char_params.p_init_value      = flash_init_value;

    add_char_params.char_props.read   = 1;
    add_char_params.char_props.notify = 1;

    add_char_params.read_access       = SEC_OPEN;
    add_char_params.cccd_write_access = SEC_OPEN;

    err_code = characteristic_add(p_cus->service_handle,
                                  &add_char_params,
                                  &p_cus->flash_handles);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    return NRF_SUCCESS;

uint32_t ble_cus_flash_update(ble_cus_t * p_cus, uint8_t * flash_data_byte, uint16_t conn_handle)
{
    ble_gatts_hvx_params_t params;
    uint16_t len = sizeof(flash_data_byte);

    memset(&params, 0, sizeof(params));
    params.type   = BLE_GATT_HVX_NOTIFICATION;
    params.handle = p_cus->flash_handles.value_handle;
    params.p_data = flash_data_byte;
    params.p_len  = &len;

    return sd_ble_gatts_hvx(conn_handle, &params);
}

My sdk_config file says MAX_MTU is defined 23

// <o> NRF_SDH_BLE_GAP_EVENT_LENGTH - GAP event length. 
// <i> The time set aside for this connection on every connection interval in 1.25 ms units.

#ifndef NRF_SDH_BLE_GAP_EVENT_LENGTH
#define NRF_SDH_BLE_GAP_EVENT_LENGTH 6
#endif

// <o> NRF_SDH_BLE_GATT_MAX_MTU_SIZE - Static maximum MTU size. 
#ifndef NRF_SDH_BLE_GATT_MAX_MTU_SIZE
#define NRF_SDH_BLE_GATT_MAX_MTU_SIZE 23
#endif

// <o> NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE - Attribute Table size in bytes. The size must be a multiple of 4. 
#ifndef NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE
#define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 1408
#endif

I am using nrf Connect to debug BLE, which gives me output like this:

0x A1-A2-A3-A4-00-00

I am able to send 4 bytes successfully, but the rest of the values are not updating.
Any help would be really appreciated.

Regards,
Anurag

Parents
  • Hi Anurag,

    The problem here is that the size you specify is always 4. See this part of your code:

    uint32_t ble_cus_flash_update(ble_cus_t * p_cus, uint8_t * flash_data_byte, uint16_t conn_handle)
    {
        ble_gatts_hvx_params_t params;
        uint16_t len = sizeof(flash_data_byte);

    Here the len variable is set to the sizeof a pointer, which is always 4 byte. This is not what you want. There is no way to know the length of the input in this function, so you must pass the length as an additional parameter.

  • Thanks, I will try to implement it.

    I am trying to read some 20 bytes on SPI (event-triggered from master device), if I wait for "BLE_GATTS_EVT_HVN_TX_COMPLETE"

    I will miss SPI data, because I am reading it every few ms.

    Isn't there another way to take care of it?

  • Hi,

    _anurag said:

    I will miss SPI data, because I am reading it every few ms.

    Isn't there another way to take care of it?

    That depends. You read data quite often, but not that much. You need to consider your connection parameters and event length etc. and configure that so that it is sensible for your use case. Then you need to consider if you should send each of these values as a single packet, or if yo should use data length extension (DLE) and concatenate this to fewer larger packets that you send less frequently.

  • Thanks,

    Right now, I am using nrfconnect to debug BLE.

    One issue is that I am able to fetch data when I am manually reading it by clicking on the download icon besides characteristics.

    But, when I am enabling notifications for auto-update, it crashes after a few reads.

    Can you suggest what might cause it?

  • Can you suggest what might cause it?

    You need to debug to understand what really happens. Is an error check hit? If so, which function is called and what error code does it return? Or if something else, what can you find from debugging? What state is your code in? Has any errors been detected, or could it be that it is stuck in an eternal loop somewhere (waiting for something that never happens), or some thing else?

  • Hi Einar,

    Thanks for your response.
    I have fixed my issue and have been able to achieve a throughput of:

    200B at 20ms = 10kB/s

    What solved the issue is Increasing:

    NRF_SDH_BLE_GAP_DATA_LENGTH
    NRF_SDH_BLE_GAP_EVENT_LENGTH
    NRF_SDH_BLE_GATT_MAX_MTU_SIZE
    hvn_tx_queue_size

    with little bit of tweaking

    The following links were really helpful:
    increase-hvn_tx_queue_size-to-maximize-throughput

    nrf_error_resources-occurred-when-the-connection-is-disconnected/331070#331070

    Regards,

    Anurag

Reply Children
No Data
Related