Phonegap barcode scan plugin not working

  • 1
  • Question
  • Updated 5 years ago
I am programming my app in Brackets and then zipping it up and uploading it to PhoneGap build for building my iPhone and Android installers.

So far I have added this line of code to my config.xml:

I then create a page in my app with this code:

alert("scan open");

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);
}
);

And just my first alert showing "scan open" pops up, I never get any barcode scanner on Android. Am I missing any steps?
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
  • frustrated

Posted 5 years ago

  • 1
Photo of Yi Ming Kuan

Yi Ming Kuan

  • 1840 Posts
  • 78 Reply Likes
Can you post the PhoneGap Build app id here?

-yiming
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
Not sure what you mean by the PhoneGap build ID
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
When viewing your app online you should see "App ID" in the details. Also its in the url i.e.e https://build.phonegap.com/apps/XXXXX...
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
1536482 How is this going to help?
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
Lets us employees have a look at the configuration of your app
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
Awesome, let me know what you see.
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
Did you add the barcodescanner plugin to your config.xml file?

<gap:plugin name="phonegap-plugin-barcodescanner" source="npm" />
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
I have this in mine:
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
So when ever I post the xml it takes it out of the comment, here it is without the arrows:

gap:plugin name="com.phonegap.plugins.barcodescanner" version="2.2.0"
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
Thats an older version, but should still work. see response below.
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
Which is a newer version that I should use?
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
I notice that you're running your code on document.ready -- use deviceready instead for phonegap applications:

$(document).on('deviceready', function() {
...


Might even be better to activate the scanner on a button click to be sure the plugin has initialized.
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
Awesome About to try this out! Will deviceready change any other functionality?
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
So I tried this and it did not work... You can look at the build right now that is online.
Photo of Abhishek Deshkar

Abhishek Deshkar

  • 48 Posts
  • 0 Reply Likes
Have you added phonegap.js at the top ?
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
I do not have a phonegap.js in my html page. Are you talking about my scan.html page or my config.xml?
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes
ah yes. any html page that utilizes device functionality (such as the barcodescanner) needs to include cordova.js (or phonegap.js, same effect):

<script src="cordova.js"></script>

not having this will result in the barcodescanner being unavailable, and also the deviceready event won't fire.

Possibly helpful: https://github.com/wildabeast/Barcode...

also I'd recommend checking out chrome remote debugger -- helps to debug issues like this.
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
Where can I download the cordova.js file?
Photo of searayman

searayman

  • 23 Posts
  • 1 Reply Like
Figured it out, the compiler adds it automatically! Now I can start using plugins!
Photo of ryan

ryan, Developer

  • 1538 Posts
  • 132 Reply Likes