浏览代码

feat: add location permission, add permission related error handling

Raihan Rizal 4 月之前
父节点
当前提交
2c483f85f6

+ 1 - 1
example/android/settings.gradle

20
 plugins {
20
 plugins {
21
     id "dev.flutter.flutter-plugin-loader" version "1.0.0"
21
     id "dev.flutter.flutter-plugin-loader" version "1.0.0"
22
     id "com.android.application" version "8.1.3" apply false
22
     id "com.android.application" version "8.1.3" apply false
23
-    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
23
+    id "org.jetbrains.kotlin.android" version "1.9.0" apply false
24
 }
24
 }
25
 
25
 
26
 include ":app"
26
 include ":app"

+ 13 - 0
example/lib/main.dart

7
 import 'package:flutter/services.dart';
7
 import 'package:flutter/services.dart';
8
 import 'package:flutter_zsdk/flutter_zsdk.dart';
8
 import 'package:flutter_zsdk/flutter_zsdk.dart';
9
 import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
9
 import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
10
+import 'package:flutter_zsdk/src/models/error_code.dart';
10
 import 'package:flutter_zsdk_example/models/bluetooth_printer.dart';
11
 import 'package:flutter_zsdk_example/models/bluetooth_printer.dart';
11
 
12
 
12
 void main() {
13
 void main() {
106
       inspect(e);
107
       inspect(e);
107
       showSnackBar(e.message);
108
       showSnackBar(e.message);
108
 
109
 
110
+      if (e.code == ErrorCode.locationServiceDisabled) {
111
+        showDialog(
112
+          context: context, 
113
+          builder: (context) => AlertDialog(
114
+            title: Text('Location service disabled'),
115
+            content: Text(
116
+              'This app require location service to scan bluetooth printers, please turn on location service on your device'
117
+            ),
118
+          ),
119
+        );
120
+      }
121
+
109
     } catch (e) {
122
     } catch (e) {
110
       inspect(e);
123
       inspect(e);
111
       showSnackBar('Unexpected error while discovering bluetooth printers');
124
       showSnackBar('Unexpected error while discovering bluetooth printers');

+ 52 - 4
example/pubspec.lock

113
     description: flutter
113
     description: flutter
114
     source: sdk
114
     source: sdk
115
     version: "0.0.0"
115
     version: "0.0.0"
116
+  http_parser:
117
+    dependency: transitive
118
+    description:
119
+      name: http_parser
120
+      sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
121
+      url: "https://pub.dev"
122
+    source: hosted
123
+    version: "4.0.2"
116
   integration_test:
124
   integration_test:
117
     dependency: "direct dev"
125
     dependency: "direct dev"
118
     description: flutter
126
     description: flutter
119
     source: sdk
127
     source: sdk
120
     version: "0.0.0"
128
     version: "0.0.0"
129
+  js:
130
+    dependency: transitive
131
+    description:
132
+      name: js
133
+      sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
134
+      url: "https://pub.dev"
135
+    source: hosted
136
+    version: "0.6.7"
121
   leak_tracker:
137
   leak_tracker:
122
     dependency: transitive
138
     dependency: transitive
123
     description:
139
     description:
150
       url: "https://pub.dev"
166
       url: "https://pub.dev"
151
     source: hosted
167
     source: hosted
152
     version: "3.0.0"
168
     version: "3.0.0"
169
+  location:
170
+    dependency: transitive
171
+    description:
172
+      name: location
173
+      sha256: "6463a242973bf247e3fb1c7722919521b98026978ee3b5177202e103a39c145e"
174
+      url: "https://pub.dev"
175
+    source: hosted
176
+    version: "7.0.0"
177
+  location_platform_interface:
178
+    dependency: transitive
179
+    description:
180
+      name: location_platform_interface
181
+      sha256: "1e535ccc8b4a9612de4e4319871136b45d2b5d1fb0c2a8bf99687242bf7ca5f7"
182
+      url: "https://pub.dev"
183
+    source: hosted
184
+    version: "5.0.0"
185
+  location_web:
186
+    dependency: transitive
187
+    description:
188
+      name: location_web
189
+      sha256: e6435cfd175b0f6e94d6fdc43c104d13cec7e27b21a8cee00bd9516a3d6a4c81
190
+      url: "https://pub.dev"
191
+    source: hosted
192
+    version: "5.0.4"
153
   matcher:
193
   matcher:
154
     dependency: transitive
194
     dependency: transitive
155
     description:
195
     description:
315
       url: "https://pub.dev"
355
       url: "https://pub.dev"
316
     source: hosted
356
     source: hosted
317
     version: "0.7.2"
357
     version: "0.7.2"
358
+  typed_data:
359
+    dependency: transitive
360
+    description:
361
+      name: typed_data
362
+      sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006
363
+      url: "https://pub.dev"
364
+    source: hosted
365
+    version: "1.4.0"
318
   vector_math:
366
   vector_math:
319
     dependency: transitive
367
     dependency: transitive
320
     description:
368
     description:
335
     dependency: transitive
383
     dependency: transitive
336
     description:
384
     description:
337
       name: web
385
       name: web
338
-      sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
386
+      sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"
339
       url: "https://pub.dev"
387
       url: "https://pub.dev"
340
     source: hosted
388
     source: hosted
341
-    version: "0.5.1"
389
+    version: "1.1.1"
342
   webdriver:
390
   webdriver:
343
     dependency: transitive
391
     dependency: transitive
344
     description:
392
     description:
348
     source: hosted
396
     source: hosted
349
     version: "3.0.3"
397
     version: "3.0.3"
350
 sdks:
398
 sdks:
351
-  dart: ">=3.3.1 <4.0.0"
352
-  flutter: ">=3.18.0-18.0.pre.54"
399
+  dart: ">=3.5.0 <4.0.0"
400
+  flutter: ">=3.22.0"

+ 25 - 0
lib/src/flutter_zsdk_method_channel.dart

9
 import 'package:flutter_zsdk/src/models/bluetooth_printer.dart';
9
 import 'package:flutter_zsdk/src/models/bluetooth_printer.dart';
10
 import 'package:flutter_zsdk/src/models/error_code.dart';
10
 import 'package:flutter_zsdk/src/models/error_code.dart';
11
 import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
11
 import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
12
+import 'package:location/location.dart' as loca;
13
+// import 'package:geolocator/geolocator.dart';
12
 import 'package:permission_handler/permission_handler.dart';
14
 import 'package:permission_handler/permission_handler.dart';
13
 
15
 
14
 import 'flutter_zsdk_platform_interface.dart';
16
 import 'flutter_zsdk_platform_interface.dart';
48
           await FlutterBluePlus.turnOn();
50
           await FlutterBluePlus.turnOn();
49
         } on FlutterBluePlusException catch (e) {
51
         } on FlutterBluePlusException catch (e) {
50
           throw FlutterZsdkException(ErrorCode.turnOnBluetoothError, "Failed to turn on bluetooth: ${e.description}");
52
           throw FlutterZsdkException(ErrorCode.turnOnBluetoothError, "Failed to turn on bluetooth: ${e.description}");
53
+        } catch (e) {
54
+          throw FlutterZsdkException(ErrorCode.turnOnBluetoothError, "Failed to turn on bluetooth");
51
         }
55
         }
