5 Replies Latest reply on Jun 14, 2013 3:32 PM by Hector Meza

    Default page is appearing while invoking scanner in Rho app

      Hi,

       

      I am able to successfuly invoke device inbuilt scanner API and abale to retrieve the barcode info. However while invoking the scannere fucntion in controller class (.rd) from html page(index.erb) , on click of scan button, the scanner call is being invoked and after data retrivel it is navigating to a default blank  page. I would like to invoke the scanner and retrieve the data on the same page without navigating to default page.

       

      The reason I believe is using of  url_for: to invoke the ruby function from the index.erb file. Is there any other way to invoke the ruby functions from the index.erb file?

      Below is the code

       

      index.rb file

       

       

      <form name="scanUPC-form" action="http://www.welcome.com" class="login-form" id="login-form">
                       <div class="inputs">
                         <input id="upc" type="text" placeholder="Enter UPC" name="UPC"  required>  
                       </div>
                       <div class="login-btn">
                         <input type="submit" value="Search" id="searchButton"/>
                      

                         <a href="<%= url_for(:action => :enableScanner)%>">
                             <button>Scan UPC</button>
                         </a>
                         
                    
        </form>

       

      conroller.rb

       

      def enableScanner

           

            Alert.show_popup('in enable scanner')

            Scanner.enabled = "SCN2"

              Scanner.decodeEvent = url_for(:action => :scannerEventListener)

            

          end

          

          def scannerEventListener

           

              Alert.show_popup('in scannerEventList')

              Alert.show_popup(@params['data'])

           

           

          end

       

       

       

       

       

       

       

       

        • Re: Default page is appearing while invoking scanner in Rho app
          Hector Meza

          Srinu, when you enable the scanner in the controller, I would suggest you do it at the controller init method i.e. added the logic to the index method of the controller.  the WebView tied to the call back will be the index.erb,  if you enable by calling a method in the controller from your form, the WebView is likely being set to the button and not the form (that is why you get the blank return).  If you want form level control, you should leverage the API via JS.

           

          The following code example should validate this:

          Add this line to your callback method - see what it returns, 

              Alert.show_popup "In callback method the webview is: #{WebView.current_location} "

           

          HM

            • Re: Default page is appearing while invoking scanner in Rho app

              Hi HM,

               

              Thanks for you quick reply. However I am still not clear how to implemnet this. My main requirment is upon clicking on scan button in html(erb) file, how to retrieve the barcode value without going to blank page and pushing it to the other html/controller file. Also pls give insights on invoking the API via JS .

                • Re: Default page is appearing while invoking scanner in Rho app
                  Hector Meza

                  The following is a sample of having enabling the scanner in the controller via Ruby and a form that displays the scan value:

                   

                  Lets call the form invfind.erb  (inventory find)

                  in the model controller - say "Inventory.rb you should have an init method for invfind that could look like the following:

                  In my example all I am doing is enabling the scanner and setting the call back method that will get called on a scan - I also added some logic so it will run in the emulator and set the scan engine for a MSI android product - you would adjust your code as this is just a demo

                   

                  # Enable Scanner

                  def invfind

                        #-- Lets make sure we can run in the emulator, which would not have a scanner 

                       if !(System::get_property('is_emulator'))

                            Scanner.decodeEvent = url_for(:action => :decodeInvCallback)

                            if System::get_property('platform') == 'ANDROID'

                                # MSI MC40/ET1 have the scanner as the 3rd element

                                Scanner.enabled = 'SCN2'

                           else

                                Scanner.enable

                           end

                      end

                       render :action => :invfind

                  end

                   

                   

                  OK, so lets talk about the call back - it is called when you are still in this model controller - i,e, the invfind.erb will still have the veiw but logic wil trigger here.  There are several parmeters returned in the call back but all I am looking for is the data - i.e. barcode.  I also am calling a JS funtion that is in the invfind.erb called "showInvdata" that updates the view on the form.

                   

                   

                  # Scanner callback for invfind

                    def decodeInvCallback

                          barcode = @params['data']

                         # look for item in Inventory

                         @inventory = Inventory.find(

                                 :first,

                                 :conditions => {:SKU => barcode})  

                         if @inventory.nil?

                              Alert.show_popup('SKU Not Found!')

                         else

                             invdesc = inventory.Description

                             WebView.execute_js("showInvdata('"+barcode+"','"+invdesc+"'); ")

                        end

                  end

                   

                   

                   

                  The invfind.erb js function could look like this:

                       <script type="text/javascript">

                              function showInvdata(SKU, invdesc {

                                    if (!document.getElementsByTagName) return;

                                       document.getElementById("barcode").innerHTML = SKU;

                                      document.getElementById("productName").innerHTML = invdec;

                                }

                   

                   

                  Hope this helps.