我想恢复每个字符串的值

时间:2019-02-15 14:07:48

标签: java android bluetooth

我每个字节块得到一句话,我想恢复这些字符串的值

我尝试使用正则表达式。

String stringReceived = intent.getStringExtra("data");
                    //check that the data comes from a sensor
                    if (stringReceived.contains("ID") && 
stringReceived.contains("Value")) {
                        //sorts the received data using regex
                        Pattern pattern = 
Pattern.compile(.*Timestamp=(\\d+).*ID=(\\d+).*Value=(\\d+));
                        Matcher matcher = pattern.matcher(stringReceived);

                        while (matcher.find()) {
                            String timestampS = matcher.group(1);
                            String idS = matcher.group(2);
                            String valueS = matcher.group(3);
   }
}

我以这种形式接收数据:

I/RECEIVER: [1/1/0 4
I/RECEIVER: :8:32] T
I/RECEIVER: imestamp
I/RECEIVER: =9466997
I/RECEIVER: 12 ID=4 
I/RECEIVER: Value=24
I/RECEIVER: 43

我想要这样的东西; 时间戳为:548468788 身份证是:5 值我们:545

1 个答案:

答案 0 :(得分:0)

您可以简单地处理每一行,并使用简单的String函数删除不需要的部分,例如substring()甚至replace():

line = line.replace("I/RECEIVER: ", "");

然后根据需要处理结果值。