Cordova call can only be made from Meteor.startup()

Is anyone else frustrated that cordova calls only work from within Meteor.startup()?

I have been trying to call the barcode scanner from a button click. It just doesn’t return to the called functions. I finally noticed this integration document and tested having the barcode scanner called from Meteor.startup and the scanner works and returns correctly.

Is there another way to do this? I still want the scanner called from my button.

I think you just have a bug, please show what you tried.

You cannot call Cordova methods just plain the the source code. That makes sense because: It just did not have the time yet to load everything.

Then Meteor.startup gets called. From that moment you are ready to use the scanner.

So, if you call it from a event handler it should also just work. Because that is also after the startup moment. So I suspect a bug in the code.

Also it might be good to code defensive, so maybe add some checks like: if(!MyBarcodeObject) to give you clear error messages when there are loading issues.

1 Like

It is not a loading/timing issue anymore. Here is the code. https://github.com/lornaz/meteorProductCapture.git

The barcode scanner is called on the button press, but doesn’t not return correctly. The same call works when it is in Meteor.startup().

Hi the repo is empty only see some license.

It should be there now. Github desktop was failing on publish. Had do delete and recreate.

Do you get some message? In the iOS simulator I get:

Scanning failed. Unable to obtain video capture device.

Which is correct because we can only test camera functionality on a real device.

If you want to test this kind of issues make your issue smaller, I made it like this as a start. Try to remove all unrelated code.

If I know how you have been testing we might be able to help you further.

function onDeviceReady() {
  console.log(device);
}

if (Meteor.isCordova) {
  Meteor.startup(function () {
   document.addEventListener("deviceready", onDeviceReady, false);
  });

  Template.scanBarcode.helpers({
    barcode: function () {
      return Session.get("barcode");
    }
  });

  Template.scanBarcode.events({

    'click button': function () {
      console.log('click detected');
      
      cordova.plugins.barcodeScanner.scan(
        function (result) {
          alert("We got a barcode\n" +
            "Result: " + result.text + "\n" +
            "Format: " + result.format + "\n" +
            "Cancelled: " + result.cancelled);
            if (!result.cancelled) {
             Session.set("barcode", result.text);
             }
        }, 
        function (error) {
          alert("Scanning failed: " + error);
        }
      );
    },

  });
}

I am testing on an android tablet. The android simulator doesn’t let me test this either.

ok so it works on the real device?

The call to barcodescanner.scan works, but does not call function(result) in the case of a button click. It only works and returns the result when called from Meteor.startup().

More testing… The call to the scanner also works from function onDeviceReady() but only when device ready is called from startup. Just not on the button click.