【问题标题】:Bluetooth Chat - Pass the complete incoming message as string蓝牙聊天 - 将完整的传入消息作为字符串传递
【发布时间】:2011-05-09 03:46:58
【问题描述】:

我正在使用来自 Android 网站的蓝牙聊天示例应用程序。我想将完整的传入消息(以下代码中包含的消息格式,因为该网站正在为我解析消息,这也是我在代码中的工作)传递给删除标签并提取消息名称和值的函数将其用于进一步的操作。readMessage 字符串(在 switch case Message_Read 中)发送选定的字符并省略一些特殊字符。以下是蓝牙聊天应用程序的代码(来自 Android 网站)。

我无法以我在代码中提到的格式接收完整的消息。它以多行显示,许多字符被删除。有什么建议为什么会这样吗?

// The Handler that gets information back from the BluetoothChatService

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);                 

            //Make sure there is a received message. Extract it's name and value
            //int msgLen = readMessage.length();
            //if( msgLen!= 0)
            // Message format is <MSG><N>shiftDirection<!N><V>1<!V><!MSG> 
            if (readBuf.equals("<MSG><N>.*<!N><V>.*<!V><!MSG>"))  
             extractData(readMessage); 
            else mTitle.setText(R.string.floor_it);

            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;
        }
    }
};

将传入的字节流读取到缓冲区并传递缓冲区对象以进行显示的代码部分。

    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);
                //String Incoming = new String(buffer);
                //Pass "Incoming" instead of "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;
            }
        }
    }

【问题讨论】:

    标签: android string buffer parameter-passing


    【解决方案1】:

    我认为问题在于,case MESSAGE_READ: 不仅在接收到完整的Packet 时被调用,而且在接收到小片段时也会被调用。所以你应该把readMessage放在一个单独的缓冲区(即mNewBuf += readMessage)检查mNewBuf里面有一个完整的数据包。如果是这样,则解析您的消息,然后清除缓冲区。它对我有用。

    【讨论】:

      【解决方案2】:
      if (readBuf.equals("<MSG><N>.*<!N><V>.*<!V><!MSG>"))  
               extractData(readMessage); 
      
      • equals() 不应用正则表达式。如果您想匹配.*,请使用java.util.regex 中的类来帮助您。

      【讨论】:

      • 感谢您的回复。我之前已经注意到了这一点,并使用了 readBuf.matches("...")。但这不是主要问题,因为它仍然无法正常工作。
      【解决方案3】:

      我遇到了同样的问题,但我不是 100% 这将始终有效,但我在 javadoc 上找到了这个:

      read(byte[] b, int off, int len) 
            Reads up to len bytes of data from the input stream into an array of bytes.
      

      如果您知道预期的字节数,那么您可以尝试使用

      bytes = mmInStream.read(buffer, 0, lengthYouWant);
      

      但是,如果您传入的数据长度可变,这将不起作用。

      【讨论】:

        猜你喜欢
        • 2012-06-03
        • 2011-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        相关资源
        最近更新 更多