I'm attempting to transfer a large file (2.7MB) from a server device to an android device via bluetooth using SPP. Upon connection, the android device reads the bytes available and store into a buffer of 2.7MB. I am using the variable "counter" (counting from 0 to 127) to verify the data.
To experiment, all data is correct if the server device outputs the file byte by byte. However, if the device outputs in packets of n bytes (e.g. 128 in this case), the data received on android was corrupted without any consistent pattern - it would incorrectly read values as 0s or 1s, but it always fixed itself at the start of the packet.
Any suggestions on how to prevent loss/corruption of data is greatly appreciated.
Receiving on Android device:
int imageSize = 2718720;
int counter = 0;
int totalByte = 0;
byte[] buffer = new byte[imageSize];
while (totalByte < imageSize) {
int bytesAvailable = mInStream.available();
int numOfBytesRead = mInStream.read(buffer, totalByte, bytesAvailable);
for (int i = totalByte; i < totalByte + numOfBytesRead; i++) {
if (buffer[i] != counter) {
Log.d("BTDevice", Integer.toString(i) + ". read byte = " + Integer.toString(buffer[i] & 0xff) + " - Failed");
} else {
Log.d("BTDevice", Integer.toString(i) + ". read byte = " + Integer.toString(buffer[i] & 0xff));
}
counter++;
if (counter == 128) {
counter = 0;
}
}
totalByte += actualBytesRead;
}