Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

character encoding - Android InputStream dropping first two bytes (modified BluetoothChat)

I've used code from BluetoothChat example to send and receive byte data from a Bluetooth Scale. The scale receives the command from the device, then sends back a byte array. {2,198,48,48,48,48,199,3} The 2 = STX, and the 198 = packet start, and 199 = packet end, and 3 = ETX in our comms protocol.

All works just fine, except that the following code in the BluetoothChatService.java reacts strangely in that it drops the first two bytes.

/**
     * This thread runs during a connection with a remote device.
     * It handles all incoming and outgoing transmissions.
     */
    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket, String socketType) {
            Log.d(TAG, "create ConnectedThread: " + socketType);
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            final 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();
                    // Start the service over to restart listening mode
                    BluetoothChatService.this.start();
                    break;
                }
            }
        }

I have a problem specifically with the following section of code:

 bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();

When debugging, and looking at the content of buffer in mmInStream.read(buffer) before it is executed, the buffer contains the correct data that was sent back by the scale device ie:

{2,198,48,48,48,48,48,199,3}

but once the code has been stepped, the first two bytes of the buffer are stripped off, and it now erroneously contains:

{48,48,48,48,48,199,3}

and it is this that the message handler then finally passes on to the activity.

For more clarity, I must add, that the stream of bytes being sent by the scale are Hex characters in the range 00 to FF. For some strange reason the string actually looks like this in the debugger:

{2,-58,48,48,48,48,48,-57,3}

and then the 2,-58 are dropped.

I noticed that when I send a byte array over a socket, I need to do the following:

byte[] sendBytes = {2,(byte)198,48,48,48,48,48,(byte)199,3}

When the content of this array is debugged it will give {2,-58,48,48,48,48,48,-57,3}

Please understand that I am new to Android - java, and have a lot to learn. All help will be appreciated. Thanks Adrian

I have added log.i entries to better understand what is happening based on Radu's advice. It appears that after I write data to my device over Bluetooth, it responds, and we read for some reason only first two bytes, then send these to the message handler, then read the rest of the packet sent from the device, and then send this off to the message handler, but before the handler has even responded the first time, the buffer has already been overwritten, thus by the time the handler tries to read the first two bytes, it is reading the 3rd and 4th bytes of the response packet, then immediately responds again and reads the entire packet from 3-17th position. So if I can put it simply .. the message handler only responds to the sent buffer after it has been overwritten. See the log below:

09-05 13:16:52.093: V/BluetoothSocket.cpp(11279): writeNative
09-05 13:16:52.118: I/IN_BUFFER(11279): The entire buffer after read stream into buffer: 2 
09-05 13:16:52.118: I/IN_BUF_AFTER(11279): 2 
09-05 13:16:52.118: I/IN_BUF_AFTER(11279): -58 
09-05 13:16:52.118: I/IN_BUF_AFTER(11279): 0 
09-05 13:16:52.118: I/IN_BUF_AFTER(11279): 0 
...truncated to save space ... 
09-05 13:16:52.163: I/IN_BUF_AFTER(11279): 0 
09-05 13:16:52.163: I/IN_BUFFER(11279): We now send to handler.
09-05 13:16:52.168: I/IN_BUFFER(11279): Read Stream into Buffer:
09-05 13:16:52.168: V/BluetoothSocket.cpp(11279): readNative
09-05 13:16:52.168: I/IN_BUFFER(11279): The entire buffer after read stream into buffer: 17 
09-05 13:16:52.168: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:52.168: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:52.168: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:52.173: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:52.173: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:52.173: I/IN_BUF_AFTER(11279): 44 
09-05 13:16:52.173: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:52.178: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:52.178: I/IN_BUF_AFTER(11279): 49 
09-05 13:16:52.178: I/IN_BUF_AFTER(11279): 50 
09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 44 
09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 85 
09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 13 
09-05 13:16:52.188: I/IN_BUF_AFTER(11279): -57 
09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 3 
09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 6 
09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 0 
...truncated to save space ... 
09-05 13:16:52.188: I/IN_BUF_AFTER(11279): 0 
09-05 13:16:52.188: I/IN_BUFFER(11279): We now send to handler.
09-05 13:16:52.193: I/IN_BUFFER(11279): Read Stream into Buffer:
09-05 13:16:52.208: V/BluetoothSocket.cpp(11279): readNative
09-05 13:16:52.208: I/MESSAGE_READ(11279): I am reading 2 bytes
09-05 13:16:52.208: I/Content(11279): The entire array:
09-05 13:16:52.208: I/some hardcoded tag(11279): 0 
09-05 13:16:52.208: I/some hardcoded tag(11279): 0 
09-05 13:16:52.273: I/MESSAGE_READ(11279): I am reading 17 bytes
09-05 13:16:52.273: I/Content(11279): The entire array:
09-05 13:16:52.273: I/some hardcoded tag(11279): 0 
...truncated to save space ... 
09-05 13:16:52.283: I/some hardcoded tag(11279): 0 
09-05 13:16:52.283: I/some hardcoded tag(11279): 0 
09-05 13:16:54.528: V/BluetoothSocket.cpp(11279): writeNative
09-05 13:16:54.553: I/IN_BUFFER(11279): The entire buffer after read stream into buffer: 2 
09-05 13:16:54.553: I/IN_BUF_AFTER(11279): 2 
09-05 13:16:54.553: I/IN_BUF_AFTER(11279): -58 
09-05 13:16:54.558: I/IN_BUF_AFTER(11279): 0 
09-05 13:16:54.558: I/IN_BUF_AFTER(11279): 0 
...truncated to save space ... 
09-05 13:16:54.618: I/IN_BUF_AFTER(11279): 0 
09-05 13:16:54.618: I/IN_BUF_AFTER(11279): 0 
09-05 13:16:54.618: I/IN_BUFFER(11279): We now send to handler.
09-05 13:16:54.618: I/IN_BUFFER(11279): Read Stream into Buffer:
09-05 13:16:54.618: V/BluetoothSocket.cpp(11279): readNative
09-05 13:16:54.623: I/IN_BUFFER(11279): The entire buffer after read stream into buffer: 17 
09-05 13:16:54.623: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:54.623: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:54.623: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:54.623: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:54.628: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:54.628: I/IN_BUF_AFTER(11279): 44 
09-05 13:16:54.628: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:54.628: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:54.633: I/IN_BUF_AFTER(11279): 49 
09-05 13:16:54.633: I/IN_BUF_AFTER(11279): 50 
09-05 13:16:54.638: I/IN_BUF_AFTER(11279): 48 
09-05 13:16:54.638: I/IN_BUF_AFTER(11279): 44 
09-05 13:16:54.638: I/IN_BUF_AFTER(11279): 85 
09-05 13:16:54.638: I/IN_BUF_AFTER(11279): 13 
09-05 13:16:54.638: I/IN_BUF_AFTER(11279): -57 
09-05 13:16:54.648: I/IN_BUF_AFTER(11279): 3 
09-05 13:16:54.648: I/IN_BUF_AFTER(11279): 6 
09-05 13:16:54.648: I/IN_BUF_AFTER(11279): 0 
09-05 13:16:54.648: I/IN_BUF_AFTER(11279): 0 
...truncated to save space ... 
09-05 13:16:54.653: I/IN_BUF_AFTER(11279): 0 
09-05 13:16:54.653: I/IN_BUF_AFTER(11279): 0 
09-05 13:16:54.653: I/IN_BUFFER(11279): We now send to handler.
09-05 13:16:54.653: I/IN_BUFFER(11279): Read Stream into Buffer:
09-05 13:16:54.653: V/BluetoothSocket.cpp(11279): readNative
09-05 13:16:54.658: I/MESSAGE_READ(11279): I am reading 2 bytes
09-05 13:16:54.658: I/Content(11279): The entire array:
09-05 13:16:54.658: I/some hardcoded tag(11279): 0 
09-05 13:16:54.663: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/MESSAGE_READ(11279): I am reading 17 bytes
09-05 13:16:54.723: I/Content(11279): The entire array:
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.723: I/some hardcoded tag(11279): 0 
09-05 13:16:54.728: I/some hardcoded tag(11279): 0 
09-05 13:16:54.728: I/some hardcoded tag(11279): 0 
09-05 13:16:54.728: I/some hardcoded tag(11279): 0 

