title: How to Continously transfer usb serial data to external device in android?
body: <p>I'm designing an app for usb interfacing, i have a usb cdc device which i want to communicate using my android funbook p280.. i have previously succeeded in making an app for accomplishing the above using buttons to transfer data.. But now, when i try to do the same for continous transfer, my app crashes. I have tried using seperate threads and now async but everything fails.. I am using the cdc library found in 
  <a href="https://github.com/mik3y/usb-serial-for-android" rel="nofollow">https://github.com/mik3y/usb-serial-for-android</a> </p>
  
  <p>My code is below:</p>
  
  <pre><code>package com.example.dds;
  
  import com.hoho.android.usbserial.driver.CdcAcmSerialDriver;
  import com.hoho.android.usbserial.driver.UsbSerialDriver;
  import android.graphics.Typeface;
  import android.hardware.usb.UsbDevice;
  import android.hardware.usb.UsbDeviceConnection;
  import android.hardware.usb.UsbManager;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.Window;
  import android.view.animation.RotateAnimation;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  import android.widget.Toast;
  import android.app.Activity;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  
  import java.io.IOException;
  import java.nio.charset.Charset;
  import java.util.HashMap;
  import java.util.Iterator;
  
  
  public class DDS extends Activity {
  
  
      RelativeLayout astep,fstep,aknob,fknob;
      UsbManager manager;
      ImageView amstep,fqstep,ampknob,freqknob;
      String str= "SET:WAVE:SIN\n";
      Iterator&lt;UsbDevice&gt; deviceIterator;
      UsbDevice dev;
      UsbSerialDriver driver;
      double mPrevAngle=0,mCurrAngle=0;
      TextView freqdisp,ampdisp;
      float x=0,y=0,startAngle=0,stopAngle=250;
      Thread th;
      asyncrun a;
      IntentFilter usbattach,usbdetach;
  
  
      //My Broadcast Receiver to detect usb
      BroadcastReceiver usbbr =new BroadcastReceiver(){
  
          @Override
          public void onReceive(Context context, Intent intent)
          {
              if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED))
              {
                   //Normal Usb Enumeration
  
                   manager = (UsbManager) getSystemService(Context.USB_SERVICE);
                      HashMap&lt;String, UsbDevice&gt; deviceList = manager.getDeviceList();
                      deviceIterator = deviceList.values().iterator();
                      while(deviceIterator.hasNext())
                      {
                          dev = deviceIterator.next();
                      }
                      if(dev.getProductId()==64905)
                      {
                          Toast t=Toast.makeText(getBaseContext(),"Device Connected",1000);
                          t.show();
                          Object params = 0;
                          a.doInBackground(params);   
              }
                      }
              else if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED))
              {
                  dev=null;
                  Toast t=Toast.makeText(getBaseContext(),"Device Not Connected",1000);
                  t.show();
              }
          }
  
      };
  
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) 
      {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
  
            //IntentFilters for attach and detach action
  
              usbattach = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
              usbdetach = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
      }
  
      @Override
      protected void onPause() 
      {
          super.onPause();
  
          this.unregisterReceiver(usbbr);
  
      }
  
      @Override
      protected void onResume() 
      {   
          super.onResume();
  
         //Registering IntentFilters for Usb attach and detach
          this.registerReceiver(usbbr, usbattach);
          this.registerReceiver(usbbr, usbdetach);
       }
  
         //My AsyncTask subclass
          public class asyncrun extends AsyncTask{
  
              @Override
              protected Object doInBackground(Object... params) {
  
                  //Transferring data to usb, These functions below work fine for an OnClick                   
                  //but for Continous transfer like async they crash..
                  try
                  {
                          driver.open();
                          driver.setBaudRate(115200); 
                          byte[] buffer = encodeUTF8(str);
                          byte[] buff = new byte[160];
                          driver.read(buff,100);
                          driver.write(buffer,100);   
                          str = "";
                          while(driver.read(buff,100)&gt; 0)
                          {
                              str = str + decodeUTF8(buff);
                          }
                          ampdisp.setText(""+str);
  
                      }   catch(IOException e)
                  {
                      e.printStackTrace();
                  }
                  return null;
              }
  
  
  
  
  
  }   
  </code></pre>