         captureSession.stopRunning()
     }
     
-    func failed() {
+    func failed(error: QRReaderError) {
         print("Scanning unsupported")
-        captureSession = nil
+        guard let readingCommand = self.readingCommand
+            , let callbackId = readingCommand.callbackId
+            , let commandDelegate = self.commandDelegate else {
+            return
+        }
+
+        let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: error.rawValue)
+        commandDelegate.send(pluginResult, callbackId:callbackId)
     }
-    
-    // override func viewWillAppear(_ animated: Bool) {
-    //     super.viewWillAppear(animated)
-    
-    //     if (captureSession?.isRunning == false) {
-    //         captureSession.startRunning()
-    //     }
-    // }
-    
-    // override func viewWillDisappear(_ animated: Bool) {
-    //     super.viewWillDisappear(animated)
-    
-    //     if (captureSession?.isRunning == true) {
-    //         captureSession.stopRunning()
-    //     }
-    // }
     
     func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
         captureSession.stopRunning()

            return
        }
        
        if (captureSession.canAddInput(videoInput)) {
            captureSession.addInput(videoInput)
        } else {
            failed()
            return
        }
        
        let metadataOutput = AVCaptureMetadataOutput()
        
        if (captureSession.canAddOutput(metadataOutput)) {
            captureSession.addOutput(metadataOutput)
            
            metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            metadataOutput.metadataObjectTypes = [.qr]
        } else {
            failed()
            return
        }
        
        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.frame = self.layer.bounds
        previewLayer.videoGravity = .resizeAspectFill
        self.layer.addSublayer(previewLayer)
        
        captureSession.startRunning()


import UIKit
import AVKit

enum QRReaderError {
  case NO_PERMISSION
  case SCANNING_UNSUPPORTED
}

class QRReader: CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
    
