Barcode Scanner Confusion

3 posts, 1 answers
  1. Devon
    Devon avatar
    6 posts
    Member since:
    Jan 2016

    Posted 14 Jul Link to this post

    I have a mobile application within which I am attempting to use the Cordova Barcode Scanner Plugin.

    In the demo, all of the scanner functions are placed within an extended Kendo Observable Object (kendo.data.ObservableObject.extend). I am able to get the scanner to work within my app and have deleted the extraneous code I do not need, but my ability to customize stops there. Here is the code that I am left with:

          var ScanViewModel,
                scanResult;

         ScanViewModel = kendo.data.ObservableObject.extend({
                scan: function () {
                    cordova.plugins.barcodeScanner.scan(
                        // success callback function
                        function (result) {
                            alert("Result: " + result.text);
                             scanResult = result.text;
                       },

                        // error callback function
                        function (error) {
                            alert("Scanning failed: " + error);
                        },
                        // options objects
                        {
                            "preferFrontCamera": false, // default false
                            "showFlipCameraButton": false // default false
                        }
                    );
                }

            });

            app.scanBarcode = {
                viewModel = new ScanViewModel()
            }

     

    What I want to be able to do is access results.text and assign it to a variable that I can then use throughout the rest of the app. As you can see, I have attempted to do this with the scanResult variable, but every time I go to use it after the scan, it comes back as undefined. I understand that this has something to do with it being an asynchronous call, but I'm not sure what the fix is. What can I do to get the value of results.text?

    Also, is it necessary to place the scanner functionality within the extended Observable Object? I have tried every which way to access the scan function of the plugin, but this code seems to be the only way that works for me.

    Any help would be appreciated. Thanks in advance!

  2. Answer
    Preslav
    Admin
    Preslav avatar
    50 posts

    Posted 19 Jul Link to this post

    Hi Devon,

    Thank you for your question.

    I checked the code snippet and was able to reproduce the issue. I assume the problem comes from the fact that, the "scanResult" variable is not globally set. Due to this it is undefined when you call it. To fix this behavior you can try to set the variable globally in your .js file.

    To guide you further I have prepared a sample app for which I used the Cordova BarcodeScanner Plugin. You can take a look at its index.html and scripts->demo.js files. In the index.html there is a new button with a callback to the "showScanResult" function and in the demo.js I've put the definition of the "showScanResult" function and the "scanResult" variable. I would suggest to test the app on a device and see what the "Show data in Scan Result!" button does.

    Let me know if you have any questions or comments on that topic. I hope this helps. 
     
    Regards,
    Preslav
    Telerik by Progress
     

    Visit the Telerik Verified Plugins Marketplace and get the custom Cordova plugin you need, already tweaked to work seamlessly with AppBuilder.

     
  3. Devon
    Devon avatar
    6 posts
    Member since:
    Jan 2016

    Posted 19 Jul in reply to Preslav Link to this post

    Thank you!! This is exactly what I needed. Works perfectly.
Back to Top