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()) {
}
}
}
@Override
public void onCreate() {
super.onCreate();
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
UsbDataWriterConstants.driver = UsbSerialProber.acquire(manager);
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();
}
}
source
share