Skip to content

Commit afbe3b1

Browse files
fix: update Bluetooth flow as suggested by Sourcery AI
1 parent 558d225 commit afbe3b1

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

lib/providers/badge_message_provider.dart

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,48 @@ class BadgeMessageProvider {
111111
if (adapterState != BluetoothAdapterState.on) {
112112
if (Platform.isAndroid) {
113113
ToastUtils().showToast('Please enable Bluetooth...');
114-
await FlutterBluePlus.turnOn();
114+
try {
115+
await FlutterBluePlus.turnOn();
116+
} catch (e) {
117+
ToastUtils().showErrorToast('Failed to enable Bluetooth: $e');
118+
logger.e('Bluetooth turnOn() failed: $e');
119+
return;
120+
}
115121

116-
adapterState = await FlutterBluePlus.adapterState
117-
.where((state) => state == BluetoothAdapterState.on)
118-
.first;
122+
try {
123+
adapterState = await FlutterBluePlus.adapterState
124+
.where((state) => state == BluetoothAdapterState.on)
125+
.first
126+
.timeout(
127+
const Duration(seconds: 10),
128+
onTimeout: () {
129+
ToastUtils().showErrorToast('Bluetooth did not turn on in time.');
130+
throw Exception('Bluetooth enable timeout');
131+
},
132+
);
133+
} catch (e) {
134+
logger.e('Error while waiting for Bluetooth to turn on: $e');
135+
return;
136+
}
119137
} else if (Platform.isIOS) {
120138
ToastUtils().showErrorToast(
121-
'Bluetooth is OFF. Please enable it from Settings.');
122-
123-
adapterState = await FlutterBluePlus.adapterState
124-
.where((state) => state == BluetoothAdapterState.on)
125-
.first;
139+
'Bluetooth is OFF. Please enable it from Settings.',
140+
);
141+
try {
142+
adapterState = await FlutterBluePlus.adapterState
143+
.where((state) => state == BluetoothAdapterState.on)
144+
.first
145+
.timeout(
146+
const Duration(seconds: 10),
147+
onTimeout: () {
148+
ToastUtils().showErrorToast('Bluetooth did not turn on in time.');
149+
throw Exception('Bluetooth enable timeout');
150+
},
151+
);
152+
} catch (e) {
153+
logger.e('Error while waiting for Bluetooth to turn on: $e');
154+
return;
155+
}
126156
} else {
127157
ToastUtils().showErrorToast("Unsupported platform");
128158
return;
@@ -134,9 +164,15 @@ class BadgeMessageProvider {
134164
data = fileHelper.jsonToData(jsonData);
135165
} else {
136166
data = await generateData(
137-
text, flash, marq, isInverted, speedMap[speed], mode, jsonData);
167+
text,
168+
flash,
169+
marq,
170+
isInverted,
171+
speedMap[speed],
172+
mode,
173+
jsonData,
174+
);
138175
}
139-
140176
DataTransferManager manager = DataTransferManager(data);
141177
await transferData(manager);
142178
}

0 commit comments

Comments
 (0)