public void onStop() { super.onStop(); try { serial.close(); } catch (IOException e) { e.printStackTrace(); } }
public void onClickSend(View v) { SpannableStringBuilder sb = (SpannableStringBuilder) editInput.getText(); String str = sb.toString(); try { Log.d("send", str); serial.write(str.getBytes()); } catch (IOException e) { e.printStackTrace(); } }
@Override public void onGetPermission(UsbSerial device) { serial = device; try { serial.open(); thread = new Thread(MainActivity.this); runFlag = true; thread.start(); } catch (IOException e) { e.printStackTrace(); } }
@Override public void run() { final byte[] buffer = new byte[1024]; while (runFlag) { try { int len = serial.read(buffer); Log.d("len", String.valueOf(len)); if (len > 0) { Log.d("read", new String(buffer)); Message msg = handle.obtainMessage(MSG_READ, new String(buffer, 0, len)); handle.sendMessage(msg); } Thread.sleep(10); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }