1
I’m making an app with bluetooth connection and it captures the messages or data that are sent by the Internet via Bluetooth, I’m sending these messages via Bluetooth, each call sends a data and each data should be shown in your specific Textview, then how do I direct this data to your specific Textview? The exchange of information already happens only that the only data is sent and appears in all Textview does not appear in only one, and I would like it to be separate information for example temperature in one and humidity in another.
/*
* Função que envia a mensagem do celular para o módulo Bluetooth, exibindo no monitor serial.
*/
private void enviar(String str) {
if(connected != Connected.True) {
Toast.makeText(getActivity(), "Não conectado", Toast.LENGTH_SHORT).show();
return;
}
try {
String msg;
byte[] data;
msg = str;
data = (str + newline).getBytes();
SpannableStringBuilder spn = new SpannableStringBuilder(msg+'\n');
spn.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorSendText)), 0, spn.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
receiveText.append(spn);
service.write(data);
} catch (Exception e) {
onSerialIoError(e);
}
}
//Envia a mensagem para o receptor, ou seja, o celular... do arduino para o celular
private void receptor(byte[] data) {
String msg = new String(data);
/*
if(newline.equals(newline_crlf) && msg.length() > 0) {
// não mostra CRLF como ^M se diretamente antes de LF
msg = msg.replace(newline_crlf, newline_lf);
// tratamento especial se CRLF e LF vierem em fragmentos separados
if (pendingNewline && msg.charAt(0) == '\n') {
Editable edt = receiveText.getEditableText();
if (edt != null && edt.length() > 1)
edt.replace(edt.length() - 2, edt.length(), "");
}
pendingNewline = msg.charAt(msg.length() - 1) == '\r';
}*/
humidadeSolo.append(TextUtil.toCaretString(msg, newline.length() != 0));
receiveText.append(TextUtil.toCaretString(msg, newline.length() != 0));
}
This other Snippet of code treats the information received to be displayed in Textview, plus which treatment I should employ to fragment that message into two parts or more and I target it to your respective Textviews, please help me...
static CharSequence toCaretString(CharSequence s, boolean keepNewline) {
return toCaretString(s, keepNewline, s.length());
}
static CharSequence toCaretString(CharSequence s, boolean keepNewline, int length) {
boolean found = false;
for (int pos = 0; pos < length; pos++) {
if (s.charAt(pos) < 32 && (!keepNewline ||s.charAt(pos)!='\n')) {
found = true;
break;
}
}
if(!found)
return s;
SpannableStringBuilder sb = new SpannableStringBuilder();
for(int pos=0; pos<length; pos++)
if (s.charAt(pos) < 32 && (!keepNewline ||s.charAt(pos)!='\n')) {
sb.append('^');
sb.append((char)(s.charAt(pos) + 64));
sb.setSpan(new BackgroundColorSpan(caretBackground), sb.length()-2, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
sb.append(s.charAt(pos));
}
return sb;
}
Ready I’m trying to do this, but my doubt even is as I will separate this String without giving errors?
– Allan Miqueias