Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Cordova plugin methods from Meteor?

New to Meteor here. I'm having trouble calling Cordova plugin methods from Meteor.

Here is the plugin I care about: http://plugins.cordova.io/#/package/com.phonegap.plugins.barcodescanner

I added the package in command line: meteor add cordova:[email protected]

Below is my javascript code. What ends up happening is on startup, the onCallback method loads, but no barcode scanning happens and neither onSuccess nor onError gets called. I've tried similar approach with other cordova packages but nothing works. I've also tried replacing 'cordova.plugins.barcodeScanner.scan' in the cordova.call with variations like all lower caps, 'barcodeScanner.scan', 'com.phonegap.plugins.barcodescanner.scan', etc., but to no avail.

if (Meteor.isCordova) {
    Meteor.startup(function () {
        cordova = new Cordova();
        cordova.addEventListener('deviceready', function() {
            function onSuccess(result) {
                alert("We got a barcode\n" +
                    "Result: " + result.text + "\n" +
                    "Format: " + result.format + "\n" +
                    "Cancelled: " + result.cancelled);      
            }

            function onError(error) {
                alert("Scanning failed: " + error);     
            }

            function onCallback(msg) {
                alert("Callback! " + msg);  
            }

            cordova.call(
                'cordova.plugins.barcodeScanner.scan', 
                [onSuccess, onError], 
                onCallback);
        });
    }
}
like image 931
silverbuggy Avatar asked Nov 29 '22 23:11

silverbuggy


1 Answers

In a Meteor Cordova App, Meteor.startup works as the deviceready event so you won't need the deviceready event. Also cordova is already defined in the global context so you won't need to try to creat a new instance. Finally, reading the plugin page on plugins.cordova.io explains that the plugin is namespaced to cordova.plugins.barcodeScanner. In your case the scan method on the barcodeScanner class is of particular interest. You can use it in meteor like below, but take note this will open the scanner when the app starts up from a full stop so it's probably better to invoke the scan method from something such as a click event.

if(Meteor.isCordova){
    Meteor.startup(function () {
        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);
            }
        );
    });
}
like image 81
Kelly Copley Avatar answered Dec 04 '22 03:12

Kelly Copley