答案是检查字符串结尾。
private void onBluetoothRead(byte[] buffer, int len) { String output = new String(buffer, 0, len); // Add read buffer to new string Log.i(LOGGER_TAG, String.format("Received: "+ output + " , " + "%d bytes", len)); outputTemp += output; if (outputTemp.endsWith("\n")){ m_deviceOutput.append(outputTemp); // Add (not replace) string to TextView StringTokenizer splitStr = new StringTokenizer(outputTemp, ","); // split string by comma String numberOne = splitStr.nextToken().replaceAll("\\D+",""); // First split string String numberTwo = splitStr.nextToken().replaceAll("\\D+",""); // Second split string m_deviceOutputPrs.setText(numberOne); m_deviceOutputSpeed.setText(numberTwo); outputTemp = ""; } }
好像你需要更多时间来接收整个消息。您可以将数据附加到同一缓冲区,而不是新缓冲区;或者您可以在检测到新输入数据和读取整个输入数据之间添加一个小延迟。