Sending file via Bluetooth SPP
Posted: Fri Jan 14, 2022 11:24 am
I have 2 ESP32 Dev Boards. One (call it Sender) send data to another(call it Reciver) via Bluetooth connection and SPP. Both have microSD adapters attached.
How it works:
1. Sender take command to upload file.
2. Open file and forming packets with specific size. I use 64 bytes. 1st byte of each packet is a command byte, 2-64 - data.
3. Send file to Reciver with esp_spp_write() like this:
4. ESP_SPP_DATA_IND_EVT happen in Reciver and Reciver copy data to buffer and call function that parse the packet.
Function packet_parser() do a lot of things but in next part:
If we recived a packet with 1st byte 0x01, Reciver extract data from it (Name of the file).
And here comes the problem. In this part
Log say
I (8834) Name : history
I (8844) Name : 68 69 73 74 6f 72 79
I (8844) File name recieved : hist
I (8844) File name recieved : 68 69 73 74
So in char name[] I have actual name but if I try to copy it to char* filename it copy only 4 bytes.
And it every time when I try to work with data.
If send name[] to function file_path(char* name) I still recive 4 bytes[/Codebox].
If change packet_parser() so it recive buffer like packet_parser(char * buff) I still recive only 4 bytes.
Any idea why it dissapear?
How it works:
1. Sender take command to upload file.
2. Open file and forming packets with specific size. I use 64 bytes. 1st byte of each packet is a command byte, 2-64 - data.
3. Send file to Reciver with esp_spp_write() like this:
Code: Select all
esp_log_buffer_hex("Packet is ready ",data_buff,sizeof(data_buff));
ESP_LOGI(TAG, "Size=%d", sizeof(data_buff));
esp_spp_write(connection_handle, sizeof(data_buff), data_buff);
Code: Select all
case ESP_SPP_DATA_IND_EVT:
ESP_LOGI(SPP_TAG, "Incoming packet");
memmove(data_buff,param->data_ind.data,param->data_ind.len);
esp_log_buffer_hex("Packet recieved ",data_buff,sizeof(data_buff));
ESP_LOGI(SPP_TAG, "Size %d", param->data_ind.len);
packet_parser();
break;
Code: Select all
case 0x01: //When we RECEIVE file from phone
if(BL_MODE == BL_COMMAND_MODE)
{
int counter = 0;
for (int i=1;i<sizeof(data_buff);i++)
{
//packet_buffer[i-1]=buffer[i];
if(data_buff[i]!=0x00)
counter++;
}
char name[counter];
for (int i=1;i<counter+1;i++)
{
name[i-1]=(char)data_buff[i];
}
filename = malloc(sizeof(name));
esp_log_buffer_char("Name ",name,sizeof(name));
esp_log_buffer_hex("Name ",name,sizeof(name));
memmove(filename,name,sizeof(name));
esp_log_buffer_char("File name recieved ",filename,sizeof(filename));
esp_log_buffer_hex("File name recieved ",filename,sizeof(filename));
//strcpy(filename, (char*)packet_buffer);
//Check if there is file with such name
if (f_check(filename ) != 0)
{
ESP_LOGI(TAG, "File already exist(write section)");
//Send command packet to say it
break;
}
ESP_LOGI(TAG, "Opening file");
f = fopen(file_path(filename ), "wb");
if (f == NULL)
{
esp_log_buffer_char("File path ",file_path(),sizeof(file_path()));
ESP_LOGE(TAG, "Failed to open file for writing");
break;
}
ESP_LOGI(TAG, "File opened");
BL_MODE = BL_DATA_MODE;
ESP_LOGI(TAG, "BT MODE %d", BL_MODE);
break;
}
And here comes the problem. In this part
Code: Select all
esp_log_buffer_char("Name ",name,sizeof(name));
esp_log_buffer_hex("Name ",name,sizeof(name));
memmove(filename,name,sizeof(name));
esp_log_buffer_char("File name recieved ",filename,sizeof(filename));
esp_log_buffer_hex("File name recieved ",filename,sizeof(filename));
I (8834) Name : history
I (8844) Name : 68 69 73 74 6f 72 79
I (8844) File name recieved : hist
I (8844) File name recieved : 68 69 73 74
So in char name[] I have actual name but if I try to copy it to char* filename it copy only 4 bytes.
And it every time when I try to work with data.
If send name[] to function file_path(char* name) I still recive 4 bytes
- [Codebox=c file=Untitled.c]
If change packet_parser() so it recive buffer like packet_parser(char * buff) I still recive only 4 bytes.
Any idea why it dissapear?