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 6865501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:03:35+00:00 2026-05-27T03:03:35+00:00

I am looking for a bluetooth sample code on Android to do non-blocking socket

  • 0

I am looking for a bluetooth sample code on Android to do non-blocking socket communication.

I found several examples, like the BluetoothChat or BluetoothSocket.java but none is non-blocking socket communication.

ps does non-blocking automatically mean that is has to be asynchronous? I think actually not – it is not the same, I assume I could do synchronous socket communication with a timeout. That’s the kind of example I am looking for…

Thank you very much

  • 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-05-27T03:03:36+00:00Added an answer on May 27, 2026 at 3:03 am

    Looks like the answer is pretty much you can’t

    however with a bit of threading magic, your can have your system work the way you want

       BluetoothSocketListener bsl = new BluetoothSocketListener(socket, handler, messageText);
        Thread messageListener = new Thread(bsl);
        messageListener.start();
    

    message system

     private class MessagePoster implements Runnable {
        private TextView textView;
        private String message;
    
        public MessagePoster(TextView textView, String message) {
          this.textView = textView;
          this.message = message;
        }
    
        public void run() {
          textView.setText(message);
        }     
      }
    

    socket listener

    private class BluetoothSocketListener implements Runnable {
    
      private BluetoothSocket socket;
      private TextView textView;
      private Handler handler;
    
      public BluetoothSocketListener(BluetoothSocket socket, 
                                     Handler handler, TextView textView) {
        this.socket = socket;
        this.textView = textView;
        this.handler = handler;
      }
    
    public void run() {
      int bufferSize = 1024;
      byte[] buffer = new byte[bufferSize];      
      try {
        InputStream instream = socket.getInputStream();
        int bytesRead = -1;
        String message = "";
        while (true) {
          message = "";
          bytesRead = instream.read(buffer);
          if (bytesRead != -1) {
            while ((bytesRead==bufferSize)&&(buffer[bufferSize-1] != 0)) {
              message = message + new String(buffer, 0, bytesRead);
              bytesRead = instream.read(buffer);
            }
            message = message + new String(buffer, 0, bytesRead - 1); 
    
            handler.post(new MessagePoster(textView, message));              
            socket.getInputStream();
          }
        }
      } catch (IOException e) {
        Log.d("BLUETOOTH_COMMS", e.getMessage());
      } 
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my android application, I'm looking for a particular bluetooth device. When I found
I'm looking for example code for setting up a connection to a bluetooth device
I'm desperately looking for some C sample source code that describes how to implement
I'm looking to write some Android or iOS applications to access a Bluetooth heart-rate
I'm currently looking into communicating with a bluetooth enabled device from an android app
I'm looking library, to create Bluetooth connection between my device and other devices. I
Looking for feedback on : http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools
Looking to do a very small, quick 'n dirty side project. I like the
I have been looking into communication methods between two iPhone devices and as I
I am currently making an Android utility related to Bluetooth, and I need to

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.