USB Communication in Flutter: From Android Host API to libusb
Talk to USB serial dongles from Flutter: usb_serial on Android, ACTION_USB_PERMISSION and bulk transfers, libusb on desktop, and why iOS is off-limits.
Published on • November 10, 2026
AI Assistant

Plugging a barcode scanner, GPS receiver, or MCU board into a phone should be as boring as it is on a desktop, but the permission and endpoint details are anything but. This post walks through Android’s USB host model as the plugins wrap it, a working usb_serial flow from enumeration to read/write, the libusb route for desktop, and the hard iOS limitation you need to know before promising cross-platform support.
What Android’s USB host API does underneath
Android 3.1+ can act as a USB host. The classes involved (per Google’s USB host guide) are UsbManager (enumerate devices), UsbDevice, UsbInterface, UsbEndpoint, UsbDeviceConnection (transfers), and UsbRequest for async I/O. Your manifest must declare the feature and, if you want auto-launch on plug-in, an intent filter:
<uses-feature android:name="android.hardware.usb.host" />
<!-- inside your activity -->
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
If you do not use that intent filter, you must request permission explicitly: register a broadcast receiver for a custom ACTION_USB_PERMISSION action, call usbManager.requestPermission(device, permissionIntent), and read UsbManager.EXTRA_PERMISSION_GRANTED from the broadcast before touching the device. Once granted, the flow is claim interface -> transfer -> releaseInterface() / close(), always on a background thread because bulkTransfer() is synchronous.
Enumerate, claim, read and write with usb_serial
usb_serial (current 0.5.2) is an Android plugin built on the well-known felHR85/UsbSerial library. The full async flow:
import 'package:usb_serial/usb_serial.dart';
import 'package:usb_serial/transaction.dart';
final devices = await UsbSerial.listDevices();
if (devices.isEmpty) return;
final port = await devices.first.create();
if (port == null || !await port.open()) return; // permission may have been denied
await port.setDTR(true);
await port.setRTS(true);
await port.setPortParameters(
115200, UsbPort.DATABITS_8, UsbPort.STOPBITS_1, UsbPort.PARITY_NONE,
);
port.inputStream.listen((Uint8List data) => print('rx: $data'));
await port.write(Uint8List.fromList([0x10, 0x00]));
// Frame by terminator instead of raw chunks:
final tx = Transaction.stringTerminated(
port.inputStream, Uint8List.fromList([13, 10]), // CRLF
);
tx.stream.listen((String line) => print('line: $line'));
final reply = await tx.transaction(
port, Uint8List.fromList([65, 66, 13, 10]), const Duration(seconds: 1),
);
Two details worth copying:
- Device filter correctness. The README’s sample
res/xml/device_filter.xmllists FTDI FT232R (1027/24577), CP210x (4292/60000), Prolific PL2303 (1659/8963), and CH340. Note the CH340 line in that sample is written asvendor-id="1A86" product-id="7523"- hex without the0xprefix. The decimal form required by Android is6790/29987, as used by other plugins, so write it that way or the filter will never match. - Chip coverage. The package advertises FTDI and CDC devices, and the filter examples cover the common USB-serial bridges (FTDI, Silicon Labs CP210x, Prolific PL2303, WCH CH340). Always verify your exact VID/PID, and expect the OS permission dialog on first use - the project FAQ links issues about the permission prompt appearing every session.
Other wrappers: flutter_usb_serial and libusb on desktop
flutter_usb_serial (current 0.0.2, published 2022) is a thin demo that combines usb_serial on Android with serial_port_win32 on Windows for CH340 dongles. Its homepage points at a LAN address and it has nine likes, so treat it as sample code rather than a dependency.
For desktop, the standard route is libusb through dart:ffi:
- libusb - Dart FFI bindings for libusb, Linux/macOS/Windows (last release is a few years old).
- dart_libusb - a fork with updated dependencies, same three desktop platforms.
- flusbserial - a cross-platform USB serial plugin for Windows, Linux, and macOS desktop apps.
- quick_usb - Android/Windows/macOS/Linux, also a few years old.
The classic libusb sequence looks like this in C terms, which is exactly what the bindings expose:
// libusb via dart:ffi (pseudo-flow)
// libusb_init(ctx) -> libusb_get_device_list() -> match VID/PID
// -> libusb_open(dev) -> libusb_claim_interface(handle, 0)
// -> libusb_bulk_transfer(ep, buf, len, &transferred, timeout)
// -> libusb_release_interface(handle, 0) -> libusb_close() -> libusb_exit(ctx)
On Windows you also need a driver (WinUSB/libusbK via Zadig) for devices that ship a vendor CDC driver.
iOS: no public USB host API
There is no iOS equivalent of UsbManager for third-party apps. Apple’s public surface for accessories is the External Accessory framework and its EASession, which only talks to MFi-certified accessories implementing the External Accessory protocol. A generic CDC/FTDI/CH340 dongle plugged into the Lightning or USB-C port will not enumerate for your Flutter app, and no pub.dev plugin can change that. Plan iOS around Bluetooth (BLE) or network transports instead, and say so in your feature matrix.
A lifecycle that survives unplug
Industrial users pull cables mid-transfer, so build for it:
- Listen for
UsbManager.ACTION_USB_DEVICE_DETACHED(or the plugin’s device-lost callback) and tear down: cancel theinputStreamsubscription,port.close(), reset your state machine. - Keep transfers off the UI thread - Android explicitly warns that
bulkTransfer()must run on another thread; in Flutter that means not awaiting a huge write inside a frame callback. - Handle
open()returningfalse(permission denied or interface claimed by another app) and treat an emptylistDevices()as a normal state, not an error. - For request/response protocols, prefer the
Transactionhelpers with a timeout over rawinputStream.listen, so a silent device cannot hang your UI.
Takeaway: Model USB on Android as enumerate -> request ACTION_USB_PERMISSION -> claim interface -> bulk read/write, let usb_serial (or a libusb FFI binding on desktop) do the plumbing, ship a decimal VID/PID device_filter.xml, and mark iOS as unsupported because Apple exposes only the MFi accessory path.
Sources
- https://pub.dev/packages/usb_serial
- https://pub.dev/packages/flutter_usb_serial
- https://pub.dev/packages/libusb
- https://pub.dev/packages/dart_libusb
- https://pub.dev/packages/flusbserial
- https://developer.android.com/guide/topics/connectivity/usb/host
- https://developer.apple.com/documentation/externalaccessory