Android InputStream soltando dois primeiros bytes (BluetoothChat modificado)

Eu usei o código do exemplo BluetoothChat para enviar e receber dados de bytes de uma escala Bluetooth. A escala recebe o comando do dispositivo e envia de volta uma matriz de bytes. {2,198,48,48,48,48,199,3} O 2 = STX e o 198 = início de pacote e 199 = fim de pacote e 3 = ETX em nosso protocolo de comunicação.

Tudo funciona muito bem, exceto que o seguinte código no BluetoothChatService.java reage estranhamente em que ele cai os dois primeiros 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;
                }
            }
        }

Eu tenho um problema especificamente com a seguinte seção de código:

 bytes = mmInStream.read(buffer);

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

Quando depuração, e olhando para o conteúdo do buffer em mmInStream.read (buffer) antes que ele seja executado, o buffer contém os dados corretos que foi enviado de volta pelo dispositivo de escala ou seja:

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

mas uma vez que o código foi escalonado, os dois primeiros bytes do buffer são removidos, e agora contém erroneamente:

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

e é isso que o manipulador de mensagens finalmente passa para a atividade.

Para maior clareza, devo acrescentar, que o fluxo de bytes enviados pela escala são caracteres hexadecimais no intervalo de 00 a FF. Por algum motivo estranho a string realmente se parece com isso no depurador:

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

e então os 2, -58 são descartados.

Notei que quando envio uma matriz de bytes por um soquete, preciso fazer o seguinte:

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

Quando o conteúdo deste array é debugado, ele dará {2, -58,48,48,48,48,48, -57,3}

Por favor, entenda que eu sou novo no Android - java, e tenho muito a aprender. Toda a ajuda será apreciada. Obrigado Adrian

Eu adicionei entradas log.i para entender melhor o que está acontecendo com base no conselho de Radu. Parece que depois de eu gravar dados no meu dispositivo por Bluetooth, ele responde, e lemos por algum motivo apenas dois primeiros bytes, depois os enviamos ao manipulador de mensagens, depois lemos o restante do pacote enviado do dispositivo e depois os enviamos. isto para o manipulador de mensagens, mas antes mesmo que o manipulador tenha respondido na primeira vez, o buffer já foi sobrescrito, assim, no momento em que o manipulador tenta ler os dois primeiros bytes, ele está lendo os bytes 3 e 4 da resposta. pacote, então imediatamente responde novamente e lê o pacote inteiro da 3-17ª posição. Então, se eu posso colocá-lo simplesmente .. o manipulador de mensagens só responde ao buffer enviado após ter sido substituído. Veja o log abaixo:

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 

