Enhance USB functionality by adding request port label management and platform support checks

This commit is contained in:
just_stuff_tm
2026-03-02 05:28:31 -05:00
committed by just-stuff-tm
parent ca5784f3f8
commit 781090243c
7 changed files with 93 additions and 34 deletions
+14 -4
View File
@@ -26,6 +26,7 @@ class UsbSerialService {
JSObject? _writer;
String? _connectedPortName;
String? _connectedPortKey;
String _requestPortLabel = 'Choose USB Device';
UsbSerialStatus get status => _status;
String? get activePortName => _connectedPortName;
@@ -49,7 +50,7 @@ class UsbSerialService {
final ports = await _getAuthorizedPorts();
if (ports.isEmpty) {
return const <String>[usbRequestPortLabel];
return <String>[_requestPortLabel];
}
return ports.map(_displayLabelForPort).toList(growable: false);
}
@@ -159,6 +160,14 @@ class UsbSerialService {
_connectedPortName = _buildDisplayLabel(portKey);
}
void setRequestPortLabel(String label) {
final trimmed = label.trim();
if (trimmed.isEmpty) {
return;
}
_requestPortLabel = trimmed;
}
void dispose() {
unawaited(disconnect().whenComplete(_closeFrameController));
}
@@ -189,7 +198,7 @@ class UsbSerialService {
if (ports.isEmpty) {
return null;
}
if (requestedPortName.isEmpty || requestedPortName == usbRequestPortLabel) {
if (requestedPortName.isEmpty || requestedPortName == _requestPortLabel) {
return ports.first;
}
for (final port in ports) {
@@ -350,7 +359,7 @@ class UsbSerialService {
try {
final info = port.callMethod<JSAny?>('getInfo'.toJS);
if (info == null) {
return usbRequestPortLabel;
return _requestPortLabel;
}
final infoObject = info as JSObject;
@@ -366,10 +375,11 @@ class UsbSerialService {
return describeWebUsbPort(
vendorId: hasVendor ? vendorId.toInt() : null,
productId: hasProduct ? productId.toInt() : null,
requestPortLabel: _requestPortLabel,
knownUsbNames: _knownUsbNames,
);
} catch (_) {
return usbRequestPortLabel;
return _requestPortLabel;
}
}