I have the following code and I use ReadUnsignedByte() function but hangs and doesn’t return anything which cause hanging the application. I used try catch but there is no response, so what shall I do in this case ?
private int getEndOfSeqeunce(DataInputStream in, byte[] sequence) throws TimeoutException {
int seqIndex = 0;
byte c = -99;
for(int i=0; i < FRAME_MAX_LENGTH; i++) {
Log.v("DataInputStream", ""+in);
try {
c = (byte) in.readUnsignedByte(); // Stuck here ... No response at this line which hangs the Android application.
Log.v("C ::::::::UNSIGNE::readUnsignedByte::::::::", ""+c);
if(c == sequence[seqIndex]) {
seqIndex++;
if(seqIndex == sequence.length) return i + 1;
} else seqIndex = 0;
} catch (IOException e) {
e.printStackTrace();
i = FRAME_MAX_LENGTH;
Activity ac = (Activity) cox;
}
}
return -1;
}
It’s a blocking call and you can’t set a timeout, it has to either return data or fail with an exception. If you are reading from a socket, it will block until the read timeout is reached (if set). You might want to run this in a separate thread in order to not hang the UI, but it will still block. If this is for a communication protocol, maybe you have a problem some where and are expecting data that is not being sent.