forked from fossasia/badgemagic-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect_state.dart
More file actions
40 lines (33 loc) · 1.38 KB
/
connect_state.dart
File metadata and controls
40 lines (33 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import 'package:badgemagic/bademagic_module/bluetooth/base_ble_state.dart';
import 'package:badgemagic/bademagic_module/bluetooth/completed_state.dart';
import 'package:badgemagic/bademagic_module/bluetooth/datagenerator.dart';
import 'package:badgemagic/bademagic_module/bluetooth/write_state.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
class ConnectState extends NormalBleState {
final ScanResult scanResult;
final DataTransferManager manager;
ConnectState({required this.scanResult, required this.manager});
@override
Future<BleState?> processState() async {
BluetoothDevice device = scanResult.device;
manager.connectedDevice = device;
try {
toast.showToast("Connecting to ${device.platformName}...");
logger.d("Attempting to connect to ${device.platformName}...");
await device.connect(autoConnect: false);
await Future.delayed(const Duration(milliseconds: 500));
logger.d("Connected to device: ${device.platformName}");
toast.showToast("Connected to ${device.platformName}");
return WriteState(manager: manager, device: device);
} catch (e) {
logger.e("Connection failed: $e");
return CompletedState(
isSuccess: false,
message: "Failed to connect to device. Please retry.",
mode: manager.mode,
shouldDisconnect: true,
manager: manager,
);
}
}
}