AOA USB Connection Issue

I worked on creating a USB connection as indicated in the link here and successfully implemented it. Its performance is consistent with the fact that it often turns off. I learned from this link and this link that there is an error in Android, that there is no broadcast event of the USB connection event. I implemented a receiver to get a USB disconnect, which is not so important. I also refer to this link to create a stable USB connection. Ie start data transfer after connecting USB without any loss. All this works great when the application has a single activity or a single screen.

For several screens, this problem is related to the problem, that is, the connection is unstable, and I have several screens in the application in which I can receive data via USB in any activity at any time. So, I have 2 questions that I am looking for answers with some code, if possible

  • How can I establish a stable connection with a device connected via a USB serial port in android on multiple screens.
  • How to get rid of this common disconnect problem in an application on multiple screens.

Any help would be helpful

EDIT:

I add my service, which is responsible for communication with usb and starts the stream for continuous data reception.

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.os.IBinder;
import arya.omnitalk.com.usb.R;
import arya.omnitalk.com.usb.constants.FileReadingWritingConstants;
import arya.omnitalk.com.usb.constants.GeneralConstant;
import arya.omnitalk.com.usb.constants.UsbDataWriterConstants;

import com.hoho.android.usbserial.driver.UsbSerialProber;

public class UsbCommunicationService extends Service{

    public static ArrayList<String> _dataArray = new ArrayList<String>();
    private Thread mCommunicationThread = null;

    private class ReadThread implements Runnable {
        @Override
        public void run() {

            while (mCommunicationThread!= null && !mCommunicationThread.isInterrupted()) {

                // Here I write code to parse data received via USB

                 }          
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); // Creating object of usb manager
        UsbDataWriterConstants.driver = UsbSerialProber.acquire(manager); // Acquiring usb channel

        if (UsbDataWriterConstants.driver != null) {
            try {
                UsbDataWriterConstants.driver.open();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            try {
                ReadThread mReadThread = new ReadThread();
                mCommunicationThread = new Thread(mReadThread);
                mCommunicationThread.start();
            } catch (SecurityException e) {
                DisplayError(R.string.error_security);
                DisplayError(R.string.error_security);
            }
        }

    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        if (mCommunicationThread != null)
            mCommunicationThread.interrupt();
        super.onDestroy();
    }


}
+5
source share
2 answers

, , deconnections, - . , , . , , . , . : http://developer.android.com/guide/components/bound-services.html

, , , USB. "" , , : http://developer.android.com/guide/components/services.html onCreate , CONNECTED, :

    public class HomeActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // ...
            Intent intent = new Intent(this, UsbCommunicationService.class);
            startService(intent);
            // ...
        }
    }

UsbCommunicationService (onStart() ):

    public class UsbCommunicationService extends Service {
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            // TODO : connect to USB...

            // We want this service to continue running until it is explicitly
            // stopped, so return sticky.       
            return START_STICKY;
        }
    }

, "", "" "" , . DISCONNECT, :

     stopSelf(); // Will actually stop AFTER all clients unbind... 

, , anwser/ :)

0

, USB.

    public class UsbAccessoryAttachedWorkaroundActivity extends Activity {

    /**
     * Used only to catch the USB_ACCESSORY_ATTACHED event, has it is not broadcasted to 
     * anything else than the activity with the right accessory_filter (no Service, nor Receiver...)
     * DETACHED event is broadcasted as other events. 
     */

     @Override
     public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             Intent intent = new Intent(this, UsbService.class);
             startService(intent);
             finish(); // <- die ASAP, without displaying anything.
     }
    }

, CONNECT, = > Android , ... HomeActivity, . - , , , , USB ...

0

All Articles