Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Can't say I fully understand your code but got a couple of hints that could help.</p> <p>Please check <a href="http://developer.android.com/guide/topics/connectivity/bluetooth.html#ManagingAConnection" rel="nofollow">Managing a Connection</a> part. As you can see;</p> <blockquote> <p>You should use a <em>dedicated thread</em> for all stream reading and writing. This is important because both <em>read(byte[])</em> and <em>write(byte[])</em> methods are <strong>blocking calls</strong>. <em>read(byte[])</em> will block <strong>until there is <em>something</em> to read from the stream</strong>. <em>write(byte[])</em> does not usually block, but can block for flow control if the remote device is not calling <em>read(byte[])</em> quickly enough and the intermediate buffers are full. So, your main loop in the thread should be dedicated to reading from the InputStream.</p> </blockquote> <p>First for a better design you should use a dedicated thread for reading and one another for writing. You should separate the part you form your data, write it to OutputStream and read from InputStream.</p> <p>When i started coding my <em>Reader Thread</em>, I tho <em>read(byte[])</em> method of InputStream will block execution until there are desired number of bytes. But it doesn't. I figured out that it does block until there is "something" to read from the stream (the part which i previously missed).</p> <hr> <p>You have a byte array of size 500. What is the entry in log when you "can not" (in this case) read the stream in part:</p> <pre><code>int byteRead = inStream.read(alreadyReceived); Log.i(TAG, "sono stati" + byteRead + " byte"); </code></pre> <p>My point is: Consider your package is; 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09, you sent it via OutputStream write and flushed. You still might get <strong>only a part of it</strong> (even 1 byte -> 0x01) in one read() call, and get the rest of it (0x02 and the rest) in the next read() call. In this case <code>byteRead</code> will be equal to 1 and <code>alreadyReceived[0]</code> will be 0x01 and the other elements will be 0</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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