mik3y / usb-serial-for-android

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

CDC ACM device works occasionally - castrated ACM device error #485

Closed jbstewart closed 1 year ago

jbstewart commented 1 year ago

Thanks for this library - it is a real time-saver!

I have an embedded device that uses a CDC ACM chipset, but I am having trouble connecting to it. In my app I cannot connect to it at all, and only get a message like device might be castrated ACM device, trying single interface logic. And then when I check the number of interfaces, it is 0. If I try to connect anyway I get an exception saying length=0; index=0 at android.hardware.usb.UsbDevice.getInterface. The weird thing is that it has worked before. I am using a CustomProber with the Vendor and Product ids added, and my usb_device_filter.xml file is correct as well, as far as I can tell. I am using your library to successfully connect to a different embedded device from the same embedded dev using a CustomProber.

So I contacted the device developer, and he sent a simple Android app over that also uses your library to connect to the device, and it worked perfectly, using openInterface() instead of openSingleInterface(). I went through trying to make my code exactly like his code, checked the CustomProber and usb_device_filter.xml file to make sure they were identical, but it still would not work in my app - still no interfaces. So then I added some code to his app to spit out the drivers found by the CustomProber, this code in particular:

        List<UsbSerialDriver> drivers = CustomProber.getCustomProber().findAllDrivers(usbManager);
        for (UsbSerialDriver d : drivers) {
            Log.i("ConnectFragment", d.toString());
        }

I added this just after calling

            driver = CustomProber.getCustomProber().probeDevice(device);

But when I ran his app like this, it got the same error my code did. This was literally the only change I made to his app. So of course, like a good software developer I checked to see if that really was what caused it not to work. I commented out the code and his app worked again. I added it back in and it broke, removed it again and added it back a couple times, but then when I tried to remove the code again, it would no longer work. I then went back to a fresh copy of the app the embedded dev sent, rebooted the device, the Android tablet, restarted Android Studio, and no matter what, I could not get it to connect with his app after that. It's almost like it changed something persistent on the device, but I cannot imagine how that could occur. It is a most vexing bug!

I am on version 3.5.1 of the library.

Any suggestions for further debugging would be most welcome!

kai-morich commented 1 year ago

there were changes in 3.5.0 version to automatically detect CDC device, please test with older library and send a dump of USB interfaces, e.g. created with 'USB Device Info' app from Alexandros Schillings

jbstewart commented 1 year ago

I rolled back to 3.4.6 in the simple app that worked for a bit yesterday, but still got the same results. And the app was actually on 2.2.2 before I changed to 3.4.6.

I will attach the USB Device Info results.

But I'm starting to think this is an Android 13 bug. After digging though the library, it sure looked like your code just couldn't find any interfaces from the Android UsbDevice at all. I have another tablet that's on Android 9, and I ran the app on it, with no changes, and it worked fine. I've also added some code to iterate through the devices, their configurations, and print out the number of interfaces on each. When it doesn't work, I have one device, with one configuration, with 0 interfaces. So I don't think this is a problem with your library.

Screenshot_20230329_123822_USB Device Info

kai-morich commented 1 year ago

maybe the power provided by your Android device over USB is not sufficient for stable operation of the CDC device

jbstewart commented 1 year ago

Well, I have an OTG dongle connected to the tablet with a USB-C power connection, and a USB-A connection that the device is connected to. The tablet charges over this connection, so my assumption would be that adequate power is also being supplied to the device.