11

I'm trying to use the BarcodeScanner plugin for Cordova in a BlackBerry device without luck. The sample code makes use of this structure:

window.plugins.BarcodeScanner...

but window.plugins is undefined.

I tried with a fresh new application and window.plugins continues to be undefined (I mean, the sample application without any addition).

Did the api have changed and the documentation is outdated or there's something I'm missing?

Thanks!

Ivo
  • 8,172
  • 5
  • 27
  • 42
  • Note that currently the syntax is quite different: http://community.phonegap.com/nitobi/topics/_barcodescanner_plugin_upgrading_scanner_javascript_api_code_changes_required – Nux Jun 21 '13 at 16:17
  • 1
    "This question is unlikely to help any future visitors;" How come it has 9 votes and 7689 views? – Spiff Oct 23 '13 at 18:10

4 Answers4

8

Cordova 2.0 has removed the "addPlugin" method used by the BarcodeScanner plugin. So a quick fix would be to remove (or comment out) the "addConstructor" function used to add the plugin, and replace it with an explicit attachment to the window object:

//cordova.addConstructor(function() {
//    cordova.addPlugin('barcodeScanner', new BarcodeScanner());
//});

window.barcodeScanner = new BarcodeScanner();

Then, since "window.plugins" isn't used, you will also need to change the code that calls the "scan" method, so replace

window.plugins.barcodeScanner.scan(...

with

window.barcodeScanner.scan(...

I have tested this with Cordova 2.0 and it works.

dogatonic
  • 2,658
  • 1
  • 23
  • 14
  • but when ever i am calling **window.barcodeScanner.scan(...** some times it calls to my plugin and some times not.......can u tell me why? and gives me **window.barcodeScanner.scan[undefined]is not a function** – Subrat nayak. Aug 08 '12 at 06:45
  • A sample of your code would be needed to help diagnose your problem. – dogatonic Aug 20 '12 at 18:37
3

Just ran into the same problem. After looking into the window-object I found the BarcodeScanner being right there. Sowindow.BarcodeScanner.prototype.scan(result, error) did the trick. Make sure you wait for cordova to be fully initialized, otherwise you may get sth like has no method exec()

d2uX
  • 241
  • 2
  • 14
  • you mean, just call `window.BarcodeScanner`? – Ivo Jul 23 '12 at 15:40
  • yep, that's what worked for me. if you `console.log(window)` you should see the `BarcodeScanner` – d2uX Jul 23 '12 at 16:07
  • Didn't work. `window.BarcodeScanner` is `undefined` too. – Ivo Jul 23 '12 at 20:28
  • without modifying anything in the plugin itself: `var bcs = new window.BarcodeScanner(); console.log(bcs)` **returns** `BarcodeScanner __proto__: Object constructor: function () { encode: function (type, data, successCallback, errorCallback, options) { scan: function (successCallback, errorCallback) { __proto__: Object` – d2uX Jul 24 '12 at 06:58
2

All, I pushed a new BarcodeScanner this morning that works with 2.0.0.

https://github.com/phonegap/phonegap-plugins/tree/master/Android/BarcodeScanner/2.0.0

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
0

Finally, I used cordova 1.9.0, as plugins are not up-to-date yet.

Thanks to everyone!

Ivo
  • 8,172
  • 5
  • 27
  • 42