Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7934865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:45:15+00:00 2026-06-03T21:45:15+00:00

Is it possible to implement a timeout in an inputstream.read() function from a BluetoothSocket

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:45:17+00:00Added an answer on June 3, 2026 at 9:45 pm

    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) {
        timeout++;
        // throws interrupted exception
        Thread.sleep(250);
    }
    
    byte[] read = new byte[available];
    in.read(read);
    

    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
          readThread.start();
          readThread.wait(timeOutInMilliSeconds);
       }
    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!

    —-edit:

    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 = in.read()) >= 0) {
                        System.out.println(new String(new byte[]{ (byte) read }));
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    
        synchronized (readThread) {
            readThread.start();
            try {
                readThread.wait(2000);
    
                if(readThread.isAlive()) {
                                        // probably really not good practice!
                    in.close();
                    System.out.println("Timeout exceeded!");
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to implement a function that results in this mapping: { (0x01,
Is it possible to implement this using templates preferably in a way that would
Possible Duplicate: Timeout on a Python function call I want to implement that when
Possible Duplicate: Implement page curl on android? How to do page flipping/turning or curl
Is it possible to implement a Subclass, that overrides a function, which returns a
Is it possible to implement a smooth transition when the app loads, from the
Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle , but
Possible Duplicate: implement actionsheet in android I am trying to implement the action sheet
Is possible to implement this kind of animation (Screenshot below) on UITableView when expanding
Is it possible to implement an abstract base class with members inherited from another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.