UsbConnection class
USB Connection backed by a native platform channel for lifecycle and libusb FFI for bulk I/O.
Platform status in this release: not yet tested on hardware on macOS or
Android (Android also lacks a bundled libusb-1.0.so); fails in testing
on Windows (cause not yet confirmed, see the README's "Known issues");
not possible on iOS, where open throws
UsbUnsupportedOnPlatformException.
Lifecycle flow:
- open checks platform support, requests permission (Android) if
needed, calls
openForFfito get a handle, verifies identity, claims the interface, and makes the handle available for I/O. - writeRaw / read delegate through UsbPlatform — in production
those methods route to a per-connection worker isolate that owns
libusb calls (Task 18). Stall recovery is bounded by
config.usbStallRetries; unplug surfaces UsbDeviceUnpluggedException and closes the connection. - close is idempotent; releases the interface, closes the libusb handle, and notifies native side.
- Inheritance
-
- Object
- Connection
- UsbConnection
Constructors
- UsbConnection(UsbDeviceAddress address, {ConnectionConfig? config})
- UsbConnection.withPlatform(UsbDeviceAddress address, UsbPlatform _platform, {ConnectionConfig? config})
- Test seam — production code should use the default constructor.
Properties
- address → UsbDeviceAddress
-
final
- config ↔ ConnectionConfig
-
getter/setter pairinherited
- connectionDescription → String
-
Human-readable description (e.g. "TCP:192.168.1.10:9100").
no setteroverride
- hashCode → int
-
The hash code for this object.
no setterinherited
- isConnected → bool
-
Whether the connection is currently open.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
bytesAvailable(
) → Future< int> -
libusb bulk endpoints can't be peeked, so this does a short read and
buffers whatever arrives. The base class's request/response loop only
calls read while this returns > 0.
override
-
close(
) → Future< void> -
Close the connection.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
open(
) → Future< void> -
Open the connection to the printer.
override
-
read(
) → Future< Uint8List?> -
Read all available bytes from the connection. Returns null if none.
override
-
sendAndWaitForResponse(
Uint8List data, {int? initialTimeout, int? readTimeout, String? endOfResponseMarker}) → Future< Uint8List> -
Send data and wait for response, optionally checking for an end-of-response marker.
Matches SDK's
ConnectionA.sendAndWaitForResponse. Outer deadline prevents infinite hang when printer trickles data.inherited -
sendAndWaitForValidResponse(
Uint8List data, {int? initialTimeout, int? readTimeout, required ResponseValidator validator}) → Future< Uint8List> -
Send data and wait for a valid response as determined by
validator. Matches SDK'sConnectionA.sendAndWaitForValidResponse. Outer deadline prevents infinite hang when printer trickles data.inherited -
toString(
) → String -
A string representation of this object.
inherited
-
waitForData(
int timeoutMs) → Future< void> -
Wait until data is available or timeout expires.
Polls every 50ms matching SDK's
Sleeper.sleep(50L).inherited -
write(
Uint8List data) → Future< void> -
Write data with chunking matching SDK's ConnectionA behavior.
Splits into
config.maxChunkSizebyte chunks withconfig.interChunkDelayMsdelay.inherited -
writeRaw(
Uint8List data) → Future< void> -
Write raw bytes to the underlying transport (single chunk, no splitting).
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited