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

  • Hi Einar,

    I am trying to implement BLE_ERROR_NO_TX_BUFFERS as described here

    But, it gives me "undeclared error", I have tried including "ble_err.h" with nomluck.
    Can you tell me which header contains "BLE_EVT_TX_COMPLETE ", I can try to include and call it.

    Regards,

    Anurag

  • Hi Anurag,

    _anurag said:

    I am trying to implement BLE_ERROR_NO_TX_BUFFERS as described here

    But, it gives me "undeclared error", I have tried including "ble_err.h" with nomluck

    That thread is too old. There is no BLE_ERROR_NO_TX_BUFFERS unless you are using an ancient SDK and SoftDevices version(?), which is why you get the error.

    _anurag said:
    Can you tell me which header contains "BLE_EVT_TX_COMPLETE ", I can try to include and call it.

    Sorry, I wrote the last post from memory, and that does not serve me too well it seems. Assuming you use a recent SDK version (17.x.x for instance) you will get NRF_ERROR_RESOURCES if the queue is full. Then you should wait for BLE_GATTS_EVT_HVN_TX_COMPLETE before you try again. (This is from ble_gatts.h. You get this as a BLE event in your BLE event handler.)

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

Related