蓝牙SPP接收一些包丢帧或丢包?

时间:2012-02-14 02:37:42

标签: android bluetooth

我使用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

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

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

这是我的解决方案,使用Android Chat示例代码。这只是一种可能的解决方案。我正在接收以\n结尾的邮件。我正在使用此线程从蓝牙连接的Arduino中读取消息。在Arduino上,我可以使用\n确保每条消息都以Serial.println( buffer );结束。

我收到的所有内容都附加到现有字符串(queue变量)。当我在这个大字符串上有一整行时,我将其发送到活动并将其从queue变量中删除。

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024];
        int bytes, pos;
        StringBuilder queue = new StringBuilder("");
        String temp;
        // Keep listening to the InputStream while connected
        while (true) {
            try {
                bytes = mmInStream.read(buffer);
                if (bytes > 0) {
                    String readMessage = new String(buffer, 0, bytes);
                    queue.append(readMessage);
                    pos = queue.indexOf("\n");
                    while (pos >= 0) {
                        // Log.d("Temperatura", "indexOf() #" + pos);
                        temp = queue.substring(0, pos - 1);
                        queue.delete(0, pos + 1);
                        mHandler.obtainMessage(Constants.MESSAGE_READ, 0, 0, temp).sendToTarget();
                        pos = queue.indexOf("\n");
                    }
                }
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                // Start the service over to restart listening mode
                BluetoothChatService.this.start();
                break;
            }
        }
    }