I have been working on making USB connection specified in the link here and successfully implemented it. Its working fine accept it is frequently getting disconnected. I got to know from this link and this link that there is a bug in android OS that there is no broadcast event of USB connection event. I have implemented a receiver for getting USB disconnecting event which is not too much important. Also I refer this link to create stable connection with USB i.e. start data communication after USB connection without any loss. This whole thing is working fine when there is single activity or single screen in application.
For multiple screen this connection thing is having problem i.e. connection is not stable and I have multiple screen in application in which I can receive data via USB in any activity at any time. So I have 2 question which I am seeking answers of with some code if possible
- How can I make stable connection with device attached via serial USB in android over multiple screens
- How to get rid of this frequent disconnection problem in the application over multiple screens
Any help would be greatful
EDIT:
I am adding my service which is responsible for communication with usb and starts a thread for continuous receiving data
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();
}
}