Like for example, in Google's bluetoothchat, they are doing the same
thing that is the model I am following. What is the difference?
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ,
bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
On Apr 23, 4:03 pm, spachner <[email protected]> wrote:
> Hi,
>
> 1) "As your wrote "a bluetooth device that is sending the temperature
> to the android phone in a constant _stream_", so not expect that the
> characters are coming somehow synced. Depending on internal socket
> stuff sometimes just a single char is received, sometimes more at
> once. The receiver is responsible to collect the read chars and do the
> sematic, i.e. the interpretation of the data by yourself. I would
> recommend to pack the data at the sender with chars which allows the
> receiver to pick the payload in between easily. E.g. when you want to
> transport temperature values the payload in something out of these
> chars "0123456789.". Then you could put a prefix (e.g. "^") and a
> postfix (e.g. "$") which would result in "^38$". The delimiting
> characters actually do not matter just use which is guaranteed not in
> the payload. The receiver then just collects the strings received and
> post process which is simply searching for the prefix and the postfix.
> Wait until both are in the collected string, then remove them and the
> chars in between is your payload, your temperature value.
>
> 2) Seems that the handler mHandler is one from the thread the read()
> is done and not from thread the actvitiy runs in as you want. If so
> the handleMessage() could only be executed when the thread is not
> blocked anymore what happens when the while(true) loop is broken by
> the exception.
>
> Hope I am right and it helps!
>
> regards
>
> spachner
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group
> athttp://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en