Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Looks like the answer is pretty much you can't</p> <p>however with a bit of threading magic, your can have your system work the way you want</p> <pre><code> BluetoothSocketListener bsl = new BluetoothSocketListener(socket, handler, messageText); Thread messageListener = new Thread(bsl); messageListener.start(); </code></pre> <p>message system</p> <pre><code> 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); } } </code></pre> <p>socket listener</p> <pre><code>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)&amp;&amp;(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()); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload