我使用的是android:BluetoothChat的示例应用程序。 但是,当我尝试发送字符串,它的大小更大的1024字节消息不传输。 我尝试改变下面的代码发送更多然后1024字节,但我没有在这个成功。 请帮帮我。在Android上通过蓝牙发送长文本信息
读码:
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);
}
}
调用write:
String message="blabla";
byte[] send = message.getBytes();
mChatService.write(send);
什么是您得到的错误消息? –
我没有收到错误消息,它只发送1024字节的字符串(它切断了字符串),即使我改变了缓冲区的大小。 – Beno
其中是发送字符串的代码 –