|
@@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart';
|
7
|
7
|
import 'package:flutter/services.dart';
|
8
|
8
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
9
|
9
|
import 'package:flutter_zsdk/src/models/bluetooth_printer.dart';
|
|
10
|
+import 'package:flutter_zsdk/src/models/error_code.dart';
|
10
|
11
|
import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
|
11
|
12
|
import 'package:permission_handler/permission_handler.dart';
|
12
|
13
|
|
|
@@ -37,7 +38,7 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
|
37
|
38
|
// print(event);
|
38
|
39
|
// });
|
39
|
40
|
if (!await FlutterBluePlus.isSupported) {
|
40
|
|
- throw FlutterZsdkException("Bluetooth is not supported on this device.");
|
|
41
|
+ throw FlutterZsdkException(ErrorCode.bluetoothIsNotSupported, "Bluetooth is not supported on this device.");
|
41
|
42
|
}
|
42
|
43
|
|
43
|
44
|
|
|
@@ -46,10 +47,10 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
|
46
|
47
|
try {
|
47
|
48
|
await FlutterBluePlus.turnOn();
|
48
|
49
|
} on FlutterBluePlusException catch (e) {
|
49
|
|
- throw FlutterZsdkException("Failed to turn on bluetooth: ${e.description}");
|
|
50
|
+ throw FlutterZsdkException(ErrorCode.turnOnBluetoothError, "Failed to turn on bluetooth: ${e.description}");
|
50
|
51
|
}
|
51
|
52
|
} else {
|
52
|
|
- throw FlutterZsdkException("Bluetooth is not turned on on this device.");
|
|
53
|
+ throw FlutterZsdkException(ErrorCode.bluetoothDisabled, "Bluetooth is not turned on on this device.");
|
53
|
54
|
}
|
54
|
55
|
}
|
55
|
56
|
|
|
@@ -75,7 +76,7 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
|
75
|
76
|
return bluetoothDiscoveryEventChannel.receiveBroadcastStream();
|
76
|
77
|
} else {
|
77
|
78
|
// throw Exception("Permissions not granted, please allow permission to discover bluetooth printers.");
|
78
|
|
- throw FlutterZsdkException("Permissions not granted, please allow permission to discover bluetooth printers.");
|
|
79
|
+ throw FlutterZsdkException(ErrorCode.permissionNotGranted, "Permissions not granted, please allow permission to discover bluetooth printers.");
|
79
|
80
|
}
|
80
|
81
|
|
81
|
82
|
} else {
|
|
@@ -91,26 +92,35 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
|
91
|
92
|
|
92
|
93
|
} on PlatformException catch (e) {
|
93
|
94
|
inspect(e);
|
94
|
|
- throw FlutterZsdkException("Failed to open connection to printer: ${e.details}");
|
|
95
|
+ throw FlutterZsdkException(e.code, "Failed to open connection to printer: ${e.details}");
|
95
|
96
|
|
96
|
97
|
} catch (e) {
|
97
|
|
- throw FlutterZsdkException("Failed to open connection to printer: $e");
|
|
98
|
+ throw FlutterZsdkException(ErrorCode.unexpectedError, "Message: $e");
|
98
|
99
|
}
|
99
|
100
|
}
|
100
|
101
|
|
101
|
102
|
@override
|
102
|
103
|
Future<bool> isConnected() async {
|
103
|
|
- bool isConnected = await methodChannel.invokeMethod('isConnected');
|
|
104
|
+ try {
|
|
105
|
+ bool isConnected = await methodChannel.invokeMethod('isConnected');
|
|
106
|
+ return isConnected;
|
|
107
|
+ } on PlatformException catch (e) {
|
|
108
|
+ throw FlutterZsdkException(e.code, "Failed to check connection , message: ${e.details}");
|
|
109
|
+ } catch (e) {
|
|
110
|
+ throw FlutterZsdkException(ErrorCode.unexpectedError, "Message: $e");
|
|
111
|
+ }
|
104
|
112
|
|
105
|
|
- return isConnected;
|
106
|
113
|
}
|
107
|
114
|
|
108
|
115
|
@override
|
109
|
116
|
Future<void> closeConnection() async {
|
110
|
117
|
try {
|
111
|
118
|
await methodChannel.invokeMethod('bluetoothCloseConnection');
|
|
119
|
+ } on PlatformException catch (e) {
|
|
120
|
+ inspect(e);
|
|
121
|
+ throw FlutterZsdkException(e.code, "Failed to close connection, message: ${e.details}");
|
112
|
122
|
} catch (e) {
|
113
|
|
- throw FlutterZsdkException("Failed to close connection to printer: $e");
|
|
123
|
+ throw FlutterZsdkException(ErrorCode.unexpectedError, "Message: $e");
|
114
|
124
|
}
|
115
|
125
|
}
|
116
|
126
|
|
|
@@ -118,8 +128,11 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
|
118
|
128
|
Future<void> printZplOverBluetooth(String zplData) async {
|
119
|
129
|
try {
|
120
|
130
|
await methodChannel.invokeMethod('printZplOverBluetooth', {'zplData': zplData});
|
|
131
|
+ } on PlatformException catch (e) {
|
|
132
|
+ inspect(e);
|
|
133
|
+ throw FlutterZsdkException(e.code, "Failed to print ZPL over Bluetooth, message: ${e.details}");
|
121
|
134
|
} catch (e) {
|
122
|
|
- throw FlutterZsdkException("Failed to print ZPL over Bluetooth: $e");
|
|
135
|
+ throw FlutterZsdkException(ErrorCode.unexpectedError, "Message: $e");
|
123
|
136
|
}
|
124
|
137
|
}
|
125
|
138
|
}
|