我正在使用来自 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();
我是一名优秀的程序员,十分优秀!