2016-12-19 86 views
0

我試圖使對RFID讀卡器Android應用(我不使用NFC),對於該i到OTG電纜連接一個高frquency RFID讀卡器和我正在使用EditText卡號正在顯示。它工作正常,但問題是有時它會檢測多個時間卡號。RFID卡閱讀器值機器人檢測兩次

1-任何想法如何解決這個問題(我不能把大小限制條件,因爲卡號長度不固定)?

2-當我使用超高頻讀卡器然後卡顯示不同的價值,任何想法我怎麼能做一個支持兩個頻率卡讀卡器的Android應用程序的另一個問題。

回答

0

Rfid繼續閱讀標籤它不是一次閱讀,所以從這個重複你必須管理到你身邊。檢查其讀取數據,如果是,則忽略。下面的調用實現Rfid方法及其讀取數據的句柄。

public class ReaderListener implements RfidEventListener { 

     private RfidReader reader; 
     private ReadInventory eventForm; 
     //private ItemTransfer eventFormitm; 
     private final ToneGenerator tg = new ToneGenerator(5, 100); 
     private boolean isEnabled; 
     private Map<String, Long> scannedItemsMap = new TreeMap<String, Long>(); 

     public ReaderListener(ReadInventory frm, String make, String macid) throws ReaderConnectionException, LicenseExpiryException, IOException, GeneralSecurityException { 
      Log.d("Test1", " "+make.toString().trim()+" "+ macid.toString().trim()); 
      reader = RfidFactory.getInstance().getRfidReader(make.toString().trim(), macid.toString().trim(), 
        PlatformConnector.AndroidConnector.getPlatformName()); 

      Log.d("Test2", " "+reader+" "); 
      //reader.removeAllListeners(); 
      reader.registerListener(this); 
      setEnabled(true); 
      this.eventForm = frm; 
     } 


     @Override 
     public void handleData(String tagid, int arg1, int arg2) { 
      synchronized (scannedItemsMap) { 
       if (!scannedItemsMap.containsKey(tagid)) { 
        System.out.println("got data............ :" + tagid); 

        scannedItemsMap.put(tagid, System.currentTimeMillis()); 
        if (eventForm != null) { 
         eventForm.handleData(tagid); 
         //this.tg.startTone(25); 
         Log.d("tagid", " " + tagid + " "); 

        } 
        /*if (eventFormitm != null) { 
         eventFormitm.handleData(tagid); 
        }*/ 
       } else if (scannedItemsMap.containsKey(tagid)) { 
        scannedItemsMap.put(tagid, System.currentTimeMillis()); 
       } 
      } 
     } 

     @Override 
     public void handleError(String msg) { 
      if (eventForm != null) { 
       eventForm.handleError(msg); 
      } 
     } 

     @Override 
     public boolean isEnabled() { 
      return isEnabled; 
     } 

     @Override 
     public void setEnabled(boolean arg0) { 
      this.isEnabled = arg0; 
     } 

     public boolean startScan(int power,int speed) throws ReaderConnectionException { 
      if (reader != null && !reader.isScanning()) { 
       reader.setSession(RfidSession.ONE); 
       reader.setPower(power); 
       reader.setScanSpeed(speed); 
       reader.startScan(); 
      } 
      return true; 
     } 

     public void stopScan() throws ReaderConnectionException { 
      if (reader.isScanning()) 
       reader.stopScan(); 
     } 

     public boolean isScanning() { 
      return reader.isScanning(); 
     } 

     public boolean isConnected() { 
      return reader.isConnected(); 
     } 

     public void removeAll() { 
      scannedItemsMap.clear(); 
     } 

     public void remove(String tagid) { 
      scannedItemsMap.remove(tagid); 

     } 
     public int getBatteryLevel(){ 
      try { 
       return reader.getBatteryLevel(); 
      }catch (Exception e){ 
       return 0; 
      } 
     } 

     @Override 
     public void handleReaderEvent(ReaderEvent arg0) { 
      // TODO Auto-generated method stub 

     } 
    } 

現在處理數據的活動....以下方法

顯示。

