@@ -2,11 +2,11 @@ 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:flutter_blue_plus/flutter_blue_plus.dart' ;
5
-
6
5
import 'base_ble_state.dart' ;
7
6
8
7
class ScanState extends NormalBleState {
9
8
final DataTransferManager manager;
9
+
10
10
ScanState ({required this .manager});
11
11
12
12
@override
@@ -17,59 +17,72 @@ class ScanState extends NormalBleState {
17
17
Completer <BleState ?> nextStateCompleter = Completer ();
18
18
bool isCompleted = false ;
19
19
20
- ScanResult ? foundDevice;
21
-
22
20
try {
23
21
subscription = FlutterBluePlus .scanResults.listen (
24
22
(results) async {
25
- if (! isCompleted) {
26
- if (results.isNotEmpty) {
27
- foundDevice = results.firstWhere (
23
+ if (! isCompleted && results.isNotEmpty ) {
24
+ try {
25
+ final foundDevice = results.firstWhere (
28
26
(result) => result.advertisementData.serviceUuids
29
27
.contains (Guid ("0000fee0-0000-1000-8000-00805f9b34fb" )),
28
+ orElse: () => throw Exception ("Matching device not found." ),
30
29
);
31
- if (foundDevice != null ) {
32
- toast.showToast ('Device found. Connecting...' );
33
- isCompleted = true ;
34
- nextStateCompleter.complete (ConnectState (
35
- scanResult: foundDevice! ,
36
- manager: manager,
37
- ));
38
- }
30
+ toast.showToast ('Device found. Connecting...' );
31
+ isCompleted = true ;
32
+ await FlutterBluePlus .stopScan ();
33
+ nextStateCompleter.complete (ConnectState (
34
+ scanResult: foundDevice,
35
+ manager: manager,
36
+ ));
37
+
38
+ toast.showToast ('Device found. Connecting...' );
39
+ isCompleted = true ;
40
+ FlutterBluePlus .stopScan ();
41
+
42
+ nextStateCompleter.complete (ConnectState (
43
+ scanResult: foundDevice,
44
+ manager: manager,
45
+ ));
46
+ } catch (e) {
47
+ //ignore and keep scanning
39
48
}
40
49
}
41
50
},
42
- onError: (e) async {
51
+ onError: (e) {
43
52
if (! isCompleted) {
44
53
isCompleted = true ;
54
+ FlutterBluePlus .stopScan ();
45
55
logger.e ("Scan error: $e " );
46
56
toast.showErrorToast ('Scan error occurred.' );
47
- nextStateCompleter.completeError (e);
57
+ nextStateCompleter
58
+ .completeError (Exception ("Error during scanning: $e " ));
48
59
}
49
60
},
50
61
);
51
62
52
63
await FlutterBluePlus .startScan (
53
64
withServices: [Guid ("0000fee0-0000-1000-8000-00805f9b34fb" )],
54
- removeIfGone: Duration (seconds: 5 ),
65
+ removeIfGone: const Duration (seconds: 5 ),
55
66
continuousUpdates: true ,
56
- timeout: const Duration (seconds: 15 ), // Reduced scan timeout
67
+ timeout: const Duration (seconds: 15 ),
57
68
);
58
69
59
70
await Future .delayed (const Duration (seconds: 1 ));
60
71
61
- // If no device is found after the scan timeout, complete with an error.
62
72
if (! isCompleted) {
73
+ isCompleted = true ;
74
+ FlutterBluePlus .stopScan ();
63
75
toast.showToast ('Device not found.' );
64
76
nextStateCompleter.completeError (Exception ('Device not found.' ));
65
77
}
66
78
67
79
return await nextStateCompleter.future;
68
80
} catch (e) {
69
81
logger.e ("Exception during scanning: $e " );
70
- throw Exception ("please check the device is turned on and retry." );
82
+ throw Exception ("Please check if the device is turned on and retry." );
71
83
} finally {
72
84
await subscription? .cancel ();
85
+ await FlutterBluePlus .stopScan ();
73
86
}
74
87
}
75
88
}
0 commit comments