mik3y / usb-serial-for-android

Android USB host serial driver library for CDC, FTDI, Arduino and other devices.
MIT License
4.81k stars 1.58k forks source link

Unable to use USBSerialPort class to open the UsbDeviceConnection connection #510

Closed mfran89 closed 1 year ago

mfran89 commented 1 year ago

My goal is to connect a Ardupilot Pixhawk device that uses a microusb to a usbc on a Samsung tablet. From reading the readme documentation here and on androids (https://developer.android.com/guide/topics/connectivity/usb/host#java) I am confident that I went through the right steps to get permission granted to interface to the USB device. However when I use the port.open(connection) method I get a IOExpection error "port connection issue java.io.IOException: Reset failed: result=-1".

Here are steps I followed to get to this point

  1. Connect USB device to tablet
  2. Get a popup from tablet asking if I want to connect to the device
  3. Through the usbmanager I run the requestpermission() and pass the pending intent method with the FLAG_MUTABLE flag in it. Current version of sdk is 13.
  4. The broadcast receiver catches this request and grants permission (like the documentation on Android explains) and with in that "if logic" I include the code that the README.md explains to use . This includes the custom prober, driver, and finally the port. It is here that my code gets stuck and cannot get past.

I can confirm that the USBDeviceConnection is good because I see the getFileDescriptor() returns a value greater than -1. However after I try to use the port.open() call the fileDescriptor() returns a -1. Also the UsbDevice obj will return a device name such as /dev/bus/usb/001/051 which I assume should be a viable path. Finally availableDrivers.get(0) returns com.hoho.android.usbserial.driver.FtdiSerialDriver@65028f8 which makes me think it found a a driver to use and the port values returned . Which makes me think the obj was not null or unusable .

Also I would like to add I have used QGroundControl another android plugin to interface to this device and it seems to work properly.

Looking forward to hearing back , please let me know if I need to give anymore information .

kai-morich commented 1 year ago

does it work with example app in this repo or https://play.google.com/store/apps/details?id=de.kai_morich.serial_usb_terminal where you can configure custom vid/pid mapping?

mfran89 commented 1 year ago

I did not use the example app yet, but I did use the example code that was recommended to connect to the device. Also I did use the serial usb terminal app and found the USB device on there and set the driver to CDC and I saw stream of data coming through granted it was in an unreadable format.

When I tired to use the FTDI driver I was unable to connect to the device through the serial usb terminal app.

kai-morich commented 1 year ago

last comment says CDC device, first comment says FTDI. Did you specify the wrong driver in your custom prober?

mfran89 commented 1 year ago

Yeah my mistake was using the FTDI driver instead of the CDC one. Once I updated the custom prober to this customTable.addProduct(usbDevice.getVendorId(), usbDevice.getProductId(), CdcAcmSerialDriver.class); I was able to fix the issue of opening the connection. The serial usb terminal app helped alot too, thanks so much!