Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过蓝牙在android上发送长文本消息_Android_Bluetooth - Fatal编程技术网

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

通过蓝牙在android上发送长文本消息,android,bluetooth,Android,Bluetooth,我正在使用android的示例应用程序:BluetoothChat。 但当我尝试发送大于1024字节的字符串时,消息不会传输。 我试图更改下面的代码以发送超过1024字节的数据,但我没有成功。 请帮帮我 阅读代码: public void run() { Log.i(TAG, "BEGIN mConnectedThread"); byte[] buffer = new byte[1024]; int bytes;

我正在使用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);

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


您收到的错误消息是什么?我没有收到错误消息,它只发送1024字节的字符串(它剪切了字符串),即使我更改了缓冲区的大小。发送字符串的代码在哪里?您如何调用write?确保传递给写入的缓冲区不限于1024。谢谢,我明天会检查这个。它工作了,但现在我有一个新问题。数组的最大大小:新字节[1024];我的内存泄漏了
mmOutStream.write(buffer);
mmOutStream.flush();