public void handleData(final String tagid) { 
     // TODO Auto-generated method stub 

     try { 
      final String Code_samplecode = convertHexToString(tagid); 

      // Log.e("Name", "Itemcode" + Code_samplecode); 
      if (SampleCode.contains(Code_samplecode)&& !p_SampleCode.contains(Code_samplecode)) { 
       // Scann items count 

       tgf.startTone(25); 

       int ind_val = SampleCode.indexOf(Code_samplecode); 

       if (ind_val != -1) { 

        final String SampleNo1 = SampleNo.get(ind_val); 
        final String StyleNo1 = StyleNo.get(ind_val); 
        final String SampleCode1 = SampleCode.get(ind_val); 
        final String StyleCode1 = StyleCode.get(ind_val); 
        //final String CartStyleCode1 = CartStyleCode.get(ind_val); 

        // final String CartStyleNo1 = CartStyleNo.get(ind_val); 


        SampleNo.remove(ind_val); 
        StyleNo.remove(ind_val); 
        SampleCode.remove(ind_val); 
        StyleCode.remove(ind_val); 

        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          // p_Code.add(Code.get(ind_val)); 
          p_SampleNo.add(SampleNo1); 
          p_StyleNo.add(StyleNo1); 
          p_SampleCode.add(SampleCode1); 
          p_StyleCode.add(StyleCode1); 


          //Code.remove(ind_val); 

//       adapter3.notifyDataSetChanged(); 
//       adapter1.notifyDataSetChanged(); 

          total_item.setAdapter(null); 

          adapter3 = new Adapter_for_Inventoryitem(ReadInventory.this, 
            StyleNo, SampleNo); 
          total_item.setAdapter(adapter3); 
          present_item.setAdapter(null); 

          adapter1 = new Adapter_for_Inventoryitem(ReadInventory.this, 
            p_StyleNo, p_SampleNo); 
          present_item.setAdapter(adapter1); 




          textvie.setText("Total " + p_SampleNo.size() + " Found Styles"); 
          textvi.setText("Total " + SampleNo.size() + " Available Styles"); 

          List<Inventory> c = db.get_all_data_INVENTARY_Query("SELECT * FROM Inventory_n WHERE SampleCode ='" + SampleCode1 + "'"); 
          if (c.size() > 0) { 
           db.execute_query("INSERT INTO Inventory_p (Code, SampleNo,StyleNo,SampleCode,StyleCode,CartStyleNo,CartStyleCode) VALUES ('" + c.get(0).getCode() + "' , \"" + 
             c.get(0).getSampleNo() + "\" , \"" + c.get(0).getStyleNo() + "\" , '" + 
             c.get(0).getSampleCode() + "' , '" + c.get(0).getStyleCode() + "' , \"" + 
             c.get(0).getCartStyleNo() + "\" , '" + c.get(0).getCartStyleCode() + "')"); 
          } 
          db.execute_query("DELETE FROM Inventory_n WHERE SampleCode ='" + SampleCode1 + "'"); 


         } 
        }); 


       } 
      } else { 

       if (!SampleCode.contains(Code_samplecode) && !p_SampleCode.contains(Code_samplecode) 
         && !not_fount_items.contains(Code_samplecode)) { 

         tgn.startTone(20); 

        scanneditems_unkown = scanneditems_unkown + 1; 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          not_fount_items.add(Code_samplecode); 

          not_fount_items_txt.setText("Total " + not_fount_items.size() + " Unknown Styles"); 


         } 
        }); 
       } 

      } 

      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 

        scan_counts.setText("Total " + p_SampleNo.size() + " Scanned"); 
        scan_counts_unknown.setText("Total " + scanneditems_unkown + " Unknown"); 

        last_scanned_items.setText("Item : " + Code_samplecode); 


       } 
      }); 

     } catch (Exception e) { 
      e.printStackTrace(); 

      Message message = new Message(); 
      message.what = READEREXCEPTION; 
      itemDetectedHandler.sendMessage(message); 

     } 
    } 
+0

如何在上面的代碼中實現RfidEventListener? –

+0

OnClick事件按鈕調用此類。將參數傳遞給構造函數。 –