Android 字节数组到字符串直到'\n';
我有Android Arduino蓝牙通信。有时安卓会收到两条信息,而不是来自Arduino的一条。我的意思是:如果我通过蓝牙从Arduino向Android发送例如5字节的消息“1234”,有时我在一条消息中得到“1”1字节,在第二条消息中得到“234”+\n 4字节。有时我会收到完整的“1234”+\n 5字节消息,但不知道原因。我需要按分隔符分割输入消息,若我得到单独的消息,我会崩溃。所以我需要在字符串中添加字节,直到新行char出现Android 字节数组到字符串直到'\n';,android,bluetooth,arduino,Android,Bluetooth,Arduino,我有Android Arduino蓝牙通信。有时安卓会收到两条信息,而不是来自Arduino的一条。我的意思是:如果我通过蓝牙从Arduino向Android发送例如5字节的消息“1234”,有时我在一条消息中得到“1”1字节,在第二条消息中得到“234”+\n 4字节。有时我会收到完整的“1234”+\n 5字节消息,但不知道原因。我需要按分隔符分割输入消息,若我得到单独的消息,我会崩溃。所以我需要在字符串中添加字节,直到新行char出现 如何向字符串添加字符,直到“\n”新行字符出现 数
- 如何向字符串添加字符,直到“\n”新行字符出现
case BLUETOOTH_RECEIVED:
byte[] buffer = (byte[])msg.obj;
int len = msg.arg1;
if (len > 0 && buffer != null) {
onBluetoothRead(buffer, len);
}
break;
}
缓冲区到字符串:
private void onBluetoothRead(byte[] buffer, int len) {
Log.i(LOGGER_TAG, String.format("Received: " + output.replace("\n", "") + " message of " + "%d bytes", len));
String output = new String(buffer, 0, len); // Add read buffer to new string
m_deviceOutput.append(output); // Add (not replace) string to TextView
StringTokenizer splitStr = new StringTokenizer(output, ","); // split string by comma
String numberOne = splitStr.nextToken(); // First split string
String numberTwo = splitStr.nextToken(); // Second split string
numberOne = numberOne.replaceAll("\\D+",""); // replace all chars, leave only numbers
numberTwo = numberTwo.replaceAll("\\D+","");
}
日志:
07-22 14:06:15.099: I/DeviceActivity(20370): Received: 1234 message of 5 bytes
07-22 14:06:20.599: I/DeviceActivity(20370): Received: 1234 message of 5 bytes
07-22 14:06:27.349: I/DeviceActivity(20370): Received: 1 message of 1 bytes
07-22 14:06:27.469: I/DeviceActivity(20370): Received: 234 message of 4 bytes
07-22 14:06:37.219: I/DeviceActivity(20370): Received: 1 message of 1 bytes
07-22 14:06:37.349: I/DeviceActivity(20370): Received: 234 message of 4 bytes
在Arduino中,我可以这样写,我希望这里有类似的东西:
//Get data from RS485:
void READ01(){
while (mySerial.available()){
mySerial.read();
}
mySerial.println("01READ");
momentas1="";
delay(20);
while (mySerial.available()) {
char c = mySerial.read();
if (c == '\n'){
break;
}
momentas1 += c;
}
}
此
void READ01
将字符添加到字符串中,直到新行字符出现。您可以使用类似缓冲区的实现,将获得的字符串添加到另一个字符串中,直到收到“\n”
private String packet = "";
private void onBluetoothRead(byte[] buffer, int len) {
Log.i(LOGGER_TAG, String.format("Received: " + output.replace("\n", "") + " message of " + "%d bytes", len));
String output = new String(buffer, 0, len); // Add read buffer to new string
packet += output;
if (packet.endsWith( "\n" ) {
//do what you need to do
m_deviceOutput.append(output); // Add (not replace) string to TextView
StringTokenizer splitStr = new StringTokenizer(packet, ","); // split string by comma
String numberOne = splitStr.nextToken(); // First split string
String numberTwo = splitStr.nextToken(); // Second split string
numberOne = numberOne.replaceAll("\\D+",""); // replace all chars, leave only numbers
numberTwo = numberTwo.replaceAll("\\D+","");
packet = "";
}
}
我不知道
string.endsWith()
。。。我会尝试,但我相信99.9%的人认为这是一个答案:)你好,我希望它能奏效。但请注意,我已进行了编辑。我写的代码中有一个错误。最初在为Eclipse编写代码时,我更正了它:)谢谢你,Ruraj:)。StartWith()
和。endsWith()
非常有用:)很高兴能帮上忙:)