Meu novo código também redefine o buffer para 0 antes de ler no último fluxo, assim, o manipulador de mensagens só vendo 0, antes de fazer isso, o log apareceu da seguinte forma:

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: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.573: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.578: I/IN_BUFFER(10176): We now send to handler.
09-05 13:06:20.578: I/IN_BUFFER(10176): Read Stream into Buffer:
09-05 13:06:20.578: V/BluetoothSocket.cpp(10176): readNative
09-05 13:06:20.578: I/IN_BUFFER(10176): The entire buffer after read stream into buffer: 17 
09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:20.578: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:20.583: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:20.583: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:20.583: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 44 
09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 49 
09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 51 
09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 44 
09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 85 
09-05 13:06:20.593: I/IN_BUF_AFTER(10176): 13 
09-05 13:06:20.598: I/IN_BUF_AFTER(10176): -57 
09-05 13:06:20.598: I/IN_BUF_AFTER(10176): 3 
09-05 13:06:20.613: I/IN_BUF_AFTER(10176): 6 
09-05 13:06:20.613: I/IN_BUF_AFTER(10176): 0 
...truncated to save space ... 
09-05 13:06:20.623: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:20.623: I/IN_BUFFER(10176): We now send to handler.
09-05 13:06:20.623: I/IN_BUFFER(10176): Read Stream into Buffer:
09-05 13:06:20.623: V/BluetoothSocket.cpp(10176): readNative
09-05 13:06:20.628: I/MESSAGE_READ(10176): I am reading 2 bytes
09-05 13:06:20.628: I/Content(10176): The entire array:
09-05 13:06:20.628: I/some hardcoded tag(10176): 48 
09-05 13:06:20.628: I/some hardcoded tag(10176): 48 
09-05 13:06:20.688: I/MESSAGE_READ(10176): I am reading 17 bytes
09-05 13:06:20.688: I/Content(10176): The entire array:
09-05 13:06:20.688: I/some hardcoded tag(10176): 48 
09-05 13:06:20.688: I/some hardcoded tag(10176): 48 
09-05 13:06:20.688: I/some hardcoded tag(10176): 48 
09-05 13:06:20.688: I/some hardcoded tag(10176): 48 
09-05 13:06:20.688: I/some hardcoded tag(10176): 48 
09-05 13:06:20.688: I/some hardcoded tag(10176): 44 
09-05 13:06:20.688: I/some hardcoded tag(10176): 48 
09-05 13:06:20.693: I/some hardcoded tag(10176): 48 
09-05 13:06:20.693: I/some hardcoded tag(10176): 49 
09-05 13:06:20.693: I/some hardcoded tag(10176): 51 
09-05 13:06:20.693: I/some hardcoded tag(10176): 48 
09-05 13:06:20.693: I/some hardcoded tag(10176): 44 
09-05 13:06:20.693: I/some hardcoded tag(10176): 85 
09-05 13:06:20.693: I/some hardcoded tag(10176): 13 
09-05 13:06:20.693: I/some hardcoded tag(10176): -57 
09-05 13:06:20.693: I/some hardcoded tag(10176): 3 
09-05 13:06:20.693: I/some hardcoded tag(10176): 6 
09-05 13:06:21.788: V/BluetoothSocket.cpp(10176): writeNative
09-05 13:06:21.803: I/IN_BUFFER(10176): The entire buffer after read stream into buffer: 2 
09-05 13:06:21.803: I/IN_BUF_AFTER(10176): 2 
09-05 13:06:21.803: I/IN_BUF_AFTER(10176): -58 
09-05 13:06:21.803: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.803: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.808: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.808: I/IN_BUF_AFTER(10176): 44 
09-05 13:06:21.818: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.818: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.818: I/IN_BUF_AFTER(10176): 49 
09-05 13:06:21.823: I/IN_BUF_AFTER(10176): 51 
09-05 13:06:21.823: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.828: I/IN_BUF_AFTER(10176): 44 
09-05 13:06:21.828: I/IN_BUF_AFTER(10176): 85 
09-05 13:06:21.833: I/IN_BUF_AFTER(10176): 13 
09-05 13:06:21.848: I/IN_BUF_AFTER(10176): -57 
09-05 13:06:21.848: I/IN_BUF_AFTER(10176): 3 
09-05 13:06:21.848: I/IN_BUF_AFTER(10176): 6 
09-05 13:06:21.853: I/IN_BUF_AFTER(10176): 0 
...truncated to save space ... 
09-05 13:06:21.853: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:21.853: I/IN_BUFFER(10176): We now send to handler.
09-05 13:06:21.858: I/IN_BUFFER(10176): Read Stream into Buffer:
09-05 13:06:21.858: V/BluetoothSocket.cpp(10176): readNative
09-05 13:06:21.858: I/IN_BUFFER(10176): The entire buffer after read stream into buffer: 17 
09-05 13:06:21.858: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.863: I/IN_BUF_AFTER(10176): 44 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 49 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 51 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 48 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 44 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 85 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 13 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): -57 
09-05 13:06:21.868: I/IN_BUF_AFTER(10176): 3 
09-05 13:06:21.873: I/IN_BUF_AFTER(10176): 6 
09-05 13:06:21.873: I/IN_BUF_AFTER(10176): 0 
...truncated to save space ...
09-05 13:06:21.893: I/IN_BUF_AFTER(10176): 0 
09-05 13:06:21.893: I/IN_BUFFER(10176): We now send to handler.
09-05 13:06:21.893: I/IN_BUFFER(10176): Read Stream into Buffer:
09-05 13:06:21.898: V/BluetoothSocket.cpp(10176): readNative
09-05 13:06:21.903: I/MESSAGE_READ(10176): I am reading 2 bytes
09-05 13:06:21.903: I/Content(10176): The entire array:
09-05 13:06:21.903: I/some hardcoded tag(10176): 48 
09-05 13:06:21.903: I/some hardcoded tag(10176): 48 
09-05 13:06:21.958: I/MESSAGE_READ(10176): I am reading 17 bytes
09-05 13:06:21.958: I/Content(10176): The entire array:
09-05 13:06:21.958: I/some hardcoded tag(10176): 48 
09-05 13:06:21.958: I/some hardcoded tag(10176): 48 
09-05 13:06:21.958: I/some hardcoded tag(10176): 48 
09-05 13:06:21.958: I/some hardcoded tag(10176): 48 
09-05 13:06:21.958: I/some hardcoded tag(10176): 48 
09-05 13:06:21.958: I/some hardcoded tag(10176): 44 
09-05 13:06:21.958: I/some hardcoded tag(10176): 48 
09-05 13:06:21.958: I/some hardcoded tag(10176): 48 
09-05 13:06:21.958: I/some hardcoded tag(10176): 49 
09-05 13:06:21.958: I/some hardcoded tag(10176): 51 
09-05 13:06:21.958: I/some hardcoded tag(10176): 48 
09-05 13:06:21.958: I/some hardcoded tag(10176): 44 
09-05 13:06:21.958: I/some hardcoded tag(10176): 85 
09-05 13:06:21.958: I/some hardcoded tag(10176): 13 
09-05 13:06:21.958: I/some hardcoded tag(10176): -57 
09-05 13:06:21.963: I/some hardcoded tag(10176): 3 
09-05 13:06:21.963: I/some hardcoded tag(10176): 6 

