2

I've got a problem in Javascript using Barcodescannerplugin for Phonegap in an Android Application.

The plugin I use is bringing a small Javascript to enable using the Barcodescanner in my Phongap application. But in my opinion the interface of the provided javascript functions isn't that optimal, cause I don't want to do an errorhandling at every position where I use these methods.

That's why I tried to make that interface more easy in the way, that I just have to call a method scanBarcode() and the calling script is getting the text from the scanned or nothing, if something fails. Here is the code:

function scanBarcode(){
    var resultText = '';
    window.plugins.barcodeScanner.scan( 
        function(result) {
            if (!result.cancelled){
                resultText = result.text;
            }
        },
        function(error) {
            alert("Scanning failed: " + error);
        }
    );
    return resultText;
}

The plugin I use can be found at: Github Phonegap Plugins Android/BarcodeScanner

The result of my Method is always the same, an empty string. I think the reason is the variable scope, but I am not sure how to solve the problem.

2 Answers 2

3

The reason is because the window.plugins.barcodeScanner.scan method executes asynchronously - the success callback is not called until the method has returned.

I'd recommend returning a Promise object from your method, so something like (using jQuery as an example):

var result = $.Deferred();
window.plugins.barcodeScanner.scan( 
    function(result) {
        if (!result.cancelled){
            result.resolve(result.text);
        }
    },
    function(error) {
        result.reject(error);
    }
);

return result;
4
  • OK, so isn't there any way to make these interface more easy in the way I tried it? Aug 25, 2012 at 11:02
  • Ah ok thank you, I'll try these example cause we are using jQuery Mobile in these Webapplication as well. Aug 25, 2012 at 11:04
  • Hi Rich, I've used your examplecode and jQuery Mobile now. The result is that I get an Object back, but i can't get the scanned string out of it. Or Otherwise didn't I understand the API documentation right if i thought, that the parameter given to the resolve method of the deferred object is the value which is returned to all callbackmethods? I tried something like that: function clickScanBarcode(){ var result = scanBarcode(); alert(result); } Aug 25, 2012 at 11:34
  • 1
    @Marcus: No, the value that is returned is the deferred object. You have to register callbacks on that object, which get executed when the deferred object gets resolved. api.jquery.com/category/deferred-object Aug 25, 2012 at 12:12
1

Thanks for your help, now it works :)

For all other who has got these problem too, the resolution looks like that:

function scanBarcode(){
    var resultObject = $.Deferred();
    window.plugins.barcodeScanner.scan(
        function(result) {
            if (!result.cancelled){
                resultObject.resolve(result.text);
            } else {
                resultObject.resolve('');
            }
        },
        function(error) {
            resultObject.resolve('');
        }
    );
    return resultObject;
}

And now it is easy to get the scanned value just by saying that:

function clickScanBarcode(){
    var result = scanBarcode();
    result.done( function(text){
        alert('Barcodetext:'+text);
    });
}

I've changed the code for error and cancelled in the way, that i always get an empty string to handle with. More errorhandling isn't useful in my case.

Greetings, Marcus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.