So here is the scenario...I am making bluetooth file transfer in android. First I am sending file name and then once file name is received I send file and receive it too.
Here is the code to receive the file name and file
while (hasFile) {
try {
if (Flags.isGotFileName()) {
mmInStream.mark(0);
bytes = mmInStream.read(buffer,0,8);
Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} else {
mmInStream.reset();
int KB = 0;
int compKB = 0;
int hasdata = 0;
File file = new File(Environment.getExternalStorageDirectory(), Flags.getFileName());
fos = new FileOutputStream(file);
hasdata=mmInStream.read(buffer,8,1016);
fos.write(buffer, 0, hasdata);
while ((hasdata = mmInStream.read(buffer)) != -1) {
Log.e(TAG, KB + "KB Copied");
fos.write(buffer, 0, hasdata);
KB++;
if (KB - compKB == 200) {
Log.e(TAG, KB + "KB Copied");
compKB = KB;
}
}
}
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothChatService.this.start();
break;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("FOSSSS", "CLOSEDDDD");
hasFile = false;
}
}
}
My problem is that after I get my file name (if part) which is of 8 bytes when I try to receive the file (else part) it is received but its size is 8 bytes less than the original one
I am new to socket communication so I cant figure out how to deal with this.