NFC-Read and write NFC tag text

Source: Internet
Author: User

Follow up on an NFC-enabled Android Auto-run program, which collates text that reads and writes NFC tags

Before you start, think about the following



for NDEF text Format. the first 1 bytes of the data describe the state of the data, and then several bytes describe the language encoding of the text, and the last remaining bytes represent the text data. These data formats are defined by the relevant specifications of the NFC Forum and can be downloaded from the address below.

Http://www.nfc-forum.org/specs/spec_dashboard


Ndef Text data format:
Status byte encoding format:
Based on the above information, you can start code new activity
Package Com.cayden.read.write.text;import Java.nio.charset.charset;import Java.util.locale;import Android.app.activity;import Android.app.pendingintent;import Android.content.intent;import Android.nfc.ndefmessage;import Android.nfc.ndefrecord;import Android.nfc.nfcadapter;import Android.nfc.Tag;import Android.nfc.tech.ndef;import Android.os.bundle;import Android.view.view;import Android.widget.TextView;import Android.widget.toast;public class Readwritetextmainactivity extends Activity {private TextView minputtext;private String mtext;private nfcadapter mnfcadapter;private pendingintent mpendingintent; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_read_write_text_main); minputtext = (TextView) Findviewbyid (R.id.textview_input_text); Mnfcadapter = Nfcadapter.getdefaultadapter (this); mpendingintent = pendingintent.getactivity (this, 0, new Intent (This, GetClass ()), 0);} Protectedvoid Onactivityresult (int requestcode, int resultcode, Intent data) {if (Requestcode = = 1 && resultcode = 1) {MTe XT = Data.getstringextra ("text"); Minputtext.settext (MText);}} @Overrideprotected void OnPause () {super.onpause (); if (mnfcadapter! = null) Mnfcadapter.disableforegrounddispatch ( this);} @Overrideprotected void Onresume () {super.onresume (); if (mnfcadapter! = null) Mnfcadapter.enableforegrounddispatch ( This, mpendingintent, null,null);} public void Onnewintent (Intent Intent) {Toast.maketext (this, "onnewintent", Toast.length_short). Show ();//Read NFC Textif (MText = = null) {Intent myintent = new Intent (this, shownfctagcontentactivity.class); Myintent.putextras (Intent); Myintent.setaction (nfcadapter.action_ndef_discovered); startactivity (myintent);} else/write NFC Text{tag Tag = Intent.getparcelableextra (Nfcadapter.extra_tag); Ndefmessage ndefmessage = new Ndefmessage (new ndefrecord[] {Createtextrecord (MText)}); Writetag (ndefmessage, tag);}} /** * * Create Ndefrecord * @param text * @return * */public Ndefrecord Createtextrecord (String text) {byte[] langbytes = Locale.CHINA.getLanguage (). GetBytes ( Charset.forname ("Us-ascii"));  Charset utfencoding = Charset.forname ("UTF-8"); byte[] textbytes = text.getbytes (utfencoding); int utfbit = 0;char Status = (char) (Utfbit + langbytes.length); byte[] data = new Byte[1 + langbytes.length + textbytes.length];d ata[0] = (byte) status; System.arraycopy (langbytes, 0, data, 1, langbytes.length); System.arraycopy (textbytes, 0, data, 1 + langbytes.length,textbytes.length); Ndefrecord Ndefrecord = new Ndefrecord (Ndefrecord.tnf_well_known,ndefrecord.rtd_text, new byte[0], data); return Ndefrecord;} /** * Write data to an NFC tag * @param ndefmessage * @param tag * @return * */boolean writetag (ndefmessage ndefmessage, tag tag) {try { Ndef Ndef = ndef.get (tag); Ndef.connect (); Ndef.writendefmessage (ndefmessage); return true;} catch (Exception e) {//Todo:handle Exception}return false;} public void Onclick_inputtext (view view) {Intent Intent = new Intent (This, InpuTtextactivity.class); Startactivityforresult (intent, 1);}} 

Then create an activity that reads the content
Package Com.cayden.read.write.text;import Android.app.activity;import Android.nfc.ndefmessage;import Android.nfc.ndefrecord;import Android.nfc.nfcadapter;import Android.nfc.tag;import Android.nfc.tech.Ndef;import Android.os.bundle;import Android.os.parcelable;import Android.widget.textview;public Class Shownfctagcontentactivity extends Activity {private TextView mtagcontent;private Tag mdetectedtag;private String Mtagtext; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_show_nfctag_content); mtagcontent = (TextView) findViewById ( r.id.textview_tag_content); Mdetectedtag = Getintent (). Getparcelableextra (Nfcadapter.extra_tag); Ndef Ndef = Ndef.get (mdetectedtag), Mtagtext = Ndef.gettype () + "\nmax size:" + ndef.getmaxsize () + "bytes\n\n"; Readnfctag ( ); Mtagcontent.settext (Mtagtext);} private void Readnfctag () {if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals (Getintent (). Getaction ())) {Parcelable[] Rawmsgs = Getintent (). Getparcelablearrayextra (Nfcadapter.extra_ndef_messages);  Ndefmessage msgs[] = null;int contentsize = 0;if (rawmsgs! = null) {msgs = new Ndefmessage[rawmsgs.length];for (int i = 0; i < rawmsgs.length; i++) {Msgs[i] = (ndefmessage) rawmsgs[i];contentsize + = Msgs[i].tobytearray (). length;}} try {if (msgs! = null) {Ndefrecord record = Msgs[0].getrecords () [0]; Textrecord Textrecord = Textrecord.parse (record), Mtagtext + = Textrecord.gettext () + "\n\ntext\n" + contentsize + "bytes"; }} catch (Exception e) {//Todo:handle Exception}}}




NFC-Read and write NFC tag text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.