52
       } else {
56
       } else {
53
         throw FlutterZsdkException(ErrorCode.bluetoothDisabled, "Bluetooth is not turned on on this device.");
57
         throw FlutterZsdkException(ErrorCode.bluetoothDisabled, "Bluetooth is not turned on on this device.");
54
       }
58
       }
55
     }
59
     }
56
 
60
 
61
+    // check location service
62
+    bool serviceEnabled = await loca.Location.instance.serviceEnabled();
63
+
64
+    if (!serviceEnabled) {
65
+      var serviceEnabled = await loca.Location.instance.requestService();
66
+      if (!serviceEnabled) {
67
+        throw FlutterZsdkException(ErrorCode.locationServiceDisabled, "This app require location service to scan bluetooth printers, please turn on location service first");
68
+      }
69
+    }
70
+    
71
+
57
     var bluetoothPermissionStatus = await Permission.bluetooth.status;
72
     var bluetoothPermissionStatus = await Permission.bluetooth.status;
58
     var bluetoothConnectPermissionStatus = await Permission.bluetoothConnect.status;
73
     var bluetoothConnectPermissionStatus = await Permission.bluetoothConnect.status;
59
     var bluetoothScanPermissionStatus = await Permission.bluetoothScan.status;
74
     var bluetoothScanPermissionStatus = await Permission.bluetoothScan.status;
74
 
89
 
