@@ -2,19 +2,21 @@ import 'dart:async';
2
2
import 'package:badgemagic/bademagic_module/bluetooth/connect_state.dart' ;
3
3
import 'package:badgemagic/bademagic_module/bluetooth/datagenerator.dart' ;
4
4
import 'package:badgemagic/providers/BadgeScanProvider.dart' ;
5
+ import 'package:badgemagic/providers/BadgeAliasProvider.dart' ;
5
6
import 'package:flutter_blue_plus/flutter_blue_plus.dart' ;
6
-
7
7
import 'base_ble_state.dart' ;
8
8
9
9
class ScanState extends NormalBleState {
10
10
final DataTransferManager manager;
11
11
final BadgeScanMode mode;
12
12
final List <String > allowedNames;
13
+ final BadgeAliasProvider aliasProvider;
13
14
14
15
ScanState ({
15
16
required this .manager,
16
17
required this .mode,
17
18
required this .allowedNames,
19
+ required this .aliasProvider,
18
20
});
19
21
20
22
@override
@@ -24,6 +26,14 @@ class ScanState extends NormalBleState {
24
26
25
27
final Completer <BleState ?> nextStateCompleter = Completer ();
26
28
bool isCompleted = false ;
29
+ bool stopScanCalled = false ;
30
+
31
+ void stopScanSafely () {
32
+ if (! stopScanCalled) {
33
+ stopScanCalled = true ;
34
+ FlutterBluePlus .stopScan ();
35
+ }
36
+ }
27
37
28
38
try {
29
39
subscription = FlutterBluePlus .scanResults.listen (
@@ -42,21 +52,44 @@ class ScanState extends NormalBleState {
42
52
.contains (Guid ("0000fee0-0000-1000-8000-00805f9b34fb" ));
43
53
44
54
final deviceName = result.device.name.trim ().toLowerCase ();
45
- final matchesName = mode == BadgeScanMode .any ||
55
+
56
+ final matchesDirectName = mode == BadgeScanMode .any ||
46
57
normalizedAllowedNames.contains (deviceName);
47
58
48
- return matchesUuid && matchesName;
59
+ final aliasMatch = normalizedAllowedNames.any ((realName) {
60
+ final alias =
61
+ aliasProvider.getAlias (realName)? .trim ().toLowerCase ();
62
+ return alias == deviceName;
63
+ });
64
+
65
+ return matchesUuid && (matchesDirectName || aliasMatch);
49
66
},
50
67
orElse: () => throw Exception ("Matching device not found." ),
51
68
);
52
69
53
70
isCompleted = true ;
54
- FlutterBluePlus .stopScan ();
71
+ stopScanSafely ();
72
+
55
73
toast.showToast ('Device found. Connecting...' );
56
74
75
+ final foundName = foundDevice.device.name.trim ();
76
+ String ? resolvedAlias;
77
+
78
+ for (final real in allowedNames) {
79
+ final alias = aliasProvider.getAlias (real)? .trim ();
80
+ if (alias != null &&
81
+ alias.toLowerCase () == foundName.toLowerCase ()) {
82
+ resolvedAlias = alias;
83
+ break ;
84
+ }
85
+ }
86
+
87
+ final displayName = resolvedAlias ?? foundName;
88
+
57
89
nextStateCompleter.complete (ConnectState (
58
90
scanResult: foundDevice,
59
91
manager: manager,
92
+ displayName: displayName,
60
93
));
61
94
} catch (e) {
62
95
logger.w ("No matching device found in this batch: $e " );
@@ -65,6 +98,7 @@ class ScanState extends NormalBleState {
65
98
onError: (e) async {
66
99
if (! isCompleted) {
67
100
isCompleted = true ;
101
+ stopScanSafely (); // ✅ Guarded again
68
102
logger.e ("Scan error: $e " );
69
103
toast.showErrorToast ('Scan error occurred.' );
70
104
nextStateCompleter.completeError (Exception ("Scan error: $e " ));
@@ -76,23 +110,25 @@ class ScanState extends NormalBleState {
76
110
withServices: [Guid ("0000fee0-0000-1000-8000-00805f9b34fb" )],
77
111
removeIfGone: Duration (seconds: 5 ),
78
112
continuousUpdates: true ,
79
- timeout: const Duration (seconds: 15 ), // Reduced scan timeout
113
+ timeout: const Duration (seconds: 15 ),
80
114
);
81
115
82
116
await Future .delayed (const Duration (seconds: 1 ));
83
117
84
- // If no device is found after the scan timeout, complete with an error.
85
118
if (! isCompleted) {
119
+ isCompleted = true ;
120
+ stopScanSafely ();
86
121
toast.showToast ('Device not found.' );
87
122
nextStateCompleter.completeError (Exception ('Device not found.' ));
88
123
}
89
124
90
125
return await nextStateCompleter.future;
91
126
} catch (e) {
92
127
logger.e ("Exception during scanning: $e " );
93
- throw Exception ("please check the device is turned on and retry." );
128
+ throw Exception ("Please check if the device is turned on and retry." );
94
129
} finally {
95
130
await subscription? .cancel ();
131
+ stopScanSafely ();
96
132
}
97
133
}
98
134
}
0 commit comments