2

I am trying to use the usb_serial: ^0.4.0 package in my Flutter project for communicating with a USB device on Android. I have followed the installation guide and the package seems to be imported successfully.

However, every time I try to open the USB connection using the open() function provided by the package, it always returns false, even though the device is connected and recognized by the Android device. I have tried debugging the issue by checking the logs, using other USB communication tools and using multiple different types of USB devices but I still can't seem to get it to work and the open() function always seems to return false.

Here is the code I am using:

UsbPort? port = await device.create(UsbSerial.FTDI);
print(await port?.open());

port is being assigned correctly yet this is the output I get in console

D/UsbSerialPortAdapter(18760): success.
I/FTDISerialDevice(18760): Interface succesfully claimed
I/FTDISerialDevice(18760): Control Transfer Response: -1
I/flutter (18760): false

Is there something I am missing or doing wrong? How can I get the USB connection to open successfully? I am guessing without opening correctly I would not be able to receive input from the usb device through the port's inputStream?

Despite this, input from a PS4 controller is being detected as system input by the app. Yet the inputStream is not receiving any input.

Rohan
  • 57
  • 3
  • 10
  • can you please include the code for more clarity? When you try to call bulkTransferIn you might be accessing an invalid index of the array – Fernando Rocha Apr 12 '23 at 00:49

1 Answers1

0

One possible reason for this could be a permission issue...You may need to request permission from the user to access the USB device using the permission_handler package in Flutter and add those lines to AndroidManifest.xml

    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
    </intent-filter>

    <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
        android:resource="@xml/accessory_filter" />

more here

Another possible reason might be an issue with the USB device itself, like an incorrect wiring or a faulty device. Can you try testing the USB device on a different platform or with a different device to ensure it is working properly?

PS: If none of those are the case, I would suggest trying another package such as quick_usb, it seems to embed permission requesting and handling.

Regarding the input from the PS4 controller, it is possible that the app is detecting the controller as a system input, but without a successful USB connection, the inputStream provided by the usb_serial package will not be able to receive any input from the device..never done that but that could be the case

Fernando Rocha
  • 2,416
  • 3
  • 15
  • 28
  • 1
    I agree with @Fernando, the most possible reason for this issue is Permission issue. You need to ask usb permissions at runtime after connecting the device. Also, make sure you're fetching the correct usb port. that can be also issue here. if issue doesn't resolve at all then you can test the connection and permission by implementing broadcast for USB_ATTACHED and USB_DETACHED. It will help in identifying the issue. – Akash Kashyap Apr 10 '23 at 09:47
  • Tried using`usb_serial` after adding the mentioned permissions to `AndroidManifest.xml` but still `open()` gives the same output. Further, I used [smart_usb](https://pub.dev/packages/smart_usb) instead of `quick_usb` since quick_usb was not compiling because of a flag error ( i believe it isn't upto date for the recent versions of android ). After granting permission for a device the app crashes. On second launch it detects the device but when i call the `bulkTransferIn` function it gives an error of `PlatformException(error, length=0, index=0) java.lang.ArrayIndexOutOfBoundsException` – Rohan Apr 10 '23 at 16:40
  • @Rohan can you please include the code for more clarity? When you try to call bulkTransferIn you might be accessing an invalid index of the array – Fernando Rocha Apr 10 '23 at 20:15