75
       if (permissionStatuses[Permission.bluetooth] == PermissionStatus.granted && permissionStatuses[Permission.bluetoothConnect] == PermissionStatus.granted && permissionStatuses[Permission.bluetoothScan] == PermissionStatus.granted && permissionStatuses[Permission.location] == PermissionStatus.granted) {
90
       if (permissionStatuses[Permission.bluetooth] == PermissionStatus.granted && permissionStatuses[Permission.bluetoothConnect] == PermissionStatus.granted && permissionStatuses[Permission.bluetoothScan] == PermissionStatus.granted && permissionStatuses[Permission.location] == PermissionStatus.granted) {
76
         return bluetoothDiscoveryEventChannel.receiveBroadcastStream();
91
         return bluetoothDiscoveryEventChannel.receiveBroadcastStream();
92
+
93
+      } else if (permissionStatuses[Permission.bluetoothConnect] != PermissionStatus.granted && permissionStatuses[Permission.bluetoothScan] != PermissionStatus.granted) {
94
+        throw FlutterZsdkException(ErrorCode.nearbyDeviceAccessPermissionNotGranted, "Nearby device access permission not granted, please allow permission to discover bluetooth printers.");
95
+
96
+      } else if (permissionStatuses[Permission.bluetooth] != PermissionStatus.granted) {
97
+        throw FlutterZsdkException(ErrorCode.bluetoothPermissionDenied, "Bluetooth permission not granted, please allow permission to discover bluetooth printers.");
98
+
99
+      } else if (permissionStatuses[Permission.location] != PermissionStatus.granted) {
100
+        throw FlutterZsdkException(ErrorCode.locationPermissionDenied, "Location permission not granted, please allow permission to discover bluetooth printers.");
101
+
77
       } else {
102
       } else {
78
         // throw Exception("Permissions not granted, please allow permission to discover bluetooth printers.");
103
         // throw Exception("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.");
104
         throw FlutterZsdkException(ErrorCode.permissionNotGranted, "Permissions not granted, please allow permission to discover bluetooth printers.");

+ 6 - 0
lib/src/models/error_code.dart

1
 class ErrorCode {
1
 class ErrorCode {
2
   static const String permissionNotGranted = "PERMISSION_NOT_GRANTED";
2
   static const String permissionNotGranted = "PERMISSION_NOT_GRANTED";
3
+  static const String locationServiceDisabled = "LOCATION_SERVICE_DISABLED";
4
+
3
   static const String turnOnBluetoothError = "TURN_ON_BLUETOOTH_ERROR";
5
   static const String turnOnBluetoothError = "TURN_ON_BLUETOOTH_ERROR";
4
   static const String bluetoothIsNotSupported = "BLUETOOTH_IS_NOT_SUPPORTED";
6
   static const String bluetoothIsNotSupported = "BLUETOOTH_IS_NOT_SUPPORTED";
5
   static const String bluetoothDisabled = "BLUETOOTH_DISABLED";
7
   static const String bluetoothDisabled = "BLUETOOTH_DISABLED";
6
 
8
 
9
+  static const String bluetoothPermissionDenied = "BLUETOOTH_PERMISSION_DENIED";
10
+  static const String locationPermissionDenied = "LOCATION_PERMISSION_DENIED";
11
+  static const String nearbyDeviceAccessPermissionNotGranted = "NEARBY_DEVICE_ACCESS_PERMISSION_NOT_GRANTED";
12
+
7
   static const String discoveryError = "DISCOVERY_ERROR";
13
   static const String discoveryError = "DISCOVERY_ERROR";
8
   static const String openConnectionError = "OPEN_CONNECTION_ERROR";
14
   static const String openConnectionError = "OPEN_CONNECTION_ERROR";
9
   static const String closeAndReopenConnectionError = "CLOSE_AND_REOPEN_CONNECTION_ERROR";
15
   static const String closeAndReopenConnectionError = "CLOSE_AND_REOPEN_CONNECTION_ERROR";

+ 6 - 3
pubspec.yaml

1
 name: flutter_zsdk
1
 name: flutter_zsdk
2
 description: "A new Flutter plugin project."
2
 description: "A new Flutter plugin project."
3
 version: 0.0.1
3
 version: 0.0.1
4
-homepage:
4
+# homepage:
5
 
5
 
6
 environment:
6
 environment:
7
   sdk: '>=3.3.1 <4.0.0'
7
   sdk: '>=3.3.1 <4.0.0'
10
 dependencies:
10
 dependencies:
11
   flutter:
11
   flutter:
12
     sdk: flutter
12
     sdk: flutter
13
-  plugin_platform_interface: ^2.0.2
14
-  permission_handler: ^11.3.1
13
+  plugin_platform_interface: 2.1.8
14
+  permission_handler: 11.3.1
15
   flutter_blue_plus: 1.34.5
15
   flutter_blue_plus: 1.34.5
16
+  location: 7.0.0
17
+  # geolocator: 9.0.2
18
+  # geolocator_android: 4.6.1
16
 
19
 
17
 dev_dependencies:
20
 dev_dependencies:
18
   flutter_test:
21
   flutter_test: