I needed some guidence for streaming to transfer file or data with the remote devices or server. Help me to find what happening in that code just stucked from last 5 days with this error:
W/System.err: java.io.IOException: bt socket closed, read return: -1
W/System.err: at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:495) 08-12 03:49:13.242 6973-11690/io.connection.bluetooth
W/System.err: at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) 08-12 03:49:13.242 6973-11690/io.connection.bluetooth
W/System.err: at io.connection.bluetooth.Thread.AcceptThread$readFile.run(AcceptThread.java:90)
Code:
package io.connection.bluetooth.Thread;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import io.connection.bluetooth.utils.Constants;
public class AcceptThread extends Thread {
private final BluetoothServerSocket serverSocket;
private static final String TAG = "AcceptThread";
BluetoothSocket socket = null;
public AcceptThread(BluetoothAdapter bluetoothAdapter) {
BluetoothServerSocket tmp = null;
try {
tmp = bluetoothAdapter
.listenUsingRfcommWithServiceRecord(
Constants.NAME_UUID, Constants.uuid);
} catch (IOException e) {
}
serverSocket = tmp;
}
public void run() {
while (true) {
try {
socket = serverSocket.accept();
if (socket.isConnected()) {
Log.d(TAG, "run: connection successfull");
Log.d(TAG, "run: " + socket.getRemoteDevice().getName() + " " +
socket.getRemoteDevice().getAddress());
readFile readfile = new readFile(socket);
readfile.start();
}
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "run: " + e.getMessage());
}
}
}
class readFile extends Thread {
private static final String TAG = "readFile";
BluetoothSocket socket = null;
InputStream in = null;
OutputStream out = null;
readFile(BluetoothSocket socket) {
this.socket = socket;
}
@Override
public void run() {
Log.d(TAG, "run: reading Start");
byte[] bytes = new byte[16 * 1024];
int count;
int total = 0;
String filename = System.nanoTime() + ".jpg";
File file = new File(Environment.getExternalStorageDirectory(), filename);
Log.d(TAG, "run: file path "+file.getPath());
try {
in = socket.getInputStream();
out = new FileOutputStream(file);
try {
//got error on this while loop after reading all data from stream
while ((count = in.read(bytes)) > 0) {
total += count;
out.write(bytes, 0, count);
Log.d(TAG, "run: " + total + " " + count + " " + in.available());
}
Log.d(TAG, "run: count End " + in.available());
out.close();
in.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "run: readFile " + e.getMessage());
} finally {
try {
Log.d(TAG, "run: socket close");
socket.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
}