我使用一些蓝牙聊天示例代码发送SMALL(177字节到3617字节)"设置文件" " securly"应用之间。 当它低于1024位时一切正常:(所以177工作完美) 发送设备按下"发送按钮"并且reciver获取它(如果他们想要它,则使用对话框...)(并将"字符串"保存到该设备上的"设置"文件)
但如果文件超过1024,则会出现chunkt / cut off ..(例如:2000byte) 所以文件被破坏(数据丢失但有些信息仍然存在..)
可能我需要"分裂"我的文件是1024位并发送位,在接收端,我需要"将它们全部加起来" ..
但我不知道"标准最佳做法"为此,你有什么建议吗?
我已经尝试过只有更高的" 1024字节到65536byte,但不起作用.. (或maby我做错了..)
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(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothChatService.this.start();
break;
}
}
}
sedan写道:
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
mmOutStream.flush();
// Share the sent message back to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
当我"点击发送设置":
String message = view.getText().toString();
String settingInAString = getSettingInALargeString();
sendMessage(settingInAString);
和" sendMessage":
if (message.length() > 0) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = message.getBytes();
mChatService.write(send); //SO convert to byte and then send the byte..
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
mOutEditText.setText(mOutStringBuffer);
}
和
/**
* Write to the ConnectedThread in an unsynchronized manner
* @param out The bytes to write
* @see ConnectedThread#write(byte[])
*/
public void write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
if (mState != STATE_CONNECTED) return;
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
}
但我想你知道我在寻找什么...... (或者我可以更改一下" BluetoothChat"所以它可以发送和重放一个大的Sring,而不是" byte:s"?:-))
最重要的是对你们所有人: - )
编辑: 在读者方面我有这个:
读者端&#34>我有: ....
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
//only a byte redebuffer, hmm can I change this? or do i use a whileloop?
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
recivedStringCheckFirst(readMessage);
//simple-check if the data is a "data-setting-file"
String [] allSettingsInALargeArray1 = doSplitOnLargeString(readMessage);
int titleArrayLength1 = getLengthOffTheUpCommingTitleArrayFromNew(allSettingsInALargeArray1); //this do a split and looks if it is 1,2,3..20 settings..)
mConversationArrayAdapter.add(titleArrayLength1 + " datasettings recived from " + mConnectedDeviceName + " SAVE THIS?");
//this type this text to the "chatwindow"
break;
现在是分裂块问题。 如果我在~1024下发送,我会收到正确数量的设置,我可以保存这个罚款: - )
如果我发送的数字大于1024,我会获得第一个例子,而#34; 6个设置来自..."然后是我收到的新消息"来自..."的设置消息: - (
仅供参考:
protected void recivedStringCheckFirst(String readMessage) {
String eventuellSettings = readMessage;
if (isThisASettingFile(eventuellSettings)){
//of ok..
System.out.println("incommingISAsetting :-) ");
inkommenSettings = eventuellSettings;
showDialog(); //dialog for save settings?
}
if (!isThisASettingFile(eventuellSettings)){
//not a settingsfile!
Toast.makeText(this, "try again..", Toast.LENGTH_LONG).show();
}
}
所以我认为是: 案例MESSAGE_READ: 不仅在收到完整文件时调用, 如果收到一小块,也会调用它。 所以我可能应该放置" readFile-chunk"在一个单独的缓冲区 (即mNewBufForFile + = readFileChunk) 然后检查mNewBufForFile中是否有完整的数据包(如何?)。如果完成:我"保存"文件消息然后清除所有缓冲区。
但我怎样才能将其从" Message_read"中分离出来,并且我和#34;添加一个停止位"所以我可以查看我何时收到所有数据?或者我可以做得更好吗?
答案 0 :(得分:1)
您可以根据需要发送任意数量的字节。它们以小于缓冲区大小(1024)的块进入。实际上,原始代码将混合使用一个缓冲区引起的所有问题。变化
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(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
到
while (true) {
try {
byte[] buffer = new byte[1024];
// Read from the InputStream
int nbytes = mmInStream.read(buffer);
Log.i(TAG, "read nbytes: " + nbytes);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, nbytes, -1, buffer)
.sendToTarget();
数据仍然以chuncks形式出现,但现在您将按照严格顺序显示所有数据。
由于chunck的大小 - 在某些测试中 - 小于1024,因此拥有更大的缓冲区是没有意义的。如果要传输真实文件,则应将所有文件连接在一起。这是使用套接字的正常操作。