Java 将多个字符串组合成一个字符串
我在一个蓝牙应用程序上工作,我在几个字符串包中接收数据。我使用速度波特率9600 例如:Java 将多个字符串组合成一个字符串,java,string,bluetooth,Java,String,Bluetooth,我在一个蓝牙应用程序上工作,我在几个字符串包中接收数据。我使用速度波特率9600 例如: 02-19 09:44:59.516 12659-12659/com.example.appcopeeks I/RECEIVER: [1/1/0 02-19 09:44:59.516 12659-12659/com.example.appcopeeks I/RECEIVER: 0:12:32] 02-19 09:44:59.526 12659-12659/com.example.appcopeeks I/
02-19 09:44:59.516 12659-12659/com.example.appcopeeks I/RECEIVER: [1/1/0
02-19 09:44:59.516 12659-12659/com.example.appcopeeks I/RECEIVER: 0:12:32]
02-19 09:44:59.526 12659-12659/com.example.appcopeeks I/RECEIVER: Timesta
02-19 09:44:59.536 12659-12659/com.example.appcopeeks I/RECEIVER: mp=94668
02-19 09:44:59.546 12659-12659/com.example.appcopeeks I/RECEIVER: 5552 ID=
02-19 09:44:59.556 12659-12659/com.example.appcopeeks I/RECEIVER: 40 Value
02-19 09:44:59.566 12659-12659/com.example.appcopeeks I/RECEIVER: =2453
这是我得到的视频
我想把所有这些放在一起
例如:
[11/2/19 9:48:25] Timestamp=1549878505 ID=4 Value=2475
我试过了,但没用
public class CapteurActivity extends AppCompatActivity {
private StringBuilder dataFull = new StringBuilder();
...
public void onReceive(Context context, Intent intent){
switch (intent.getAction()){
//writes the data received in the EditText
case BGXpressService.BGX_DATA_RECEIVED: {
String stringReceived = intent.getStringExtra("data");
if ( stringReceived != null ) {
if ( stringReceived.startsWith("[")) {
getAssembleData(intent);
}
}
Log.d("Test DataFull: ",dataFull.toString());
...
}
}
}
...
public String getAssembleData(Intent intent){
StringBuilder dataFull = new StringBuilder();
String stringReceived = intent.getStringExtra("data");
while (!stringReceived.contains("[")){
dataFull.append(stringReceived);
}
return dataFull.toString();
}
}
感谢您花时间阅读。您调用toAssemble两次,并且没有检查空指针异常。这里有一个更简单的方法,可以满足您的需要。 lastStringReceived将存储最后一个字符串程序集,直到收到新字符串 公共类CapturActivity扩展了AppCompatActivity{ 静态字符串lastStringReceived=; StringBuffer=新的StringBuffer; ... 接收上下文上下文、意图上的公共无效{ switch intent.getAction{ //将收到的数据写入EditText 案例BGXpressService.BGX_收到的数据:{ String stringReceived=intent.getStringExtradata; 如果接收到字符串!=null{ 如果收到字符串,则使用[{ lastStringReceived=buffer.toString; 缓冲区=新的字符串缓冲区; } 已接收缓冲区.appendstringReceived } ... } } }
尝试将StringBuilder dataFull声明为类成员。这并没有改变我的问题:/嗯,对不起。你知道我尝试过,确实尝试过;但似乎没有取得任何进展。因此,请提供一个实际可复制的示例,这样就有可能用一些有效的东西给你一个直接的答案。thxI有几个字符串,我会我想让他组成一个。我收到的字符串是nome stringReceived。这是我为NPE得到的视频,我可以说很多意图。getStringExtradata;返回null。但是我无法告诉这个方法是做什么的,或者null来自何处。对-但我认为在这种情况下,op应该看看为什么它是null他正在调用Assemble twiceI。我刚刚更正了我的代码。但我仍然没有希望得到的结果。对不起,但现在你的代码似乎有一些问题:它没有循环,所以你最多有一个字符串。所以lastStringreceived一直都是,缓冲区可能有一个字符串,也可能没有。@MaximilianVoss:我用眼睛检查过s、 lastStringReceived=buffer.toString;而buffer在StringBuffer=new StringBuffer;//qed时为空