bluetooth – Implement a timeout in BluetoothSocket in Android-ThrowExceptions

Exception or error:

Is it possible to implement a timeout in an function from a BluetoothSocket in Android?

I’ve tried using Thread.sleep() but this only pauses my activity.


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…


        public void run(){
            try {
            char r;
            String respuesta = "";
            while (true) {
                    r = (char);
                respuesta += r;
                if (r == 0x3e) {
            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);
            // 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

How to solve:

You could do something like this:

InputStream in = someBluetoothSocket.getInputStream();
int timeout = 0;
int maxTimeout = 8; // leads to a timeout of 2 seconds
int available = 0;

while((available = in.available()) == 0 && timeout < maxTimeout) {
    // throws interrupted exception

byte[] read = new byte[available];;

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:

Thread readThread = new ReadThread(); // being a thread you use to read from an InputStream
try {
   synchronized (readThread) {
      // edit: start readThread here
catch (InterruptedException e) {}

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!


I implemented an example without using any handlers.

Socket s = new Socket("localhost", 8080);
    final InputStream in = s.getInputStream();

    Thread readThread = new Thread(new Runnable() {
        public void run() {
            int read = 0;
            try {
                while((read = >= 0) {
                    System.out.println(new String(new byte[]{ (byte) read }));
            } catch (IOException e) {
                // TODO Auto-generated catch block

    synchronized (readThread) {
        try {

            if(readThread.isAlive()) {
                                    // probably really not good practice!
                System.out.println("Timeout exceeded!");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block



A Kotlin extension function for socket read with timeout:

  buffer: ByteArray,
  offset: Int,
  length: Int,
  timeout: Long
): Int = runBlocking {
    val readAsync = async {
        if (available() > 0) read(buffer, offset, length) else 0
    var byteCount = 0
    var retries = 3
    while (byteCount == 0 && retries > 0) {
        withTimeout(timeout) {
            byteCount = readAsync.await()

Leave a Reply

Your email address will not be published. Required fields are marked *