3 Replies Latest reply on Jan 31, 2014 7:48 AM by Daniel Silva

    Safely enabling barcode scanner

    Ruben Ruben



      I've successfully enabled the barcode scanner, and used it in my apps, but i've run into a couple of problems:


           - In rhosimulator, any reference to Rho::Barcode prevents the view from showing up (I guess rhoelements api works only on devices, not on rhosimulator). My workarround is to check if Rho::Barcode is defined, but it seems a little ugly to introduce code in my app that is only aimed for rhosimulator. Is there any other way arround this?


           - In android, if I enable the scanner (which I want to do so my WM devices with integrated scanner have the HW button available), it automatically starts the callback handler without pressing anything. I have a list of items to scan where you can just click on the item, or scan the barcode associated to it, but it instantly starts the callback handler, giving a null string as barcode.


           - In WM devices, after I leave this view, I disable the scanner. If I switch rapidly between views that enable the scanner and views that disable it, the app will eventually crash (normally, just 3 attempts needed).


      This is how I enable my Barcode scanner":


      if defined?(Rho::Barcode)
          @barcodePresent = 1
          Rho::Barcode.enable({}, url_for(:action => :scan_received, :query => {:ritnr => ritnr, :stopnr => stopnr}))
          @barcodePresent = 0


      Using latest versions, on a MC45 device and a galaxy s3 mini on android 4.1


      Any suggestion?

        • Re: Safely enabling barcode scanner
          Kutir Mobility

          Hi Ruben,

          Please see below, the answers for your questions:

          1. Using system API, you can check whether it is running on Rhosimulator or not as shown below,

             Use "if !Rho::System.isRhoSimulator" instead of "if defined?(Rho::Barcode)".


          2. You should enable the scanner in the click action for android device and in the view action, check using system api whether it is running on WM devices or not.


          3. You can disable scanner in the barcode callback event.


          Visnupriya R

          Kutir Mobility

          1 of 1 people found this helpful
            • Re: Safely enabling barcode scanner
              Ruben Ruben

              Thanks for the answer, points 1 and 2 provide interesting info, but about the disable in the callback event:

              in that view, the use of the barcode scanner is optional. I want the user to be able to scan if he wants to, or just tap manually the correct item on the list. So, there is a chance he leaves the view without having executed the callback event.

              How I solve this, is simply disabling the scanner on the next view, and I think that is resposible for the crashes when I tap in and out of the view rapidly. Is there any other way to disable the scanner? Am I doing this wrong?



            • Re: Safely enabling barcode scanner
              Daniel Silva

              What I have done on some projects is check whether running on a Motorola device or not.  This should work to detect either a Windows or Android based Motorola device.  If not a Motorola device, show a scan button.

                  <% if !Rho::System.isMotorolaDevice %>

                  <a href="javascript: scan_using_default_scanner()" class="ui-btn-right" data-icon="plus" data-direction="reverse">




              In the scan_using_default_scanner() javascript function, I am checking if I'm running on rhosimulator, and if so, I just fake a scan so I can work through the app flow by pressing the scan button created above.


                                       // submit some dummy scan data




                               Rho.Barcode.take({}, scan_received);



              Then on the page, I have some javascript that loads when the page loads, and this checks for Motorola device, and if it is, it does an enable.





              Note that I am using 2 different callback functions, as the data comes back in a slightly different form depending on whether it is from a scanner or the camera. The barcode comes back in the "barcode" parameter if camera, and in "data" if from the scanner.