Skip to content

Commit 87f59cf

Browse files
o-baggeDennisMoschina
authored andcommitted
Revert "Revert "update device connection exceptions""
1 parent 311461f commit 87f59cf

File tree

5 files changed

+80
-17
lines changed

5 files changed

+80
-17
lines changed

example/lib/main.dart

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22

33
import 'package:example/widgets/button_state_widget.dart';
44
import 'package:example/widgets/fota/firmware_update.dart';
5+
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
56
import 'package:open_earable_flutter/open_earable_flutter.dart';
67
import 'package:example/global_theme.dart';
78
import 'package:flutter/material.dart';
@@ -152,9 +153,30 @@ class MyAppState extends State<MyApp> {
152153
trailing: _buildTrailingWidget(device.id,
153154
Theme.of(context).colorScheme.secondary),
154155
onTap: () async {
155-
Wearable wearable = await _wearableManager
156-
.connectToDevice(device);
157-
provider.setSelectedPeripheral(wearable);
156+
try {
157+
Wearable wearable = await _wearableManager
158+
.connectToDevice(device);
159+
provider.setSelectedPeripheral(wearable);
160+
} catch (e) {
161+
String message = _wearableManager
162+
.deviceErrorMessage(e, device.name);
163+
if (context.mounted) {
164+
showPlatformDialog(
165+
context: context,
166+
builder: (context) => PlatformAlertDialog(
167+
title: PlatformText('Connection Error'),
168+
content: PlatformText(message),
169+
actions: [
170+
PlatformDialogAction(
171+
onPressed: () =>
172+
Navigator.of(context).pop(),
173+
child: PlatformText('OK'),
174+
),
175+
],
176+
),
177+
);
178+
}
179+
}
158180
},
159181
),
160182
if (index != discoveredDevices.length - 1)
@@ -246,7 +268,8 @@ class MyAppState extends State<MyApp> {
246268
if (_connectedDevice is ButtonManager)
247269
GroupedBox(
248270
title: "Button State",
249-
child: ButtonStateWidget(buttonManager: _connectedDevice as ButtonManager),
271+
child: ButtonStateWidget(
272+
buttonManager: _connectedDevice as ButtonManager),
250273
),
251274
if (_connectedDevice is FrequencyPlayer)
252275
GroupedBox(

example/pubspec.lock

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ packages:
4545
dependency: transitive
4646
description:
4747
name: characters
48-
sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803
48+
sha256: faf38497bda5ead2a8c7615f4f7939df04333478bf32e4173fcb06d428b5716b
4949
url: "https://pub.dev"
5050
source: hosted
51-
version: "1.4.0"
51+
version: "1.4.1"
5252
clock:
5353
dependency: transitive
5454
description:
@@ -182,6 +182,14 @@ packages:
182182
url: "https://pub.dev"
183183
source: hosted
184184
version: "6.0.0"
185+
flutter_platform_widgets:
186+
dependency: "direct main"
187+
description:
188+
name: flutter_platform_widgets
189+
sha256: "22a86564cb6cc0b93637c813ca91b0b1f61c2681a31e0f9d77590c1fa9f12020"
190+
url: "https://pub.dev"
191+
source: hosted
192+
version: "9.0.0"
185193
flutter_plugin_android_lifecycle:
186194
dependency: transitive
187195
description:
@@ -292,18 +300,18 @@ packages:
292300
dependency: transitive
293301
description:
294302
name: matcher
295-
sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2
303+
sha256: "12956d0ad8390bbcc63ca2e1469c0619946ccb52809807067a7020d57e647aa6"
296304
url: "https://pub.dev"
297305
source: hosted
298-
version: "0.12.17"
306+
version: "0.12.18"
299307
material_color_utilities:
300308
dependency: transitive
301309
description:
302310
name: material_color_utilities
303-
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
311+
sha256: "9c337007e82b1889149c82ed242ed1cb24a66044e30979c44912381e9be4c48b"
304312
url: "https://pub.dev"
305313
source: hosted
306-
version: "0.11.1"
314+
version: "0.13.0"
307315
mcumgr_flutter:
308316
dependency: "direct main"
309317
description:
@@ -560,10 +568,10 @@ packages:
560568
dependency: transitive
561569
description:
562570
name: test_api
563-
sha256: ab2726c1a94d3176a45960b6234466ec367179b87dd74f1611adb1f3b5fb9d55
571+
sha256: "19a78f63e83d3a61f00826d09bc2f60e191bf3504183c001262be6ac75589fb8"
564572
url: "https://pub.dev"
565573
source: hosted
566-
version: "0.7.7"
574+
version: "0.7.8"
567575
tuple:
568576
dependency: transitive
569577
description:

example/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies:
4242
# Use with the CupertinoIcons class for iOS style icons.
4343
cupertino_icons: ^1.0.2
4444
flutter_colorpicker: ^1.1.0
45+
flutter_platform_widgets: ^9.0.0
4546
meta: ^1.16.0
4647
bloc: ^9.1.0
4748
flutter_svg: ^2.0.17

lib/open_earable_flutter.dart

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:async';
44

55
import 'package:logger/logger.dart';
66
import 'package:meta/meta.dart';
7+
import 'package:open_earable_flutter/src/exceptions/device_exception.dart';
78
import 'package:open_earable_flutter/src/models/devices/cosinuss_one_factory.dart';
89
import 'package:open_earable_flutter/src/models/devices/esense_factory.dart';
910
import 'package:open_earable_flutter/src/models/devices/open_earable_factory.dart';
@@ -193,7 +194,7 @@ class WearableManager {
193194
}) async {
194195
if (_connectedIds.contains(device.id)) {
195196
logger.w('Device ${device.id} is already connected');
196-
throw Exception('Device is already connected');
197+
throw AlreadyConnectedException();
197198
}
198199
_connectingStreamController.add(device);
199200

@@ -226,9 +227,9 @@ class WearableManager {
226227
}
227228
_connectedIds.remove(device.id);
228229
await _bleManager.disconnect(device.id);
229-
throw Exception('Device is currently not supported');
230+
throw UnsupportedDeviceException();
230231
} else {
231-
throw Exception('Failed to connect to device');
232+
throw ConnectionFailedException();
232233
}
233234
}
234235

