Sending and Receiving Data with Android and Arduino -


i trying write android application can send , receive serial data , arduino board connected on-the-go (otg) adapter. using usb-serial-for-android library. setting arduino if receives string "t" send string "t" back. android set same, although if string variable datatosend isn't empty send value of along "t". want these 2 send data , forth. arduino side of project working perfectly, whenever receives data records sd card, , if detects "t" send "t" tell android can send again.

here code, sorry length feel there multiple places problem:

public class mainactivity extends activity {  private boolean cansend; private boolean notinit;  private string datatosend;  private usbmanager manager; private serialinputoutputmanager serialiomanager;  private static usbserialdriver senddriver;  private static usbserialdriver recdriver;  private textview outputtext;  private executorservice mexecutor = executors.newsinglethreadexecutor();  private serialinputoutputmanager.listener mlistener = new    serialinputoutputmanager.listener() {      @override     public void onrunerror(exception e) {   }      @override     public void onnewdata(final byte[] data) {          mainactivity.this.runonuithread(new runnable() {                     @override                     public void run() {                         try {                  mainactivity.this.updatereceiveddata(data);              } catch (ioexception e) { e.printstacktrace(); }                     }                  });      } };   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      datatosend = "";      notinit = true;      cansend = true;      outputtext = (textview) findviewbyid(r.id.textview1);  }  @override     protected void onpause() {     super.onpause();     stopiomanager();     if (recdriver != null) {         try {             recdriver.close();         } catch (ioexception e) {             // ignore.         }         recdriver = null;     }     finish(); }  @override protected void onresume() {     super.onresume();          manager = (usbmanager) getsystemservice(context.usb_service);         recdriver = usbserialprober.acquire(manager);       if (recdriver == null) {          } else {               try {                   recdriver.open();                   recdriver.setbaudrate(9600);              } catch (ioexception e) {                   try {                   recdriver.close();               } catch (ioexception e2) {                     // ignore.                 }               recdriver = null;               return;             }             recdriver.getclass().getsimplename());         }         ondevicestatechange();     }   private void stopiomanager() {      // usbmanager android.     manager = (usbmanager) getsystemservice(context.usb_service);      // find first available driver.     recdriver = usbserialprober.acquire(manager);          if (serialiomanager != null) {             log.i("device", "stopping io manager ..");             serialiomanager.stop();             serialiomanager = null;         } else {              log.d("device", "recdriver null");          }     }   private void startiomanager() {         // usbmanager android.         manager = (usbmanager) getsystemservice(context.usb_service);          // find first available driver.         recdriver = usbserialprober.acquire(manager);          if (recdriver != null) {             log.i("device", "starting io manager ..");             serialiomanager = new serialinputoutputmanager(recdriver, mlistener);             mexecutor.submit(serialiomanager);         } else {              log.d("device", "recdriver null");          }     }   private void ondevicestatechange() {         stopiomanager();         startiomanager();     }  protected void updatereceiveddata(byte[] data) throws ioexception {     //use data       log.d("device", "is updating");        final string datain = hexdump.dumphexstring(data);       outputtext.settext(datain);        cansend = checkforsend(datain);       if (cansend) {           senddata();       } }  private boolean checkforsend (string in) {      string cur;      int len = in.length();       (int = 0; < len; i++) {          cur = in.substring(i, + 1);           if (cur.equals("t")) {              return true;          }       }     return false;  }  static void show(context context, usbserialdriver driver) {     recdriver = driver;     final intent intent = new intent(context, mainactivity.class);     intent.addflags(intent.flag_activity_single_top | intent.flag_activity_no_history);     context.startactivity(intent); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  public void requestsend(view v) {      datatosend = "9375644603\n";       if (notinit) {          try {             senddata();          } catch (ioexception e) {              e.printstacktrace();         }      }      notinit = false;   }  private void senddata() throws ioexception {      // usbmanager android.     manager = (usbmanager) getsystemservice(context.usb_service);      // find first available driver.     senddriver = usbserialprober.acquire(manager);      if (senddriver != null) {         senddriver.open();         try {             senddriver.setbaudrate(9600);                              datatosend = "2345\n";                if (!datatosend.equals("")) {                  byte [] bytetosend = datatosend.getbytes();                  senddriver.write(bytetosend, 1000);              }              datatosend = "";               byte[] terminator = "t\n".getbytes();             senddriver.write(terminator, 1000);          } catch (ioexception e) {             // deal error.         } {             senddriver.close();         }      }      // usbmanager android.     manager = (usbmanager) getsystemservice(context.usb_service);      // find first available driver.     senddriver = usbserialprober.acquire(manager);  }   } 

notice trying use 2 separate drivers same i/o manager. not sure if should negatively affect it. able send data arduino, , arduino's tx led comes on, know being sent. onnewdata method triggered when serialinputoutputmanager listener detects incoming data never being ran. take driver manager's listener not getting initialized correctly or something.

if has better method of talking between android , arduino via two-way usb serial, please let me know is. have had lot of trouble trying work.

i found downloading app arduino uno communicator android, , using arduino uno communicator-client code developer best option. broadcast intents , receive data using intent filter, , communication done on separate thread, didn't have worry crashes. if want implement method locally in code, can @ source code of app here.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -