蓝牙串口通信数据接收问题 2C

各位高人,我在做一个关于蓝牙串口通信的项目,现在涉及到接收返回的数据并进行处理。
现在的问题是数据总是接收不全。接收的数据有两种格式,1.以F2或F5开头的三个字节的
数据。2 是以F1 开头的12个字节的数据。以下是我接收的数据

03-12 14:08:03.214: D/BluetoothService(31197): --buffer.length is: 12. InStream.read() buffer is: f12140001000000304000076
03-12 14:08:04.144: D/BluetoothService(31197): --buffer.length is: 1. InStream.read() buffer is: f1
03-12 14:08:04.178: D/BluetoothService(31197): --buffer.length is: 11. InStream.read() buffer is: 2100001000000304000036
03-12 14:08:05.140: D/BluetoothService(31197): --buffer.length is: 1. InStream.read() buffer is: f1
03-12 14:08:05.172: D/BluetoothService(31197): --buffer.length is: 10. InStream.read() buffer is: 21400010000003040000
03-12 14:08:05.173: D/BluetoothService(31197): --buffer.length is: 1. InStream.read() buffer is: 76
03-12 14:08:05.223: D/BluetoothService(31197): --buffer.length is: 1. InStream.read() buffer is: f1
03-12 14:08:05.225: D/BluetoothService(31197): --buffer.length is: 9. InStream.read() buffer is: 214000100000030300
03-12 14:08:05.258: D/BluetoothService(31197): --buffer.length is: 2. InStream.read() buffer is: 0071
03-12 14:08:08.711: D/BluetoothService(31197): --buffer.length is: 12. InStream.read() buffer is: f12100001000000303000031
03-12 14:08:09.664: D/BluetoothService(31197): --buffer.length is: 1. InStream.read() buffer is: f1
03-12 14:08:09.665: D/BluetoothService(31197): --buffer.length is: 9. InStream.read() buffer is: 214000100000030300
03-12 14:08:09.698: D/BluetoothService(31197): --buffer.length is: 2. InStream.read() buffer is: 0071
03-12 14:08:15.959: D/BluetoothService(31197): --buffer.length is: 12. InStream.read() buffer is: f12100001000000303000031
03-12 14:08:17.244: D/BluetoothService(31197): --buffer.length is: 12. InStream.read() buffer is: f12100001000003c0300000e
03-12 14:08:17.681: D/BluetoothService(31197): --buffer.length is: 11. InStream.read() buffer is: f12100001000003c033000
03-12 14:08:17.692: D/BluetoothService(31197): --buffer.length is: 1. InStream.read() buffer is: 3e
03-12 14:08:18.374: D/BluetoothService(31197): --buffer.length is: 1. InStream.read() buffer is: f1
03-12 14:08:18.388: D/BluetoothService(31197): --buffer.length is: 10. InStream.read() buffer is: 2100001000003c043000
03-12 14:08:18.395: D/BluetoothService(31197): --buffer.length is: 1. InStream.read() buffer is: 39
03-12 14:08:20.252: D/BluetoothService(31197): --buffer.length is: 12. InStream.read() buffer is: f12100001000000204000037
03-12 14:08:22.713: D/BluetoothService(31197): --buffer.length is: 12. InStream.read() buffer is: f12100001000000203000030
03-12 14:08:26.756: D/BluetoothService(31197): --buffer.length is: 12. InStream.read() buffer is: f12140001000000203000070
03-12 14:08:30.268: D/BluetoothService(31197): --buffer.length is: 12. InStream.read() buffer is: f12140001000001a03000068
03-12 14:08:31.775: D/BluetoothService(31197): --buffer.length is: 12. InStream.read() buffer is: f10100001000001a03000008
03-12 14:08:32.730: D/BluetoothService(31197): --buffer.length is: 10. InStream.read() buffer is: f12140001000001a0300

部分代码如下:
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[12];
byte[] tempBuffer = new byte[100];
int bytes;
int index = 0;
int sum = 0;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
if (mmInStream.available() > 0) {
if (D)
Log.d(TAG, mmInStream.available()
+ " 1 mminsream---" + index + " " + sum
+ " " +tempBuffer[0]+ (tempBuffer[0] == (byte) 0xF1)+ " (tempBuffer[0]==(byte)0xF5)="
+ (tempBuffer[0] == (byte) 0xF5)
+ " (tempBuffer[0]==(byte)0xF2)="
+ (tempBuffer[0] == (byte) 0xF2));
bytes = mmInStream.read(buffer);

                    byte[] arr = new byte[bytes];

                    if (buffer[0] == (byte) 0xF1
                            || buffer[0] == (byte) 0xF5
                            || buffer[0] == (byte) 0xF2 || sum > 12) {
                        index = 0;
                        sum = 0;
                    }
                    System.arraycopy(buffer, 0, arr, 0, bytes);
                    for (int i = 0; i < bytes; i++) {
                        if (i > 0
                                && (arr[i] == (byte) 0xF1
                                        || arr[i] == (byte) 0xF2 || arr[i] == (byte) 0xF5)) {
                            Log.d(TAG,
                                    "break i ="
                                            + i
                                            + (i > 0 && (arr[i] == (byte) 0xF1
                                                    || arr[i] == (byte) 0xF2 || arr[i] == (byte) 0xF5)));
                            break;
                        }
                        tempBuffer[i + index] = arr[i];
                        sum++;
                    }
                    if (D)
                        Log.d(TAG, mmInStream.available()
                                + "  2  mminsream---" + index + "  " + sum
                                + " (tempBuffer[0]==(byte)0xF1)="
                                + (tempBuffer[0] == (byte) 0xF1)
                                + " (tempBuffer[0]==(byte)0xF5)="
                                + (tempBuffer[0] == (byte) 0xF5)
                                + " (tempBuffer[0]==(byte)0xF2)="
                                + (tempBuffer[0] == (byte) 0xF2) + " sum="
                                + sum);
                    index = index + bytes;
                    if (sum == 12 && tempBuffer[0] == (byte) 0xF1) {

                        // Send the obtained bytes to the UI Activity
                        Bundle bundle = new Bundle();
                        bundle.putInt(BUNDLE_TYPE,
                                MainActivity.MESSAGE_READ);
                        bundle.putByteArray("buffer", tempBuffer);
                        bundle.putInt("length", sum);
                        showMessage(bundle);
                        index = 0;
                        sum = 0;
                    }
                    if (sum == 3
                            && (tempBuffer[0] == (byte) 0xF2 || tempBuffer[0] == (byte) 0xF5)) {

                        // Send the obtained bytes to the UI Activity
                        Bundle bundle = new Bundle();
                        bundle.putInt(BUNDLE_TYPE,
                                MainActivity.MESSAGE_READ);
                        bundle.putByteArray("buffer", tempBuffer);
                        bundle.putInt("length", sum);
                        showMessage(bundle);
                        index = 0;
                        sum = 0;
                    }

还请高人指教。

3个回答

请问楼主解决了吗,我现在在弄蓝牙串口通信的例子,也出现了接收数据被截断的问题。

刚看见,我的博客被封杀了,能评论吗

401585284加我qq,我们可以探讨一下

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问