Reputation: 73
I work on a Bluetooth application and I receive my data in several String packages. (I use speed Baud 9600)
example:
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
here is a video of what I get screenpresso.com/=8kakb I would like to put all this together in a string.
example:
[11/2/19 9:48:25] Timestamp=1549878505 ID=4 Value=2475
I tried this but it did not work.
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();
}
}
Thank you for taking the time to read.
Upvotes: 0
Views: 109
Reputation: 71
You call toAssemble twice and you don't check for Null Pointer Exception. Here is a more simple approach which could fit your needs. lastStringReceived will have the last String assembly stored till a new String is received.
public class CapteurActivity extends AppCompatActivity {
static String lastStringReceived = "";
StringBuffer buffer = new StringBuffer();
...
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("[")) {
lastStringReceived = buffer.toString();
buffer = new StringBuffer();
}
buffer.append(stringReceived)
}
...
}
}
}
Upvotes: 1