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