Bladeren bron

feat: add location permission, add permission related error handling

Raihan Rizal 4 maanden geleden
bovenliggende
commit
2c483f85f6

+ 1 - 1
example/android/settings.gradle

@@ -20,7 +20,7 @@ pluginManagement {
20 20
 plugins {
21 21
     id "dev.flutter.flutter-plugin-loader" version "1.0.0"
22 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 26
 include ":app"

+ 13 - 0
example/lib/main.dart

@@ -7,6 +7,7 @@ import 'dart:async';
7 7
 import 'package:flutter/services.dart';
8 8
 import 'package:flutter_zsdk/flutter_zsdk.dart';
9 9
 import 'package:flutter_zsdk/src/models/flutter_zsdk_exception.dart';
10
+import 'package:flutter_zsdk/src/models/error_code.dart';
10 11
 import 'package:flutter_zsdk_example/models/bluetooth_printer.dart';
11 12
 
12 13
 void main() {
@@ -106,6 +107,18 @@ class _MyAppState extends State<MyApp> {
106 107
       inspect(e);
107 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 122
     } catch (e) {
110 123
       inspect(e);
111 124
       showSnackBar('Unexpected error while discovering bluetooth printers');

+ 52 - 4
example/pubspec.lock

@@ -113,11 +113,27 @@ packages:
113 113
     description: flutter
114 114
     source: sdk
115 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 124
   integration_test:
117 125
     dependency: "direct dev"
118 126
     description: flutter
119 127
     source: sdk
120 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 137
   leak_tracker:
122 138
     dependency: transitive
123 139
     description:
@@ -150,6 +166,30 @@ packages:
150 166
       url: "https://pub.dev"
151 167
     source: hosted
152 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 193
   matcher:
154 194
     dependency: transitive
155 195
     description:
@@ -315,6 +355,14 @@ packages:
315 355
       url: "https://pub.dev"
316 356
     source: hosted
317 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 366
   vector_math:
319 367
     dependency: transitive
320 368
     description:
@@ -335,10 +383,10 @@ packages:
335 383
     dependency: transitive
336 384
     description:
337 385
       name: web
338
-      sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
386
+      sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"
339 387
       url: "https://pub.dev"
340 388
     source: hosted
341
-    version: "0.5.1"
389
+    version: "1.1.1"
342 390
   webdriver:
343 391
     dependency: transitive
344 392
     description:
@@ -348,5 +396,5 @@ packages:
348 396
     source: hosted
349 397
     version: "3.0.3"
350 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,6 +9,8 @@ import 'package:flutter_blue_plus/flutter_blue_plus.dart';
9 9
 import 'package:flutter_zsdk/src/models/bluetooth_printer.dart';
10 10
 import 'package:flutter_zsdk/src/models/error_code.dart';
11 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 14
 import 'package:permission_handler/permission_handler.dart';
13 15
 
14 16
 import 'flutter_zsdk_platform_interface.dart';
@@ -48,12 +50,25 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
48 50
           await FlutterBluePlus.turnOn();
49 51
         } on FlutterBluePlusException catch (e) {
50 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 56
       } else {
53 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 72
     var bluetoothPermissionStatus = await Permission.bluetooth.status;
58 73
     var bluetoothConnectPermissionStatus = await Permission.bluetoothConnect.status;
59 74
     var bluetoothScanPermissionStatus = await Permission.bluetoothScan.status;
@@ -74,6 +89,16 @@ class MethodChannelFlutterZsdk extends FlutterZsdkPlatform {
74 89
 
75 90
       if (permissionStatuses[Permission.bluetooth] == PermissionStatus.granted && permissionStatuses[Permission.bluetoothConnect] == PermissionStatus.granted && permissionStatuses[Permission.bluetoothScan] == PermissionStatus.granted && permissionStatuses[Permission.location] == PermissionStatus.granted) {
76 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 102
       } else {
78 103
         // throw Exception("Permissions not granted, please allow permission to discover bluetooth printers.");
79 104
         throw FlutterZsdkException(ErrorCode.permissionNotGranted, "Permissions not granted, please allow permission to discover bluetooth printers.");

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

@@ -1,9 +1,15 @@
1 1
 class ErrorCode {
2 2
   static const String permissionNotGranted = "PERMISSION_NOT_GRANTED";
3
+  static const String locationServiceDisabled = "LOCATION_SERVICE_DISABLED";
4
+
3 5
   static const String turnOnBluetoothError = "TURN_ON_BLUETOOTH_ERROR";
4 6
   static const String bluetoothIsNotSupported = "BLUETOOTH_IS_NOT_SUPPORTED";
5 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 13
   static const String discoveryError = "DISCOVERY_ERROR";
8 14
   static const String openConnectionError = "OPEN_CONNECTION_ERROR";
9 15
   static const String closeAndReopenConnectionError = "CLOSE_AND_REOPEN_CONNECTION_ERROR";

+ 6 - 3
pubspec.yaml

@@ -1,7 +1,7 @@
1 1
 name: flutter_zsdk
2 2
 description: "A new Flutter plugin project."
3 3
 version: 0.0.1
4
-homepage:
4
+# homepage:
5 5
 
6 6
 environment:
7 7
   sdk: '>=3.3.1 <4.0.0'
@@ -10,9 +10,12 @@ environment:
10 10
 dependencies:
11 11
   flutter:
12 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 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 20
 dev_dependencies:
18 21
   flutter_test: