通过蓝牙在android上发送长短信

Ben*_*eno 5 android bluetooth

我正在使用来自 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;
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

发送代码:

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

打电话写:

    String message="blabla";
byte[] send = message.getBytes();
        mChatService.write(send);
Run Code Online (Sandbox Code Playgroud)

Den*_*ews 2

写入后,您可能需要刷新流以强制发送数据,因为流可能正在缓冲数据并在实际发送数据之前等待更多数据。尝试 ..

mmOutStream.write(buffer);
mmOutStream.flush();
Run Code Online (Sandbox Code Playgroud)