Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bluetooth socket tutorial for non-blocking communication

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

like image 769
user387184 Avatar asked Nov 20 '11 20:11

user387184


People also ask

How to connect Bluetooth socket in Android?

To create a BluetoothSocket for connecting to a known device, use BluetoothDevice. createRfcommSocketToServiceRecord() . Then call connect() to attempt a connection to the remote device. This call will block until a connection is established or the connection fails.

What is the purpose of the Bluetooth server socket?

The interface for Bluetooth Sockets is similar to that of TCP sockets: Socket and ServerSocket . On the server side, use a BluetoothServerSocket to create a listening server socket. When a connection is accepted by the BluetoothServerSocket , it will return a new BluetoothSocket to manage the connection.

Does Android support Bluetooth SPP?

Android allows communication with Bluetooth devices which have SPP.


1 Answers

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());
  } 
}
}
like image 165
GregM Avatar answered Oct 23 '22 22:10

GregM