android – 蓝牙SPP接收一些包框架可以丢失或?

我使用
android示例代码进行修改.只想收到包裹

但是,我的代码只在这里修改

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(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);
            //String readMessage = BytesTrans_fill.bytesToHexString(readBuf);
            Log.d("read", BytesTrans.bytes2HexString(readBuf,msg.arg1));
            String readMessage = BytesTrans.bytes2HexString(readBuf,msg.arg1);
            ppV.setText(ppV.getText().toString() + 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;
        }
    }
};

和BluetoothChatService

    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();
                break;
            }
        }
    }

并添加此功能

package com.example.android.BluetoothChat;

public class BytesTrans {
    public static String byte2HexString(byte b) {
        String ret = "";
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase()  + " ";
        return ret;
    }

    public static String bytes2HexString(byte[] b, int count) {
        String ret = "";
        for (int i = 0; i < count; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase() + " ";
        }
        return ret;
    }
    /*public static String String2byte(String b)
    {
        String[] ttt;
        for (int i = 0; i < b.length(); i++)
        {
            for (int j= i; j<=i+1; j++)
            {
                ttt[i] = b;
            }

        }           

        String ttmp = "";
        String tmp = "";





        ret += tmp;
    }*/
    public static int hexToTen(String b) {
        int D2 = Integer.parseInt(b,16);


        return D2;
        }
    }

但是这个程序有时甚至不能显示我的发送包的框架

我发送这样的包:

aa07210820001202140934390000000000000000000000000000000000000000000297c0fe6b

但有时收到包裹:

aa000297c0fe6b02131452470000000000000000000000000000000000000000000297c0fe6b

如何更改我的代码以接收完整包的框架

最佳答案 您尚未显示任何蓝牙接口代码.但是,如果这主要基于BluetoothChat示例,那么BluetoothChat示例存在一个简单的问题:基本上,当从蓝牙套接字发出read()并放入字节数组时,该数组引用是正如您所做的那样,使用Handler发送到UI.实际的问题是,如果使用BluetoothChat示例以比输入速率更快的速度接收数据,那么您开始看到字符丢失或变得混乱,因为后续的read()在UI仍然覆盖时覆盖了数组读取数组以提取收到的最后一串字符.

因此,如果您的MESSAGE_WRITE对象包含对数组的引用,那么您将调用socket read(),这可能就是您丢失字符的原因.因此,尝试在Message中使用Arrays.copyOf()发送数组的副本.或者,也许你可以使用循环缓冲区安排.

当我使用BluetoothChat示例作为我的项目的基础时,我遇到了这个问题.我亲自解决问题(并消除了复制缓冲区等的需要)是实现一种机制,我将告诉蓝牙连接线程(包含阻塞套接字.read()的线程),方法调用的方法,我期望响应的字节数(幸运的是,我正在处理的协议允许知道响应长度).然后,连接线程仅在收到完整响应时发送消息,而不是向UI发送带有响应片段的多个消息.

点赞