@@ -111,18 +111,48 @@ class BadgeMessageProvider {
111
111
if (adapterState != BluetoothAdapterState .on ) {
112
112
if (Platform .isAndroid) {
113
113
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
+ }
115
121
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
+ }
119
137
} else if (Platform .isIOS) {
120
138
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
+ }
126
156
} else {
127
157
ToastUtils ().showErrorToast ("Unsupported platform" );
128
158
return ;
@@ -134,9 +164,15 @@ class BadgeMessageProvider {
134
164
data = fileHelper.jsonToData (jsonData);
135
165
} else {
136
166
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
+ );
138
175
}
139
-
140
176
DataTransferManager manager = DataTransferManager (data);
141
177
await transferData (manager);
142
178
}
0 commit comments