Search code examples
javascriptioscordovabarcode-scanner

barcodescanner plugin issue in phonegap project


I have tried barcodescanner.js samples for my phonegap project which need Qrcode reader, the sample project provided works fine in xcode. Problem arises when iam trying to develop an independent project.

  • my config.xml has:

<plugin name="com.cordova.barcodeScanner" value="CDVBarcodeScanner" />

  • iam using: phonegap 2.7.0
  • i have included barcodescanner.js and its tag properly.

my code:

function onDeviceReady()
                {
                    // do your thing!
                    navigator.notification.alert("PhoneGap is working");

                    scanButton = document.getElementById("scan-button");
                    resultSpan = document.getElementById("scan-result");

                    scanButton.addEventListener("click", clickScan, false);
                    createButton.addEventListener("click", clickCreate, false);

                }
                  function clickScan() {
                      alert("clickScan");
                    window.plugins.barcodeScanner.scan(scannerSuccess, scannerFailure);
                }


                function scannerSuccess(result) {
                    console.log("scannerSuccess: result: " + result)
                    resultSpan.innerText = "success: " + JSON.stringify(result)
                }

                function scannerFailure(message) {
                    console.log("scannerFailure: message: " + message)
                    resultSpan.innerText = "failure: " + JSON.stringify(message)
                }

it is ok till the alert; "clickscan",

after that nothing happens (what prevents my window.plugins.barcodeScanner.scan(scannerSuccess, scannerFailure);working).

This is how my project looks like--> enter image description here

Iam struggling with this for two days and i checked almost all questions on "barcodescanner" tag in SO, did'nt solved my issue, need your help.. Thanks.


Solution

  • In your config.xml you have:

    <plugin name="com.cordova.barcodeScanner" value="CDVBarcodeScanner" />
    

    But in barcodescanner.js from the zip archive linked in your question it's called like this:

    Cordova.exec(successWrapper, fail, "org.apache.cordova.barcodeScanner", "scan", options);
    

    So try changing the line in your config.xml to

    <plugin name="org.apache.cordova.barcodeScanner" value="CDVBarcodeScanner" />
    

    After more investigation it's been estabilished that the barcodescanner.js from example .zip was written for older version of Phonegap and was incompatible with 2.7. Here's a version I use with 2.7 and 2.9, requires <plugin name="BarcodeScanner" value="CDVBarcodeScanner" /> in config.xml and can be called like this:

    var scanner = cordova.require("cordova/plugin/barcodescanner"); 
    scanner.scan(scannerSuccess, scannerFailure);