Rfid card reader value android detecting twice

1.4k views Asked by At

I am trying to make an android app for RFID card reader (i am not using NFC), for this i connect one high frquency RFID card reader through OTG cable and i am using EditText where card number is displaying. it is working fine, but problem is sometime it detects multiple time card number.

1- Any idea how can i resolve this (i cannot put size limit condition because card number length is not fixed)?

2- One more problem when i am using ultra high frequency card reader then card is showing different value, any idea how can i make an android app which supports both frequency card readers.

2

There are 2 answers

2
Ramchandra Singh On

Rfid continue reading tag its not one time reading so from this repetition you have to manage to your side. Check its reading data or not if yes then ignore.Below the call Where implement Rfid methods and its reading data handle.

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

        }
    }

Now Handle The data in activity ....

Show in below method.

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
Jaswant Singh On

You need to Override

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

And use Debounce (I'll recommend using RX Java) if the card numbers are of different length. What debounce does is, it will wait for a particular amount of time after a keyevent happens, So you can concat the whole string and then use it.