我正在使用Android的RFCOMM Tutorial。我有它工作,但是当我发送大邮件时我遇到了问题(这是有意的,无法更改)。
问题是我会看到多次传输相同的数据,但是在发送X字节后它会停止,即使这些字节中的N个是重复的。
例如,发送一个12800字节的块,我的接收端看到这个数据(从完整日志中删除 - 数字表示字符串的长度):
阅读:(990)yJv; ZU> h8JCfAEttQEA7V9BSkITgDn5350SdahFILcbwrYAfD7b_Oavhqr:DF
阅读:(2970)HQTZWMFv08G3qHiHn @ 4_f8KWrQX9v] gmV = tvqGFpP:c8yV ^ a0i17:ndDiOS
阅读:(990)ZdXZvCxK5sdLlKDnPrrP; 2nuEItZ9< \ 29e7x7lNFafHU] R @ Ap = lBF \ M?1 \ W_O
阅读:(990)ZdXZvCxK5sdLlKDnPrrP; 2nuEItZ9< \ 29e7x7lNFafHU] R @ Ap = lBF \ M?1 \ W_O
阅读:(990)ZdXZvCxK5sdLlKDnPrrP; 2nuEItZ9< \ 29e7x7lNFafHU] R @ Ap = lBF \ M?1 \ W_O
阅读:(1980)ZdXZvCxK5sdLlKDnPrrP; 2nuEItZ9< \ 29e7x7lNFafHU] R @ Ap = lBF \ M?1 \ W_
阅读:(990)TD?lyEvS; [PMj> xpg5RiAIK8di61QNngkOvpoMhl = 0`I @ 32iUuScR1xiGiZ:
阅读:(990)Ahyxc2PMA?sub8l ^:prr = D5vIaGw73 @ rS0EHTeT; XLcsq9k6li; MVw_IJ4QiSs
阅读:(990)YQe:OWv_Iv_6eL @ Yu> KUnbjmsN9KsidM1Vx6:LSsgZ [sbG3V> 0dp7kkvUhX1 ^ n
阅读:(920)YQe:OWv_Iv_6eL @ Yu> KUnbjmsN9KsidM1Vx6:LSsgZ [sbG3V> 0dp7kkvUhX1 ^ n
如您所见,有3个精确重传,以及一个附加了更多数据的重传。但是,所有长度的总和是12800 - 消息的原始大小。
如果您想知道,目前数据只是一个随机字符串。所以,不,它不会在那里多次获得相同的数据位。我已经通过查看发送方确认了这一点。
现在,我需要数据通过而没有任何损坏,所以我正在检查另一方的哈希。但是,对于重传和丢失的数据,它在大多数情况下都不匹配。删除重新传输只会让我丢失数据。
所以,问题似乎有两方面:
所有相关代码都来自Android的网站(上面的链接)。然而,关键点是:
这个块来自我链接到的教程full code:
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
mTitle.setText(R.string.title_connected_to);
mTitle.append(mConnectedDeviceName);
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
mTitle.setText(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
mTitle.setText(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);
break;
case MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};