Is it possible to implement a timeout in an inputstream.read() function from a BluetoothSocket in Android?
I've tried using Thread.sleep() but this only pauses my activity.
---Update---
I had an idea, make 2 thread code herereads(t1 & t2) where each thread interrupt other, one of them(t1) do a sleep(5000) then interrupt the other thread(t2), from the other side the other thread(t2) if in read the inputstream detects some character as 0x0D interrupt the other thread(t1), but here is my question, does anybody can help me? because i didn't use interrupt() method of threads, I hope someone can help me, thank you...
---Update---
public void run(){
while(true){
try {
char r;
String respuesta = "";
while (true) {
r = (char) mmInStream.read();
respuesta += r;
if (r == 0x3e) {
break;
}
}
respuesta = respuesta.replaceAll(" ", "");
Log.d("respuesta", respuesta);
rHandler.obtainMessage(MESSAGE_READ, -1, -1, respuesta).sendToTarget();
} catch (IOException readException) {
Log.e("ServicioGeneral", "Error de lectura", readException);
this.interrupt();
connectionLost();
// posibly break();
}
}
}
This is my implementation when something comes in a different Thread, the problem is that the timeout will be reached if i dont get the 0x3e character from de mmInStream.
I supposed that in the second example i must use a notifyAll(), but, when do I have to start the readThread()?
Thank you, @weeman
You could do something like this:
This way you are able to initially read from a stream with a specific timeout. If u want to implement a timeout at any time of reading you can try something like this:
using this you may need some kind of event handler to notify your application if the thread actually read something from the input stream.
I hope that helps!
----edit:
I implemented an example without using any handlers.