posprinter_usb_android
Android USB host and network (TCP) transport for ESC/POS thermal receipt printers.
It lists attached USB printers, performs the Android USB permission handshake,
opens the bulk OUT endpoint and writes raw ESC/POS bytes to it. It can also
connect to a network printer over TCP — usually port 9100 — and scan the
local network for printers.
This package is a maintained fork of
flutter_pos_printer_platform_image_3
1.2.4. See CHANGELOG.md for what changed and
LICENSE for attribution.
Platform support
| Android | iOS | Windows | macOS | Linux | Web | |
|---|---|---|---|---|---|---|
| USB host printing | ✅ | — | — | — | — | — |
| Network (TCP) printing | ✅ | ✅ | ✅ | ✅ | ✅ | — |
USB is driven by the Android plugin. On every other platform the USB API is a
no-op: discovery() yields nothing, connect() returns false and the state
stream is empty.
Network printing uses dart:io sockets, so it works on every platform that has
socket support. Discovery scans the private IPv4 /24 networks the device is
attached to; if you already know the printer address, connect to it directly
and skip discovery.
Requirements
- Android
minSdk21 or newer. android.hardware.usb.hoston the device (declared asrequired="false"by the plugin, so the app still installs on devices without a USB host port).- Flutter 3.44 / Dart 3.12 or newer. This is what lets the module use AGP's built-in Kotlin support instead of applying the Kotlin Gradle Plugin itself.
Usage
import 'package:posprinter_usb_android/posprinter_usb_android.dart';
final PrinterManager manager = PrinterManager.instance;
USB
// 1. List attached printers.
final List<PrinterDevice> printers = await manager
.discovery(type: PrinterType.usb)
.toList();
// 2. Connect. Completes only after the Android permission dialog is answered.
final PrinterDevice printer = printers.first;
final bool connected = await manager.connect(
type: PrinterType.usb,
model: UsbPrinterInput(
name: printer.name,
vendorId: printer.vendorId,
productId: printer.productId,
),
);
// 3. Send raw ESC/POS bytes.
await manager.send(type: PrinterType.usb, bytes: escPosBytes);
// 4. Close.
await manager.disconnect(type: PrinterType.usb);
Network (TCP)
// 1. Scan the local private /24 networks (port 9100). Each printer is emitted
// as soon as it answers; the stream completes when the scan finishes.
await for (final PrinterDevice device
in manager.discovery(type: PrinterType.network)) {
print('${device.name} at ${device.address}');
}
// 2. Connect when you know the address, or use one found above.
final bool connected = await manager.connect(
type: PrinterType.network,
model: TcpPrinterInput(ipAddress: '192.168.1.50'),
);
// 3. Send raw ESC/POS bytes.
await manager.send(type: PrinterType.network, bytes: escPosBytes);
// 4. Close.
await manager.disconnect(type: PrinterType.network);
TcpPrinterInput defaults to port 9100 and a 5-second connection timeout:
TcpPrinterInput(
ipAddress: '192.168.1.50',
port: 9100,
timeout: const Duration(seconds: 3),
);
To scan a single network instead of every private interface, pass a model to
discovery:
manager.discovery(
type: PrinterType.network,
model: TcpPrinterInput(ipAddress: '192.168.1.1'),
);
You can also scan without a PrinterManager:
final found = await TcpPrinterConnector.discoverPrinters(
ipAddress: '192.168.1.1', // optional local address / network to scan
port: 9100,
timeout: TcpPrinterConnector.probeTimeout,
);
The network transport keeps the socket open between connect and
disconnect, so you can print several receipts without reconnecting.
Discovering both transports
discoverPrinters() runs the USB and network modes and returns everything
found:
final List<PrinterDiscovered<dynamic>> found = await discoverPrinters();
Network discovery takes a few seconds because it probes up to 254 hosts per interface. Pass only the modes you need:
final usbOnly = await discoverPrinters(
modes: <DiscoverResult<dynamic> Function()>[
UsbPrinterConnector.discoverPrinters,
],
);
USB state events
PrinterManager.instance.stateUSB reports what the platform is doing:
USBStatus |
Meaning |
|---|---|
none |
No printer connected (also sent on detach). |
connecting |
Android is showing the USB permission dialog. |
connected |
Access was granted. |
attached |
A USB device was just plugged in — a good moment to reconnect. |
manager.stateUSB.listen((USBStatus status) {
if (status == USBStatus.attached) {
// The printer was replugged; try to reconnect it.
}
});
ESC/POS bytes
The plugin only transports bytes. Generate them with
flutter_esc_pos_utils or any
other ESC/POS generator.
Example
example/ contains a small app that scans for USB and network printers,
connects to the one you tap (or to a network address you type) and prints a test
receipt. Run it on a physical Android device:
cd example
flutter run
Credits
Forked from
flutter_pos_printer_platform_image_3
by diantahoc, which
in turn was forked from
flutter_pos_printer_platform
by arthas1888.
License
MIT — see LICENSE. The upstream copyright notice is retained.
Libraries
- discovery
- posprinter_usb_android
- Android USB host and network (TCP) transports for ESC/POS thermal receipt printers.
- printer