Last Updated on : 2024-11-20 08:51:41download
The bulk data transfer channel allows the data that is locally stored in a specified format to be quickly transferred to a mobile app.
With the bulk data transfer channel, the Bluetooth devices can quickly transmit a large amount of cached data to the mobile app after the Bluetooth is connected.
For example, the smart watch or fitness tracker will store the measured activity data such as pace, distance, and heart rate to the flash memory if the Bluetooth is disconnected. After the mobile app is connected to the Bluetooth device, it will send the bulk data transfer command to allow the device to transmit the cached data.
Mobile app to device communication
Field | Bytes | Description |
---|---|---|
version | 1 | The protocol version of the bulk data transfer is fixed to 0x00 . |
bulk_type | 1 | The type of the bulk data, defined by specific products. Its value starts with 1 . 0 indicates the generic data type. |
Device to mobile app communication
Field | Bytes | Description |
---|---|---|
version | 1 | The protocol version of the bulk data transfer is fixed to 0x00 . |
bulk_type | 1 | The type of the bulk data, defined by specific products. Its value starts with 1 . 0 indicates the generic data type. |
status | 1 | Status
|
flag | 1 | Specify whether the mobile app parses the bulk data.
|
total_length | 4 | The total length of the bulk data, in big-endian format. |
total_crc32 | 4 | The total CRC32 value, in big-endian format. |
block_length | 2 | The block length. The bulk data is read by blocks. The mobile app sends the block number to the device to specify the block to be transmitted. A block is divided into packets for transmission. |
Each time the device receives the read blocks of data command, it will transmit the data packets of the specified packet length by using the report bulk data command.
Mobile app to device communication
Field | Bytes | Description |
---|---|---|
version | 1 | The protocol version of the bulk data transfer is fixed to 0x00 . |
bulk_type | 1 | The type of the bulk data, defined by specific products. Its value starts with 1 . 0 indicates the generic data type. |
block_number | 2 | The zero-based block number, identifying the block to be read. |
Device to mobile app communication
Field | Bytes | Description |
---|---|---|
version | 1 | The protocol version of the bulk data transfer is fixed to 0x00 . |
bulk_type | 1 | The type of the bulk data, defined by specific products. Its value starts with 1 . 0 indicates the generic data type. |
status | 1 | Status
|
block_number | 2 | The block number, identifying the block to be read. |
block_length | 2 | The block length, indicating the length of the block to be read. |
packet_length | 2 | The packet length, indicating the maximum packet size for transmitting a block. |
block_crc16 | 2 | The CRC16 checksum of the block. |
Each time the device receives the read blocks of data command, it will transmit a block in packets by using this command.
Example:
0
. The device returns the block length, the CRC16 value, and the maximum packet length. The mobile app starts the timer and waits for a block after receiving the response from the device.Device to mobile app communication
Field | Bytes | Description |
---|---|---|
version | 1 | The protocol version of the bulk data transfer is fixed to 0x00 . |
bulk_type | 1 | The type of the bulk data, defined by specific products. Its value starts with 1 . 0 indicates the generic data type. |
block_number | 2 | The block number, identifying the block to be transmitted. |
packet_number | 2 | The current packet number. Zero-based sequence number of data packets for the current block transmission. |
packet_length | 2 | The length of the current data packet. |
packet_data | packet_length | The current data packet. |
Mobile app to device communication
Field | Bytes | Description |
---|---|---|
version | 1 | The protocol version of the bulk data transfer is fixed to 0x00 . |
bulk_type | 1 | The type of the bulk data, defined by specific products. Its value starts with 1 . 0 indicates the generic data type. |
Device to mobile app communication
Field | Bytes | Description |
---|---|---|
version | 1 | The protocol version of the bulk data transfer is fixed to 0x00 . |
bulk_type | 1 | The type of the bulk data, defined by specific products. Its value starts with 1 . 0 indicates the generic data type. |
status | 1 | Status
|
The type length data (TLD) format is used to parse data on the mobile app and store data on local flash. It does not apply to data parsing in the cloud. The bulk data must follow the TLD format to be transmitted to the mobile app for parsing. There are two format types. You can choose one as needed.
Field | Bytes | Description |
---|---|---|
Type | 1 | Format type
|
Len | 2 | Data length |
Data | Len | Payload |
Stores all the data point (DP) data produced simultaneously. The mobile app parses the data.
Data format:
Field | Bytes | Description |
---|---|---|
UNIX_time | 4 | Unix timestamp |
DP_1 | Data of DP 1. For more information, see DP data format. | |
… | ||
DP_n | Data of DP N. For more information, see DP data format. |
Stores all the DP data produced simultaneously, including the overhead for data structure alignment. The mobile app parses the data.
Data format:
Field | Bytes | Description |
---|---|---|
storage_header | 6 | The overhead that is not parsed. |
valid_data_len | 2 | The total length of the Unix timestamp and the DP data, in big-endian format. |
UNIX_time | 4 | Unix timestamp |
DP_1 | Data of DP 1. For more information, see DP data format. | |
… | ||
DP_n | Data of DP N. For more information, see DP data format. | |
invalid_data | Len-6-2-valid_data_len | The overhead that is not parsed. |
DP 1 data | – | DP n data | ||||||
---|---|---|---|---|---|---|---|---|
1 | 2 | 3 to 4 | 5 and greater | – | n | n+1 | n+2 to n+3 | n+4 and greater |
Dp_id | Dp_type | Dp_len | Dp_data | – | Dp_id | Dp_type | Dp_len | Dp_data |
Function | tuya_ble_bulk_data_read_block_size_get |
---|---|
Function prototype | uint32_t tuya_ble_bulk_data_read_block_size_get(void) |
Feature description | Read the block length |
Parameters | None |
Return value | The block length. |
Note | tuya_ble_bulk_data_read_block_size_get is used to determine the length of a block based on the maximum transmission unit (MTU) and Bluetooth stack. |
Example:
static uint32_t bulk_data_read_block_size = 0;
void tuya_ble_app_bulk_data_handler(tuya_ble_bulk_data_request_t *p_data)
{
switch (p_data->evt)
{
case TUYA_BLE_BULK_DATA_EVT_READ_INFO: {
bulk_data_read_block_size = tuya_ble_bulk_data_read_block_size_get();
} break;
//...
}
}
Function | tuya_ble_bulk_data_response |
---|---|
Function prototype | tuya_ble_status_t tuya_ble_bulk_data_response(tuya_ble_bulk_data_response_t *p_data) |
Feature description | Respond to bulk data transfer request |
Parameters | p_data[in]: the return value of the bulk data request |
Return value |
|
Note | The device calls this function to return a response on receiving the bulk data request. The return value must follow the format defined in commands. |
Example:
void tuya_ble_app_bulk_data_handler(tuya_ble_bulk_data_request_t *p_data)
{
uint8_t rsp_flag = 1;
tuya_ble_bulk_data_response_t rsp_data;
switch (p_data->evt)
{
case TUYA_BLE_BULK_DATA_EVT_READ_INFO: {
//...
} break;
case TUYA_BLE_BULK_DATA_EVT_READ_BLOCK: {
//...
} break;
case TUYA_BLE_BULK_DATA_EVT_SEND_DATA: {
//...
} break;
case TUYA_BLE_BULK_DATA_EVT_ERASE: {
//...
} break;
default: {
rsp_flag = 0;
} break;
}
if (rsp_flag)
{
tuya_ble_bulk_data_response(&rsp_data);
}
}
The Bluetooth SDK sends data or events to the mobile app using messages or registered callbacks.
After the Bluetooth module receives a bulk data transfer command from the mobile app, the Bluetooth SDK will send the following callbacks to the program accordingly. The specific callback handler is intended to be implemented by you. For more information, see Register the message queue.
Event | TUYA_BLE_CB_EVT_BULK_DATA |
---|---|
Data structure | tuya_ble_bulk_data_request_t |
Description | Bulk data transfer request callback. |
Note | You can implement bulk data transfer handlers in this callback. |
Data structure
typedef enum {
TUYA_BLE_BULK_DATA_EVT_READ_INFO, /**< Request the bulk data information */
TUYA_BLE_BULK_DATA_EVT_READ_BLOCK, /**< Read blocks of data */
TUYA_BLE_BULK_DATA_EVT_SEND_DATA, /**< Report bulk data */
TUYA_BLE_BULK_DATA_EVT_ERASE, /**< Erase bulk data */
TUYA_BLE_BULK_DATA_EVT_UNKONWN, /**< Unknown event */
} tuya_ble_bulk_data_evt_type_t;
typedef struct {
uint16_t block_number;
} tuya_ble_bulk_data_evt_read_block_req_t;
typedef struct {
uint16_t block_number;
} tuya_ble_bulk_data_evt_send_data_req_t;
typedef struct {
tuya_ble_bulk_data_evt_type_t evt; // The specific bulk data transfer command
uint8_t bulk_type;
union
{
tuya_ble_bulk_data_evt_read_block_req_t block_data_req_data;
tuya_ble_bulk_data_evt_send_data_req_t send_data_req_data;
} params;
} tuya_ble_bulk_data_request_t;
Example:
static void tuya_ble_sdk_callback(tuya_ble_cb_evt_param_t *event)
{
switch (event->evt)
{
//...
case TUYA_BLE_CB_EVT_BULK_DATA: { // Implement bulk data transfer handlers.
tuya_ble_app_bulk_data_handler(&event->bulk_req_data);
} break;
//…
}
}
Data structure
typedef struct {
uint8_t status; /**< 0: success, with valid payload. 1: invalid, with invalid payload or without any payloads. Others: reserved */
uint8_t flag; /**< 0: The mobile app parses the bulk data. 1: The control panel inside the mobile app or the cloud parses the bulk data. */
uint32_t bulk_data_length; /**< The total length */
uint32_t bulk_data_crc; /**< The total CRC32 */
uint16_t block_data_length; /**< The block length */
} tuya_ble_bulk_data_evt_read_info_res_t;
typedef struct {
uint8_t status; /**< 0: success. 1: The format type is invalid. 2: The block number is invalid. Others: reserved */
uint16_t block_number; /**< The block number */
uint16_t block_data_length; /**< The block length */
uint16_t block_data_crc16; /**< The CRC16 value of a block*/
uint16_t max_packet_data_length; /**< The maximum packet size for transmitting a block */
} tuya_ble_bulk_data_evt_read_block_res_t;
typedef struct {
uint16_t current_block_number; /**< The current block number */
uint16_t current_block_length; /**< The current block length */
uint8_t *p_current_block_data; /**< The pointer to the current block */
} tuya_ble_bulk_data_evt_send_data_res_t;
typedef struct {
uint8_t status; /**< 0: success. 1: failure. Others: reserved */
} tuya_ble_bulk_data_evt_erase_res_t;
typedef struct {
tuya_ble_bulk_data_evt_type_t evt;
uint8_t bulk_type;
union
{
tuya_ble_bulk_data_evt_read_info_res_t bulk_info_res_data;
tuya_ble_bulk_data_evt_read_block_res_t block_res_data;
tuya_ble_bulk_data_evt_send_data_res_t send_res_data;
tuya_ble_bulk_data_evt_erase_res_t erase_res_data;
} params;
} tuya_ble_bulk_data_response_t;
Example:
void tuya_ble_app_bulk_data_handler(tuya_ble_bulk_data_request_t *p_data)
{
uint8_t rsp_flag = 1;
tuya_ble_bulk_data_response_t rsp_data;
switch (p_data->evt)
{
case TUYA_BLE_BULK_DATA_EVT_READ_INFO: {
// Parse the `p_data`.
// Assign values to `rsp_data.bulk_info_res_data`
} break;
case TUYA_BLE_BULK_DATA_EVT_READ_BLOCK: {
// Parse `p_data->params.block_data_req_data`
// Assign values to `rsp_data.block_res_data`
} break;
case TUYA_BLE_BULK_DATA_EVT_SEND_DATA: {
// Parse `p_data->params.send_data_req_data`
// Assign values to `rsp_data.send_res_data`
} break;
case TUYA_BLE_BULK_DATA_EVT_ERASE: {
// Parse the `p_data`.
// Assign values to `rsp_data.erase_res_data`
} break;
default: {
rsp_flag = 0;
} break;
}
if (rsp_flag)
{
tuya_ble_bulk_data_response(&rsp_data); // Return values
if (rsp_data.evt == TUYA_BLE_BULK_DATA_EVT_SEND_DATA) {
tuya_ble_free(rsp_data.params.send_res_data.p_current_block_data);
}
}
}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback