t03330130 2015-03-12 08:08 采纳率: 25%
浏览 2717
已结题

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

各位高人,我在做一个关于蓝牙串口通信的项目,现在涉及到接收返回的数据并进行处理。
现在的问题是数据总是接收不全。接收的数据有两种格式,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条回答 默认 最新

  • Max_Wang7519 2017-08-30 06:24
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?