Espero que isso não tenha confundido o assunto, mas na verdade demonstra o problema que muitas pessoas parecem estar tendo com o código de demonstração BluetoothChat, quando adicionado para uso próprio. De alguma forma, precisamos evitar que o buffer seja sobrescrito até que o manipulador de mensagens o leia? Saudações

Adrian Wreyford

Código atualizado funcionando melhor por causa do sono!

public void run() {
            Log.i(TAG, "BEGIN  IN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                      try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    bytes = mmInStream.available();
                    Log.i("IN_BUFFER", "mmInStream-available bytes: " + Integer.toString(bytes)+ " ");
                    if (bytes>0){ 
                    for(int i=0; i<30; i++){ 
                       buffer[i] = 0;}
                    // Read from the InputStream
                    Log.i("IN_BUFFER", "Read Stream into Buffer:");
                    bytes = mmInStream.read(buffer);

                    Log.i("IN_BUFFER", "The entire buffer after read stream into buffer: " + Integer.toString(bytes)+ " "); 
                    for(int i=0; i<30; i++) 
                         Log.i("IN_BUF_AFTER", buffer[i] + " ");
                    // Send the obtained bytes to the UI Activity
                    Log.i("IN_BUFFER", "We now send to handler.");
                    mHandler.obtainMessage(BluetoothScale.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();}
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    // Start the service over to restart listening mode
                    BluetoothScaleService.this.start();
                    break;
                }
            }
        }

Agora os logs são os seguintes:

09-05 20:57:15.833: V/BluetoothSocket.cpp(25368): availableNative
09-05 20:57:15.838: I/IN_BUFFER(25368): mmInStream-available bytes: 0 
09-05 20:57:15.888: V/BluetoothSocket.cpp(25368): availableNative
09-05 20:57:15.888: I/IN_BUFFER(25368): mmInStream-available bytes: 0 
09-05 20:57:15.943: V/BluetoothSocket.cpp(25368): availableNative
09-05 20:57:15.943: I/IN_BUFFER(25368): mmInStream-available bytes: 0 
09-05 20:57:15.958: V/BluetoothSocket.cpp(25368): writeNative
09-05 20:57:15.988: V/BluetoothSocket.cpp(25368): availableNative
09-05 20:57:15.993: I/IN_BUFFER(25368): mmInStream-available bytes: 2 
09-05 20:57:15.993: I/IN_BUFFER(25368): Read Stream into Buffer:
09-05 20:57:15.993: V/BluetoothSocket.cpp(25368): readNative
09-05 20:57:15.998: I/IN_BUFFER(25368): The entire buffer after read stream into buffer: 19 
09-05 20:57:15.998: I/IN_BUF_AFTER(25368): 2 
09-05 20:57:15.998: I/IN_BUF_AFTER(25368): -58 
09-05 20:57:16.003: I/IN_BUF_AFTER(25368): 48 
...truncated to save space ... 
09-05 20:57:16.033: I/IN_BUF_AFTER(25368): 85 
09-05 20:57:16.033: I/IN_BUF_AFTER(25368): 13 
09-05 20:57:16.033: I/IN_BUF_AFTER(25368): -57 
09-05 20:57:16.033: I/IN_BUF_AFTER(25368): 3 
09-05 20:57:16.038: I/IN_BUF_AFTER(25368): 6 
09-05 20:57:16.038: I/IN_BUF_AFTER(25368): 0 
...truncated to save space ... 
09-05 20:57:16.043: I/IN_BUF_AFTER(25368): 0 
09-05 20:57:16.043: I/IN_BUFFER(25368): We now send to handler.
09-05 20:57:16.058: I/MESSAGE_READ(25368): I am reading 19 bytes
09-05 20:57:16.058: I/Content(25368): The entire array:
09-05 20:57:16.058: I/some hardcoded tag(25368): 2 
09-05 20:57:16.058: I/some hardcoded tag(25368): -58 
09-05 20:57:16.058: I/some hardcoded tag(25368): 48 
...truncated to save space ...
09-05 20:57:16.063: I/some hardcoded tag(25368): 13 
09-05 20:57:16.063: I/some hardcoded tag(25368): -57 
09-05 20:57:16.063: I/some hardcoded tag(25368): 3 
09-05 20:57:16.063: I/some hardcoded tag(25368): 6 
09-05 20:57:16.093: V/BluetoothSocket.cpp(25368): availableNative
09-05 20:57:16.093: I/IN_BUFFER(25368): mmInStream-available bytes: 0

Note que o mmInStream.available () retorna 2 bytes, então na próxima linha de código quando lemos o buffer, 19 bytes são lidos .. realmente estranho, como ele é preenchido entre esses dois passos supostamente imediatos. O sono parece permitir tempo suficiente para o manipulador ler a mensagem do buffer passado, antes que o buffer seja reescrito.

Eu teria esperado o handler.obtainmessage ... para enviar um buffer exclusivo, mas parece enviar a referência para o buffer de thread, assim, o aborrecimento. Como posso enviar um buffer exclusivo a cada vez? Thx Adrian

questionAnswers(5)

yourAnswerToTheQuestion