gpt4 book ai didi

android - 蓝牙聊天 - 将完整的传入消息作为字符串传递

转载 作者:行者123 更新时间:2023-11-30 04:48:41 25 4
gpt4 key购买 nike

我正在使用 Android 网站上的蓝牙聊天示例应用程序。我想传递完整的传入消息(消息格式包含在下面的代码中,因为这个网站正在为我解析消息,这也是我在代码中的工作,传递给一个删除标签并提取消息名称和值的函数使用它进行进一步的操作。readMessage 字符串(在 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;
}
}
}

最佳答案

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

关于android - 蓝牙聊天 - 将完整的传入消息作为字符串传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4166355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com