8

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
  • I'm looking for same and coming up empty, if you find a solution please update answer, I will do same!
    – GregM
    Nov 21, 2011 at 15:24

1 Answer 1

13

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());
  } 
}
}
2
  • 1
    I should mention this is "Professional Android 2 Application Development" By Reto Meier
    – GregM
    Nov 21, 2011 at 19:51
  • 1
    just for info, I actually implemented non blocking and timeout myself using two seperate timers. One time just exists the waiting BT loop if it does not receive anything within a minimum time and the second timer is used to define the maximum time the loop waits for data. This way I finally can control the communication
    – user387184
    May 17, 2012 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.