Refactor USB port handling to introduce display labels and improve state management

This commit is contained in:
just_stuff_tm
2026-03-02 15:54:39 -05:00
committed by just-stuff-tm
parent a0feb129e1
commit 5216e00807
5 changed files with 80 additions and 27 deletions
+13 -5
View File
@@ -26,11 +26,14 @@ class UsbSerialService {
StreamSubscription<dynamic>? _androidDataSubscription;
StreamSubscription<FlSerialEventArgs>? _dataSubscription;
UsbSerialStatus _status = UsbSerialStatus.disconnected;
String? _connectedPortName;
String? _connectedPortKey;
String? _connectedPortLabel;
FlSerial? _serial;
UsbSerialStatus get status => _status;
String? get activePortName => _connectedPortName;
String? get activePortKey => _connectedPortKey;
String? get activePortDisplayLabel =>
_connectedPortLabel ?? _connectedPortKey;
Stream<Uint8List> get frameStream => _frameController.stream;
bool get _useAndroidUsbHost =>
!kIsWeb && defaultTargetPlatform == TargetPlatform.android;
@@ -126,7 +129,8 @@ class UsbSerialService {
}
}
_connectedPortName = normalizedPortName;
_connectedPortKey = normalizedPortName;
_connectedPortLabel = normalizedPortName;
if (_useAndroidUsbHost) {
_androidDataSubscription = _androidEventChannel
.receiveBroadcastStream()
@@ -168,7 +172,8 @@ class UsbSerialService {
if (_status == UsbSerialStatus.disconnected) return;
_status = UsbSerialStatus.disconnecting;
_connectedPortName = null;
_connectedPortKey = null;
_connectedPortLabel = null;
await _androidDataSubscription?.cancel();
_androidDataSubscription = null;
await _dataSubscription?.cancel();
@@ -204,7 +209,10 @@ class UsbSerialService {
if (trimmed.isEmpty) {
return;
}
_connectedPortName = trimmed;
_connectedPortLabel = buildUsbDisplayLabel(
basePortLabel: _connectedPortKey ?? trimmed,
deviceName: trimmed,
);
}
void dispose() {
+2 -1
View File
@@ -29,7 +29,8 @@ class UsbSerialService {
String _requestPortLabel = 'Choose USB Device';
UsbSerialStatus get status => _status;
String? get activePortName => _connectedPortName;
String? get activePortKey => _connectedPortKey;
String? get activePortDisplayLabel => _connectedPortName ?? _connectedPortKey;
Stream<Uint8List> get frameStream => _frameController.stream;
bool get isConnected => _status == UsbSerialStatus.connected;