tags - TagLostException when reading NFC-F card on Android -
i'm developing android application needs read nfc card (the card technology nfc-f). there, following exception:
android.nfc.taglostexception: tag lost.
here code:
private void handleintent(intent intent) { string action = intent.getaction(); if (nfcadapter.action_ndef_discovered.equals(action)) { } else if (nfcadapter.action_tech_discovered.equals(action)) { } else if(nfcadapter.action_tag_discovered.equals(action)) { tag tag = intent.getparcelableextra(nfcadapter.extra_tag); if (tag != null) { nfcf nfcf = nfcf.get(tag); try { nfcf.connect(); byte[] auto_polling_start = {(byte) 0xe0, 0x00, 0x00, 0x40, 0x01}; byte[] response = nfcf.transceive(auto_polling_start); nfcf.close(); } catch (exception e) { e.printstacktrace(); mtextview.settext(e.tostring()); } } } } can me regarding issue?
you receive taglostexception because send invalid command tag. since nfc-f tags remain silent upon reception of invalid commands, android cannot distinguish between actual loss of communication or negative response unsupported/invalid command , throws taglostexception in both cases.
valid felica (nfc-f) commands have form
+----------+----------+------------------+-----------------------------------------------+ | len | command | idm | parameter | | (1 byte) | (1 byte) | (8 bytes) | (n bytes) | +----------+----------+------------------+-----------------------------------------------+
you assemble them following method:
public byte[] transceivecommand(nfcf tag, byte commandcode, byte[] idm, byte[] param) { if (idm == null) { // use system idm idm = tag.gettag().getid(); } if (idm.length != 8) { idm = arrays.copyof(idm, 8); } if (param == null) { param = new byte[0]; } byte[] cmd = new byte[1 + 1 + idm.length + param.length]; // len: fill placeholder cmd[0] = (byte)(cmd.length & 0x0ff); // cmd: fill placeholder cmd[1] = commandcode; // idm: fill placeholder system.arraycopy(idm, 0, cmd, 2, idm.length); // param: fill placeholder system.arraycopy(param, 0, cmd, 2 + idm.length, param.length); try { byte[] resp = tag.transceive(cmd); return resp; } catch (taglostexception e) { // warn: tag-loss cannot distinguished unknown/unexpected command errors } return null; } for instance, command should succeed on tags request system code command (0x0e):
nfcf.connect(); byte[] resp = transceivecommand(nfcf, (byte)0x0c, null, null); nfcf.close();
Comments
Post a Comment