Reading an NDEF message from an NFC tag from an Android application -
i trying create application using nfc , want try , read nfc tag , text message tag , place textview. have code already, nothing happens when try pair phone nfc tag.
here code , please @ , see doing wrong , needs done fix issue please:
button measurementsdatabutton; nfcadapter mynfcadapter; pendingintent mypendingintent; intentfilter ndef; intentfilter[] filters; string[][] techlists; int mcount; textview mtext; string payload; byte payloadheader; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_nfc_scanner); final actionbar actionbar = getactionbar(); actionbar.setdisplayhomeasupenabled(true); mtext = (textview) findviewbyid(r.id.flowtextview1); measurementsdatabutton = (button) findviewbyid(r.id.measurementsbutton1); measurementsdatabutton.setonclicklistener(this); mynfcadapter = nfcadapter.getdefaultadapter(this); mypendingintent = pendingintent.getactivity(this, 0, new intent(this, getclass()).addflags(intent.flag_activity_single_top), 0); ndef = new intentfilter(nfcadapter.action_ndef_discovered); filters = new intentfilter[] {ndef, }; techlists = new string[][] {new string[] {ndef.class.getname()}, new string[] {ndefformatable.class.getname()}}; } @override public void onpause() { super.onpause(); mynfcadapter.disableforegrounddispatch(this); } @override public void onresume() { super.onresume(); if(mynfcadapter != null) { mynfcadapter.enableforegrounddispatch(this, mypendingintent, filters, techlists); } } @override public void onnewintent(intent intent) { if (nfcadapter.action_ndef_discovered.equals(getintent())) { ndefmessage [] messages = getndefmessages(getintent()); for(int = 0; i<messages.length; i++) { for(int j = 0; j<messages[0].getrecords().length; j++) { ndefrecord record = messages[i].getrecords()[j]; payload = new string(record.getpayload(), 1, record.getpayload().length-1, charset.forname("utf-8")); mtext.settext(payload); payloadheader = record.getpayload()[0]; } } } } ndefmessage[] getndefmessages(intent intent) { // todo auto-generated method stub ndefmessage[] message = null; if (nfcadapter.action_ndef_discovered.equals(intent.getaction())) { parcelable[] rawmessages = intent.getparcelablearrayextra(nfcadapter.extra_ndef_messages); if(rawmessages != null) { message = new ndefmessage[rawmessages.length]; for(int = 0; < rawmessages.length; i++) { message[i] = (ndefmessage) rawmessages[i]; } } else { byte[] empty = new byte[] {}; ndefrecord record = new ndefrecord (ndefrecord.tnf_unknown, empty, empty, empty); ndefmessage msg = new ndefmessage (new ndefrecord[] {record}); message = new ndefmessage[] {msg}; } } else { log.d("", "unknown intent."); finish(); } return message; }
example of code send-receive ndef tag:
public class mainactivity extends appcompatactivity implements nfcadapter.createndefmessagecallback { private nfcadapter mnfcadapter; textview tv; private pendingintent mpendingintent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv = (textview) findviewbyid(r.id.textviewreceivedmessage); nfcmanager nfcmanager = (nfcmanager) getsystemservice(nfc_service); mnfcadapter = nfcmanager.getdefaultadapter(); mpendingintent = pendingintent.getactivity(this, 0, new intent(this, getclass()) .addflags(intent.flag_activity_single_top), 0); } private static ndefmessage gettestmessage() { byte[] mimebytes = "application/com.android.cts.verifier.nfc" .getbytes(charset.forname("us-ascii")); byte[] id = new byte[] {1, 3, 3, 7}; byte[] payload = "cts verifier ndef push tag".getbytes(charset.forname("us-ascii")); return new ndefmessage(new ndefrecord[] { new ndefrecord(ndefrecord.tnf_mime_media, mimebytes, id, payload) }); } @override protected void onresume() { super.onresume(); mnfcadapter.enableforegrounddispatch(this, mpendingintent, null, null); mnfcadapter.setndefpushmessagecallback(this, this); } // sending message @override public ndefmessage createndefmessage(nfcevent event) { return gettestmessage(); } private ndefmessage[] getndefmessages(intent intent) { parcelable[] rawmessages = intent.getparcelablearrayextra(nfcadapter.extra_ndef_messages); if (rawmessages != null) { ndefmessage[] messages = new ndefmessage[rawmessages.length]; (int = 0; < messages.length; i++) { messages[i] = (ndefmessage) rawmessages[i]; } return messages; } else { return null; } } static string displaybytearray(byte[] bytes) { string res=""; stringbuilder builder = new stringbuilder().append("["); (int = 0; < bytes.length; i++) { res+=(char)bytes[i]; } return res; } // displaying message @override protected void onnewintent(intent intent) { super.onnewintent(intent); ndefmessage[] messages = getndefmessages(intent); tv.settext(displaybytearray(messages[0].tobytearray())); }
}
Comments
Post a Comment