My new code also resets the buffer to 0 before reading in the latest stream, thus the message handler only seeing 0, before I did this the log appeared as follows:

09-05 13:06:20.508: V/BluetoothSocket.cpp(10176): writeNative
09-05 13:06:20.533: I/IN_BUFFER(10176): The entire buffer after read stream into buffer: 2 
09-05 13:06:20.533: I/IN_BUF_AFTER(10176): 2 
09-05 13:06:20.533: I/IN_BUF_AFTER(10176): -58 
09-05 13:06:20.533: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.533: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.538: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.538: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.548: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.548: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.548: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.553: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.553: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.568: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.573: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.573: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.573: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.573

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have seen people run into this sort of problem before when using the Bluetooth Chat example. The problem with the example code is that the message object that is sent to the Handler simply contains a reference to the actual byte[] array that is used for each subsequent read() operation. This means that as soon as the Handler obtains the message and starts inspecting the array, the subsequent read() operation on the Bluetooth socket has already had the opportunity to write newer data to that array.

In this line of code:

mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget()

The array is not being copied; but rather the message is simply conveying an object reference to the same array.

The only reason why the Bluetooth Chat example works for its original purpose is because its purpose is to convey short bunches of characters at human typing speed. If you send anything faster than that, what the Handler reads from that array turns to garbage.

The answer is to send a copy of the array (e.g. System.arraycopy()) or use a simple circular buffer, which is what my own Bluetooth application uses.

The fact that the first two bytes are being mangled is a strange one, but it could just be down to the specific implementation of the underlying read operation in the Bluetooth stack on your particular device. The simple fact is that once you have called read() with that buffer, you should not be touching that buffer in the meantime or caring what's in it. Perhaps the particular Bluetooth socket read implementation on your device does something with those first few bytes in the buffer because of something to do with its internal operation. But you should not care what kind of funny intermediate state the buffer is in while read() is blocking, because no other thread should be trying to make sense of it at that time. All you care about is that the buffer is in a valid state with valid data when read() returns.

The reason why using the sleep() operation apparently partly "cures" the problem is because it's a crude way of giving your Handler time to look at the array before the subsequent read() operation gets its hands on the array. This is not a good solution though.

The second issue you have is due to the fact that in Java, byte is signed. That's why the debugger shows you the bytes as signed values. In your application, if you need to work with a given byte as an int and the byte is originally unsigned, you do something like:

int someValue = myByteArray[someIndex] & 0xff;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...