I’m using the below code to read data from a connected socket and the data I’m receiving in the buffer variable after I execute the System.arraycopy(tempBuffer, 0, buffer, 0, byteRead) code is of length 41 bytes. The sending system is sending 43 bytes. Anyone know why arraycopy is chopping off the other bytes?
public class BluetoothClientSocket{
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
//handle exception logic
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run(){
byte[] tempBuffer = new byte[1024];
byte[] buffer = null;
int byteRead;
while (true) {
try {
InputStream itStrm = mmInStream;
byteRead = mmInStream.read(tempBuffer);
if(byteRead > 0){
buffer = new byte[byteRead];
System.arraycopy(tempBuffer, 0, buffer, 0, byteRead);
}
}
catch(Exception ex){
//handle exception logic
}
}
}
}
}
That doesn’t mean you’re receiving all 43 bytes in a single call to
read.It’s not. I suspect you’ll find that
byteReadis 41, which means the “problem” occurs beforearraycopygets involved.I’d expect you to get the remaining two bytes (and possibly more data, if the other end has sent any more) on the next call to
read.Either that or your code is wrong on the other end, and you’re only sending 41 bytes to start with.