Hello IOIO users. I am new to IOIO Android development and I have some
issues on a simple app I am trying to develop.
First of all I have managed to connect my IOIO to my phone (Samsung Galaxy
S3) and also run Ytai's samples (and others) without any problems.
In this app I just want to send data through UART and receive it and then
display it on a TextView on real-time after I click a ToggleButton (similar
to HelloIOIO app).
In my setup I have pins 3 and 4 conected to each other to test it first
with only one IOIO board. Here's the thing:
Sometimes it displays the data but other times it crashes. I don't know
what I am doing wrong... maybe "thread issues"? I am kinda new to Java
though.
I am also using the "#" character to detect the end of the sent string and
stop displaying it on the TextView. That's because I don't know how I can
send the string only one time and I keep sending it continuously.
Is there a better way to this? And also to send a string only one time? I
hope you understand what I am saying. Thank you very much in advance.
Here's my code:
public class MainActivity extends IOIOActivity {
private ToggleButton button_;
private TextView tx_;
private TextView rx_;
private TextView rx_decoded_;
private String string_sent = "test string";
private String string_received = "";
private String string_decoded = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button_ = (ToggleButton) findViewById(R.id.button);
tx_ = (TextView) findViewById(R.id.tx);
rx_ = (TextView) findViewById(R.id.rx);
rx_decoded_ = (TextView) findViewById(R.id.rx_decoded);
}
class Looper extends BaseIOIOLooper {
private DigitalOutput led_;
private Uart uart;
private InputStream stream_in = null;
private OutputStream stream_out = null;
private String received_char;
private boolean printed = false;
@Override
protected void setup() throws ConnectionLostException {
led_ = ioio_.openDigitalOutput(0, true);
uart = ioio_.openUart(3, 4, 115200, Uart.Parity.NONE, Uart.StopBits.ONE);
stream_in = uart.getInputStream();
stream_out = uart.getOutputStream();
}
@Override
public void loop() throws ConnectionLostException {
led_.write(!button_.isChecked());
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
if (button_.isChecked()) {
try {
stream_out.write((string_sent+"#").getBytes());
} catch (IOException e) {}
try {
if (stream_in.available() > 0 && printed == false) {
byte received_byte = (byte) stream_in.read();
received_char = Byte.toString(received_byte);
// code for string_decoded
// ...
}
} catch (IOException e) {}
runOnUiThread(new Runnable() {
public void run() {
if (!(received_char.equals("35")) && printed == false) {
tx_.setText("Tx (ON): " + string_enviada);
string_received = string_received + received_char;
rx_.setText("Rx (ON): " + string_received);
// string_decoded = string_decoded + decoded_char;
rx_decoded_.setText("Rx (decoded): " + string_decoded);
} else {
done = true;
}
}
});
}
else {
string_received = "";
string_decoded = "";
runOnUiThread(new Runnable() {
public void run() {
tx_.setText("Tx (OFF): ");
rx_.setText("Rx (OFF): ");
rx_decoded_.setText("Rx (decoded): ");
}
});
}
}
}
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
}
--
You received this message because you are subscribed to the Google Groups
"ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.