1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
public class MainActivity extends IOIOActivity {
Uart uart;
InputStream in;
OutputStream out;
Button button_;
EditText textout;
TextView textin;
String messageout = "";
String messagein ="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_ = (Button) findViewById(R.id.send);
textout = (EditText) findViewById(R.id.textout);
textin = (TextView) findViewById(R.id.textin);
}
class Looper extends BaseIOIOLooper implements OnClickListener {
@Override
public void setup() throws ConnectionLostException {
uart = ioio_.openUart(39,40,115200,Uart.Parity.NONE,Uart.StopBits.ONE);
in = uart.getInputStream();
out = uart.getOutputStream();
button_.setOnClickListener(this);
}
public void loop() throws ConnectionLostException, InterruptedException{
try {
String rx = Rx();
textin.setText(rx);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread.sleep(10);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==button_){
Tx();
}
}
}
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
private void Tx() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
messageout = textout.getText().toString();
out.write(messageout.getBytes());
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private String Rx() throws IOException{
StringBuilder out = new StringBuilder();
char [] buffer = new char[30];
InputStreamReader reader = new InputStreamReader (in,"UTF-8");
int l;
while((l=reader.read(buffer,0,30)) != -1){
out.append(buffer,0,1);
}
reader.close();
return out.toString();
}
} |
Partager