【问题标题】:Android outputStream.write send multiple messagesAndroid outputStream.write 发送多条消息
【发布时间】:2013-12-09 18:34:52
【问题描述】:

有没有办法用OutputStream.write(bytes[]) 发送多条消息,例如,当我调用两次我的函数来编写func.write("hi"); func.write(" how are you"); 时,我收到这样的消息“连接”:"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);
        }
    }
}

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    呼叫:

    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

    • 刷新()

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

    【讨论】:

    • Flush 没有解决我的连接问题。有没有其他解决办法。
    • 如果您正在为接收端编写代码,可能会添加一个消息终止字节,如回车。在代码的接收端查找该字节,您就会知道这些消息之间存在拆分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多