Android outputStream.write发送多条消息

时间:2013-12-09 18:34:52

标签: android bluetooth

有没有办法用OutputStream.write(bytes[])发送多条消息,例如当我调用两次我的函数来编写func.write("hi"); func.write(" how are you");时,我会收到消息" concateneted"像这样:"hi how are you",但我想要两个不同的消息,有没有办法在我的消息中不使用分隔符来做到这一点,我的意思是当其他设备收到消息时,这是我的代码(它的android蓝牙)样品):

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");
        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;
            }
        }
    }

    /**
     * Write to the connected OutStream.
     * @param buffer  The bytes to write
     */
    public void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);

            // Share the sent message back to the UI Activity
            mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

呼叫:

mmOutStream.flush() 

每个消息部分之后。

public void write(byte[] buffer) {
    try {
        mmOutStream.write(buffer);

        //send what is already in buffer
        mmOutStream.flush();

        // Share the sent message back to the UI Activity
        mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
                .sendToTarget();
    } catch (IOException e) {
        Log.e(TAG, "Exception during write", e);
    }
}

来自docs

  • 齐平()

      

    (刷新此输出流并强制写出任何缓冲的输出字节。刷新的一般合同是调用它表示,如果先前写入的任何字节已被输出流的实现缓冲,例如应立即将字节写入其预期目的地。

  •