Rfid读卡器价值android检测两次

时间:2016-12-19 06:27:38

标签: android rfid

我正在尝试为RFID读卡器制作一个Android应用程序(我不使用 NFC ),为此我连接一个高频率RFID读卡器通过 OTG 电缆,我正在使用 EditText 显示卡号。它工作正常,但问题是它有时检测到多个时间卡号。

1-任何想法如何解决这个问题(我不能设置尺寸限制条件,因为卡号长度不固定)?

2-当我使用超高频读卡器时还有一个问题,那么卡显示出不同的价值,任何想法如何制作支持这两种读卡器的Android应用程序。

2 个答案:

答案 0 :(得分: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);

        }
    }

答案 1 :(得分:0)

您需要覆盖

public boolean onKeyDown(final int keyCode, final KeyEvent event) {
    //To get the characters (One By one)
    event.getDisplayLabel();
}

如果卡号长度不同,请使用防抖动功能(我建议使用RX Java)。 防抖动的作用是,它将在按键事件发生后等待特定的时间,因此您可以连接整个字符串然后使用它。