通过蓝牙在Android上发送长文本消息

我正在使用android:BluetoothChat的示例应用程序.
但是当我尝试发送大小超过1024字节的字符串时,消息不会传输.
我尝试更改下面的代码发送超过1024字节,但我没有成功.
请帮我.

阅读代码:

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(SmallWorld.MESSAGE_READ, bytes, -1,
                            buffer).sendToTarget();

                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }

发送代码:

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

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

打电话给:

    String message="blabla";
byte[] send = message.getBytes();
        mChatService.write(send);

解决方法:

在写入之后,您可能希望刷新流以强制发送数据,因为可能是流正在缓冲数据并在发送数据之前等待更多数据.
试试..

mmOutStream.write(buffer);
mmOutStream.flush();
上一篇:在Android中,MAC地址是否随时间而变化


下一篇:C++函数调用栈使用进阶