Phonegap-plugin-barcodescanner: Error while scanning sometimes on android

Created on 3 Jan 2016  路  17Comments  路  Source: phonegap/phonegap-plugin-barcodescanner

On android, sometimes, when the code is scanned the scanner doesnt close and if u continue scanning after scan that the scanner closes with {text:"", cancelled: true}

bug android

Most helpful comment

Update: This is the same as issue https://github.com/phonegap/phonegap-plugin-barcodescanner/issues/38?_pjax=%23js-repo-pjax-container#issuecomment-161598604
Change "ng-click" to "on-touch". This also explains why the timeout was somewhat effective.

All 17 comments

It happend also to me in my samsung s3 mini.

The first scan works fine, then the scanner scan the code twice and returns {text:"", cancelled: true}

I made an interesting test:

$scope.scanBarcode = function() {
    alert("With this alert, the scan will work");

    $cordovaBarcodeScanner.scan().then(function(imageData) {
      alert(imageData.text);
      console.log("Barcode Format -> " + imageData.format);
      console.log("Cancelled -> " + imageData.cancelled);
    }, function(error) {
      alert(error);
      console.log("An error happened -> " + error);
    });
  };

It's a bit weird, but the scan works. This doesn't solve the issue, but can help to find what's wrong.

Yeah i think the js code is fine, something probably wrong with the java code here I think this test resultCode == Activity.RESULT_OK is failing and this.callbackContext.error("Unexpected error"); is being called. I have no idea where to go from here. I would be happy if it atleast gives the correct result after second scan. Idk java @kbytesys can u look into it.

That java code doesn't explain why the barcode reader tries to scan twice.

I think there are some bug in the activation of the cordova plugin or in the zxing library installed in some phones, but I'm unable to debug this issue atm :(

Maybe some official dev could try to recreate the bug or just release the plugin with some extra debug log info :smile_cat:

given the amount of activity here that will be a long time

@sparshy please, you can try if my fork submitted in the pr solves your problem?

https://github.com/kbytesys/phonegap-plugin-barcodescanner/tree/android-zxing-integration

@kbytesys No still the same error, is it working for you?

@sparshy yep

did you removed the old plugin and added mine with

cordova plugin add https://github.com/kbytesys/phonegap-plugin-barcodescanner.git#android-zxing-integration

?

Wrapping the plugin call with a setTimeout also works.

@fonini can you elaborate

@kbytesys yes i did install ur plugin

@sparshy I've used the $timeout from Ionic/Angular

$timeout(function(){
    cordova.plugins.barcodeScanner.scan(
      function (result) {
          alert("We got a barcode\n" +
                "Result: " + result.text + "\n" +
                "Format: " + result.format + "\n" +
                "Cancelled: " + result.cancelled);
      }, 
      function (error) {
          alert("Scanning failed: " + error);
      }
   );
});

But I guess you can use a simple setTimeout instead the $timeout.

@fonini Wrapping in a $timeout worked for me. Do you have any idea what the issue is and why this would fix it?

@koga73 Unfortunately, I have no idea why.

I've noticed when it is going to "fail" the barcode scanner almost appears to open twice. Any scanning after and it fails. The timeout works most of the time but I still saw it fail once or twice. I added a 100ms delay to the timeout and it seems to be better now.

Update: This is the same as issue https://github.com/phonegap/phonegap-plugin-barcodescanner/issues/38?_pjax=%23js-repo-pjax-container#issuecomment-161598604
Change "ng-click" to "on-touch". This also explains why the timeout was somewhat effective.

This thread has been automatically locked.

Was this page helpful?
0 / 5 - 0 ratings