Rohan
Rohan

Reputation: 87

Flutter USB Serial Package always returning false on open() function

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.

Upvotes: 2

Views: 3500

Answers (1)

Fernando Rocha
Fernando Rocha

Reputation: 2589

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

Upvotes: 0

Related Questions