Showing
1 changed file
with
54 additions
and
0 deletions
| @@ -87,6 +87,60 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | @@ -87,6 +87,60 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | ||
| 87 | ); | 87 | ); |
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | + /// Request permission to access the camera. | ||
| 91 | + /// | ||
| 92 | + /// Throws a [MobileScannerException] if the permission is not granted. | ||
| 93 | + Future<void> _requestCameraPermission() async { | ||
| 94 | + final MobileScannerState authorizationState; | ||
| 95 | + | ||
| 96 | + try { | ||
| 97 | + authorizationState = MobileScannerState.fromRawValue( | ||
| 98 | + await methodChannel.invokeMethod<int>('state') ?? 0, | ||
| 99 | + ); | ||
| 100 | + } on PlatformException catch (error) { | ||
| 101 | + // If the permission state is invalid, that is an error. | ||
| 102 | + throw MobileScannerException( | ||
| 103 | + errorCode: MobileScannerErrorCode.genericError, | ||
| 104 | + errorDetails: MobileScannerErrorDetails( | ||
| 105 | + code: error.code, | ||
| 106 | + details: error.details as Object?, | ||
| 107 | + message: error.message, | ||
| 108 | + ), | ||
| 109 | + ); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + switch (authorizationState) { | ||
| 113 | + case MobileScannerState.denied: | ||
| 114 | + throw const MobileScannerException( | ||
| 115 | + errorCode: MobileScannerErrorCode.permissionDenied, | ||
| 116 | + ); | ||
| 117 | + case MobileScannerState.authorized: | ||
| 118 | + return; // Already authorized. | ||
| 119 | + case MobileScannerState.undetermined: | ||
| 120 | + try { | ||
| 121 | + final bool permissionResult = await methodChannel.invokeMethod<bool>('request') ?? false; | ||
| 122 | + | ||
| 123 | + if (permissionResult) { | ||
| 124 | + return; // Authorization was granted. | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + throw const MobileScannerException( | ||
| 128 | + errorCode: MobileScannerErrorCode.permissionDenied, | ||
| 129 | + ); | ||
| 130 | + } on PlatformException catch (error) { | ||
| 131 | + // If the permission state is invalid, that is an error. | ||
| 132 | + throw MobileScannerException( | ||
| 133 | + errorCode: MobileScannerErrorCode.genericError, | ||
| 134 | + errorDetails: MobileScannerErrorDetails( | ||
| 135 | + code: error.code, | ||
| 136 | + details: error.details as Object?, | ||
| 137 | + message: error.message, | ||
| 138 | + ), | ||
| 139 | + ); | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | + } | ||
| 143 | + | ||
| 90 | @override | 144 | @override |
| 91 | Stream<BarcodeCapture?> get barcodesStream { | 145 | Stream<BarcodeCapture?> get barcodesStream { |
| 92 | return eventsStream.where((event) => event['name'] == 'barcode').map((event) => _parseBarcode(event)); | 146 | return eventsStream.where((event) => event['name'] == 'barcode').map((event) => _parseBarcode(event)); |
-
Please register or login to post a comment