rfid读卡器值android检测两次
Posted
技术标签:
【中文标题】rfid读卡器值android检测两次【英文标题】:Rfid card reader value android detecting twice 【发布时间】:2016-12-19 06:27:37 【问题描述】:我正在尝试为 RFID 读卡器制作一个 android 应用程序(我没有使用 NFC),为此我通过 连接了一个 高频 RFID 读卡器 >OTG 电缆,我正在使用显示卡号的 EditText。它工作正常,但问题是有时它会检测到多个时间卡号。
1- 知道如何解决这个问题(由于卡号长度不固定,我无法设置大小限制条件)?
2- 当我使用超高频读卡器时还有一个问题,然后卡显示不同的价值,知道如何制作一个支持两种频率读卡器的安卓应用程序。
【问题讨论】:
【参考方案1】:Rfid 继续阅读标签它不是一次阅读,所以从这个重复中你必须设法站在你身边。检查其读取数据,如果是则忽略。在调用 Where 实现 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);
【讨论】:
你是如何在上面的代码中实现 RfidEventListener 的? 按钮调用此类的OnClickEvent..将参数传入构造函数。【参考方案2】:你需要重写
public boolean onKeyDown(final int keyCode, final KeyEvent event)
//To get the characters (One By one)
event.getDisplayLabel();
如果卡号长度不同,请使用 Debounce(我建议使用 RX Java)。 debounce 的作用是,它会在 keyevent 发生后等待一段特定的时间,所以你可以连接整个字符串然后使用它。
【讨论】:
以上是关于rfid读卡器值android检测两次的主要内容,如果未能解决你的问题,请参考以下文章
如何通过使用 Python2.7 将卡刷到读卡器上来捕获 RFID 卡的 UID?