Hi All,
I am trying to transfer file from one device to another using
Bluetooth. But, I am getting following “Connection reset by peer”
exception:-
java.io.IOException: Connection reset by peer
07-11 12:20:29.042: W/System.err(8598): at
android.bluetooth.BluetoothSocket.readNative(Native Method)
07-11 12:20:29.042: W/System.err(8598): at
android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:336)
07-11 12:20:29.042: W/System.err(8598): at
android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
07-11 12:20:29.042: W/System.err(8598): at
java.io.InputStream.read(InputStream.java:157)
07-11 12:20:29.042: W/System.err(8598): at
com.example.bluetoothapi8file.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:421)
And, it will write the file completely from client side but on
server side it was not read completely.
If I will comment the code
mmOutStream.close(); //In client side -> write(File
fileIn) method
Then it will transfer the file completely.
But in that case control will never comes out of the *while
loop*written in server side read(File
fileIn) method.
So, control will not comes to line
mHandler.obtainMessage(BluetoothMainActivity.MESSAGE_READ_COMPLETED, -1,
-1); //In Server side -> read(File fileIn) method
and I cannot send msg or update the UI.
Please provide me some solution as early as possible.
*Client Side:-*
*//Code for Writing to the *mmOutStream (read chunk of bytes from File)
*public* *void* write(File fileIn) {
*byte*[] buffer = *new* *byte*[1024];
*try* {
mmFileInStream = *new* FileInputStream(fileIn);
*long* *total_bytes* = 0;
*int* byteRead = 0;
*while* ((byteRead = mmFileInStream.read(buffer)) !=
-1) {
System.*out*.println("Write: " + byteRead);
total_bytes += byteRead;
mmOutStream.write(buffer, 0, byteRead);
}
Log.*d*(*TAG*, "Writing to the outstream
completed))))))))))))))))))))))");
} *catch* (Exception e) {
e.printStackTrace();
}
*finally* {
*try* {
*if* (mmFileInStream != *null*) {
mmFileInStream.close();
}
*if* (mmOutStream != *null*) {
*mmOutStream**.close();* *//if
commented*
}
*if* (mmSocket != *null*) {
/*try {
mmSocket.close();
} catch (IOException e) {
//catch logic
}*/
}
} *catch* (Exception e) {
e.printStackTrace();
}
}
}
*Server Side:-*
*//Code for Writing to the *mmFileOutStream (read chunk of bytes from
mmInStream)
*public* *void* read(File fileIn) {
mmFileOutStream = *new* FileInputStream(fileIn);
try {
long total_bytes = 0;
*int* byteRead = 0;
while ((byteRead = mmInStream.read(buffer)) != -1) {
System.out.println("read: " + byteRead);
total_bytes += byteRead;
mmFileOutStream.write(buffer, 0, byteRead);
}
Log.d(TAG, "Inside finally block
MESSAGE_READ_COMPLETED))))))))))))");
* mHandler.obtainMessage(BluetoothMainActivity.MESSAGE_READ_COMPLETED,
-1, -1);*
} catch (Exception e) {
Log.d(TAG, "Inside Catch with
*erroe*block))))))))))))"+e.toString());
e.printStackTrace();
} finally {
Log.d(TAG, "Inside finally block))))))))))))");
try {
if (mmInStream != null) {
mmInStream.close();
}
if (mmFileOutStream != null) {
mmFileOutStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Log.d(TAG, "Inside Outside read Catch with
*erroe*block))))))))))))");
}
Thanks & Regards,
DEEPAK KUMAR
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en