mitja.gti
mitja.gti

Reputation: 261

PhoneGap BarcodeScanner plugin not working

I have installed the latest version of Node.js, PhoneGap and PhoneGap developer app for iOS. My system is Windows 8.1 (64 bit). Testing device is iPhone 6 with latest iOS.

I am trying to use BarcodeScanner plugin without success. Can someone please explain what I am doing wrong or is this some sort of a bug?

Here are the steps to replicate the issue:

phonegap create HelloWorld
cd HelloWorld
phonegap serve

Edit config.xml and add the following line

<gap:plugin name="com.phonegap.plugins.barcodescanner" version="2.0.0" />

Edit www/index.html. Add a button with id btn-scan, parahraph with id info and reference barcodescanner.js.

<body>
    <div class="app">
        <h1>PhoneGap</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
        <button id="btn-scan">Scan</button>
        <p id="info"></p>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="barcodescanner.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
</body>

Edit js/index.js file...

...
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
    document.getElementById('btn-scan').addEventListener('click', this.scan, false);
},
...

First failing example

scan: function(){
    console.log('scanning');

    try {
        var scanner = cordova.require("cordova/plugin/BarcodeScanner");
    } catch (e) {
        console.log('Error 1');
        console.log('Name: ' + e.name);
        console.log('Message: ' + e.message);
        console.log('Stack: ' + e.stack);
        return;
    }

    try {
        scanner.scan(function (result) {
            alert("We got a barcode\n" +
            "Result: " + result.text + "\n" +
            "Format: " + result.format + "\n" +
            "Cancelled: " + result.cancelled);

            console.log("Scanner result: \n" +
                 "text: " + result.text + "\n" +
                 "format: " + result.format + "\n" +
                 "cancelled: " + result.cancelled + "\n");
            document.getElementById("info").innerHTML = result.text;
            console.log(result);
        }, function (error) {
            console.log("Scanning failed: ", error);
        });
    } catch (e) {
        console.log('Error 2');
        console.log('Name: ' + e.name);
        console.log('Message: ' + e.message);
        console.log('Stack: ' + e.stack);
        return;
    }

},

Error:

[phonegap] [console.log] Received Event: deviceready
[phonegap] [console.log] scanning
[phonegap] [console.log] Error 1
[phonegap] [console.log] Name: undefined
[phonegap] [console.log] Message: undefined
[phonegap] [console.log] Stack: undefined

Second failing example:

    scan: function(){
    console.log('scanning');

    try {
        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);
          }
       );
    } catch (e) {
            console.log('Name: ' + e.name);
            console.log('Message: ' + e.message);
            console.log('Stack: ' + e.stack);
            return;
    }
},

Error:

[phonegap] [console.log] Received Event: deviceready
[phonegap] [console.log] scanning
[phonegap] [console.log] Name: TypeError
[phonegap] [console.log] Message: undefined is not an object (evaluating 'cordova.plugins.barcodeScanner')
[phonegap] [console.log] Stack: scan@http://x.x.x.x:x/js/index.js:43:28

Upvotes: 1

Views: 1130

Answers (1)

Nefiron
Nefiron

Reputation: 31

You can't run plugins yet through Phonegap's server serve. You have to make a build and put it on the phone in order to debug those.

Upvotes: 1

Related Questions