gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:22 24 4
gpt4 key购买 nike

我正在使用来自 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 - 通过蓝牙在安卓上发送长短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6637145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com