Byte transfer over Bluetooth trouble
I am trying to send file data across Bluetooth using RFCOMM from a phone to a PC.
The way I am doing this is to first read the size of the file, and send that to the PC as a string, which the PC then parses as an Integer (I'm doing this because other data is sent along with the file size, but it is not being used for anything yet, so I'm ignoring it for this explanation).
The code doing this is:
FileConnection fc = (FileConnection)Connector.open(currentPath);
int currentSize = (int)fc.fileSize();
fc.close();
String toSend = currentSize;
connection = (StreamConnection)Connector.open(transmitUrl);
out = connection.openDataOutputStream();
out.writeUTF(toSend);
out.flush();
At the receiving end, there is this code:
StreamConnection connection = notifier.acceptAndOpen();
RemoteDevice rdev = RemoteDevice.getRemoteDevice(connection);
DataInputStream dataIn = connection.openDataInputStream();
String commandIn = dataIn.readUTF();
System.out.println(Integer.parseInt(commandln) +" bytes expected");
NOTE: I have cut bits of code here and there that don't really have any direct relevance. You'll have to accept that 'notifier' and other variables have been initialised already.
This works fine. I get the file size (seems to be accurate) and it is sucessfully sent, parsed and printed to the screen. The phone then waits for a user input (just a confirm button press to allow the PC to sort itself out) before running this code:
fc = (FileConnection)Connector.open(filePath);
InputStream fis fc.openInputStream();
byte[] fileData =newbyte[currentSize];
int length = fis.read(fileData, 0, currentSize);
out.write(fileData, 0, fileData.length);
out.flush();
The PC has this code for accepting this data:
byte[] receivedData =newbyte[Integer.parseInt(commandln)];
int result = dataIn.read(fileData, 0, fileData.length);
System.out.println(result +" bytes read");
But what happens at this point is the Phone proceeds fine, but the PC prints out:
1499 bytes read
Upper layer is not properly registered..error in data transferUpper layer is not properly registered..error in data transferUpper layer is not properly registered..error in data transfer
It doesn't matter what size the file is, the 1499 value is always the same (I haven't actually tried it with a file less then 1499 bytes in length because I don't have one). I really can't figure it out, and it is desperatly needed for a student project that is due in very soon.
If anyone can offer a reason and/or a solution, I will be very grateful. I have 10 points on offer because I really need to fix this!
EDIT: I've had an experiment, and forced it to only send 1498 bytes and it worked fine. 1499 bytes also works fine, but 1500 bytes causes the error. Why is this?

