1
1
import 'package:badgemagic/bademagic_module/bluetooth/datagenerator.dart' ;
2
2
import 'package:badgemagic/bademagic_module/bluetooth/write_state.dart' ;
3
3
import 'package:flutter_blue_plus/flutter_blue_plus.dart' ;
4
- import 'package:badgemagic/providers/BadgeAliasProvider.dart' ;
5
- import 'package:get_it/get_it.dart' ;
6
4
import 'base_ble_state.dart' ;
7
5
8
6
class ConnectState extends RetryBleState {
9
7
final ScanResult scanResult;
10
8
final DataTransferManager manager;
11
- final String displayName;
12
9
13
- ConnectState ({
14
- required this .manager,
15
- required this .scanResult,
16
- required this .displayName,
17
- });
10
+ ConnectState ({required this .manager, required this .scanResult});
18
11
19
12
@override
20
13
Future <BleState ?> processState () async {
@@ -28,20 +21,12 @@ class ConnectState extends RetryBleState {
28
21
if (connectionState == BluetoothConnectionState .connected) {
29
22
connected = true ;
30
23
31
- String alias = displayName;
32
- final aliasProvider = GetIt .I <BadgeAliasProvider >();
33
- final maybeAlias = aliasProvider.getAlias (displayName);
34
- if (maybeAlias != null && maybeAlias.trim ().isNotEmpty) {
35
- alias = maybeAlias;
36
- }
37
-
38
- logger.d ("Device '$displayName ' connected" );
39
- toast.showToast ('Connected successfully to "$alias ".' );
24
+ logger.d ("Device connected" );
25
+ toast.showToast ('Device connected successfully.' );
40
26
41
27
final writeState =
42
28
WriteState (device: scanResult.device, manager: manager);
43
29
final result = await writeState.process ();
44
-
45
30
try {
46
31
await scanResult.device.disconnect ();
47
32
logger.d ("Device disconnected after transfer" );
@@ -50,17 +35,23 @@ class ConnectState extends RetryBleState {
50
35
} catch (e) {
51
36
logger.e ("Error during disconnect after transfer: $e " );
52
37
}
53
-
54
38
return result;
55
39
} else {
56
40
throw Exception ("Failed to connect to the device" );
57
41
}
58
42
} catch (e) {
59
- toast.showErrorToast ('Failed to connect to "$ displayName ", retrying...' );
43
+ toast.showErrorToast ('Failed to connect retrying...' );
60
44
rethrow ;
61
45
} finally {
62
46
if (! connected) {
63
- await scanResult.device.disconnect ();
47
+ try {
48
+ await scanResult.device.disconnect ();
49
+ logger.d ("Device disconnected in finally block" );
50
+ await Future .delayed (const Duration (seconds: 1 ));
51
+ logger.d ("Waited 1s after disconnect (finally)" );
52
+ } catch (e) {
53
+ logger.e ("Error during disconnect in finally: $e " );
54
+ }
64
55
}
65
56
}
66
57
}
0 commit comments