1 //向NFC读写数据 2 public class ReadWriteTextMainActivity extends Activity { 3 private TextView mInputText; 4 private String mText; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 // TODO Auto-generated method stub 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.fragment_main); 11 12 mInputText = (TextView) findViewById(R.id.textView_inputtext); 13 14 } 15 16 @Override 17 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 18 // TODO Auto-generated method stub 19 super.onActivityResult(requestCode, resultCode, data); 20 if (requestCode == 1 && resultCode == 1) { 21 mText = data.getStringExtra("text"); 22 mInputText.setText(mText); 23 } 24 25 } 26 27 @Override 28 protected void onNewIntent(Intent intent) { 29 // TODO Auto-generated method stub 30 super.onNewIntent(intent); 31 // 读NFC文本 32 if (mText == null) { 33 Intent myIntent = new Intent(this, ShowNFCContentActivity.class); 34 myIntent.putExtras(intent); 35 myIntent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED); 36 startActivity(myIntent); 37 } else { 38 // 写入NFC标签 39 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 40 NdefMessage ndefMessage = new NdefMessage( 41 new NdefRecord[] { creatTextRecord(mText) }); 42 writeTag(ndefMessage, tag); 43 44 } 45 } 46 47 public NdefRecord creatTextRecord(String text) { 48 49 byte[] langBytes = Locale.CHINA.getLanguage().getBytes( 50 Charset.forName("US-ASCII")); 51 Charset utfEncoding = Charset.forName("UTF-8"); 52 byte[] textBytes = text.getBytes(utfEncoding); 53 int utfBit = 0; 54 char status = (char) (utfBit + langBytes.length); 55 byte[] data = new byte[1 + langBytes.length + textBytes.length]; 56 data[0] = (byte) status; 57 System.arraycopy(langBytes, 0, data, 1, langBytes.length); 58 System.arraycopy(textBytes, 0, data, 1 + langBytes.length, 59 textBytes.length); 60 61 NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 62 NdefRecord.RTD_TEXT, new byte[0], data); 63 return ndefRecord; 64 65 } 66 67 boolean writeTag(NdefMessage ndefMessage, Tag tag) { 68 try { 69 Ndef ndef = Ndef.get(tag); 70 ndef.connect(); 71 ndef.writeNdefMessage(ndefMessage); 72 return true; 73 74 } catch (Exception e) { 75 // TODO: handle exception 76 } 77 78 return false; 79 80 } 81 }
1 //读取nfc标签 2 public class ShowNFCContentActivity extends Activity { 3 private TextView mTagContent; 4 private Tag mDetecTag; 5 private String mTagText; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 // TODO Auto-generated method stub 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.show_nfc_tagcontent); 12 mTagContent = (TextView) findViewById(R.id.textView_tag_content); 13 mDetecTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); 14 Ndef ndef = Ndef.get(mDetecTag); 15 16 mTagText = ndef.getType() + " maxsize:" + ndef.getMaxSize() 17 + "bytes "; 18 19 readNFCtag(); 20 21 mTagContent.setText(mTagText); 22 23 } 24 25 private void readNFCtag() { 26 // TODO Auto-generated method stub 27 if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { 28 29 Parcelable[] rawMsg = getIntent().getParcelableArrayExtra( 30 NfcAdapter.EXTRA_NDEF_MESSAGES); 31 NdefMessage message[] = null; 32 int contenSize = 0; 33 if (rawMsg != null) { 34 message = new NdefMessage[rawMsg.length]; 35 for (int i = 0; i < rawMsg.length; i++) { 36 message[i] = (NdefMessage) rawMsg[i]; 37 contenSize += message[i].toByteArray().length; 38 39 } 40 } 41 try { 42 if (message != null) { 43 NdefRecord record = message[0].getRecords()[0]; 44 TextRecord textRecord = TextRecord.parse(record); 45 mTagText += textRecord.getText() + " text " 46 + contenSize + "bytes"; 47 48 } 49 } catch (Exception e) { 50 // TODO: handle exception 51 } 52 } 53 } 54 55 }
1 public class InputTextActivity extends Activity { 2 private EditText mTextTag; 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 // TODO Auto-generated method stub 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.input); 9 mTextTag = (EditText) findViewById(R.id.editText1); 10 11 } 12 13 public void click(View view) { 14 Intent intent = new Intent(); 15 intent.putExtra("text", mTextTag.getText().toString()); 16 setResult(1, intent); 17 finish(); 18 } 19 }
1 public class TextRecord { 2 private final String mText; 3 4 private TextRecord(String text) { 5 // TODO Auto-generated constructor stub 6 7 mText = text; 8 } 9 10 public String getText() { 11 return mText; 12 } 13 14 public static TextRecord parse(NdefRecord ndefRecord) { 15 // 16 if (ndefRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) { 17 return null; 18 } 19 20 if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) { 21 return null; 22 } 23 24 try { 25 26 byte[] palyload = ndefRecord.getPayload(); 27 // 根据最高位判断字符编码 28 String textEncoding = ((palyload[0] & 0x80) == 0) ? "UTF-8" 29 : "UTF-16"; 30 // 根据第六位获得语言编码长度 31 int languageCodeLength = palyload[0] & 0x3f; 32 // 获得语言编码 33 String languageCod = new String(palyload, 1, languageCodeLength, 34 "US-ASCII"); 35 36 String text = new String(palyload, languageCodeLength + 1, 37 palyload.length - languageCodeLength - 1, textEncoding); 38 39 return new TextRecord(text); 40 41 } catch (Exception e) { 42 // TODO: handle exception 43 throw new IllegalArgumentException(); 44 } 45 46 } 47 }