@@ -252,12 +253,25 @@ class WearableManager {
252253
);
253254
connectedWearables.add(wearable);
254255
} catch (e) {
255-
logger.e('Failed to connect to system device ${device.id}: $e');
256+
logger.e(
257+
'Failed to connect to system device ${device.id}: ${deviceErrorMessage(e, device.name)}',
258+
);
256259
}
257260
}
258261
return connectedWearables;
259262
}
260263

264+
String deviceErrorMessage(dynamic e, String deviceName) {
265+
return switch (e) {
266+
UnsupportedDeviceException _ => 'Device "$deviceName" is not supported.',
267+
AlreadyConnectedException _ =>
268+
'Device "$deviceName" is already connected.',
269+
ConnectionFailedException _ =>
270+
'Failed to connect to device "$deviceName". Please try again.',
271+
_ => e.toString(),
272+
};
273+
}
274+
261275
void addPairingRule(PairingRule rule) {
262276
_pairingManager.addRule(rule);
263277
}
@@ -292,7 +306,9 @@ class WearableManager {
292306
try {
293307
await connectToDevice(discoveredDevice);
294308
} catch (e) {
295-
logger.e('Error auto connecting device ${discoveredDevice.id}: $e');
309+
logger.e(
310+
'Error auto connecting device ${discoveredDevice.id}: ${deviceErrorMessage(e, discoveredDevice.name)}',
311+
);
296312
}
297313
}
298314
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
sealed class DeviceException implements Exception {
2+
const DeviceException();
3+
}
4+
5+
class UnsupportedDeviceException extends DeviceException {
6+
const UnsupportedDeviceException();
7+
}
8+
9+
class AlreadyConnectedException extends DeviceException {
10+
const AlreadyConnectedException();
11+
}
12+
13+
class ConnectionFailedException extends DeviceException {
14+
const ConnectionFailedException();
15+
}

0 commit comments

Comments
 (0)