基于mui框架读取nfc数据_10、NFC技术:读写NFC标签中的文本数据

1 importjava.nio.charset.Charset;2 importjava.util.Locale;3

4 importandroid.app.Activity;5 importandroid.content.Intent;6 importandroid.nfc.NdefMessage;7 importandroid.nfc.NdefRecord;8 importandroid.nfc.NfcAdapter;9 importandroid.nfc.Tag;10 importandroid.nfc.tech.Ndef;11 importandroid.os.Bundle;12 importandroid.view.View;13 importandroid.widget.TextView;14

15 /**

16 * 主 Activity17 * 读写NFC标签的纯文本数据18 *@authordr19 *20 */

21 public class ReadWriteTextMainActivity extendsActivity {22 privateTextView mInputText;23 privateString mText;24

25 @Override26 protected voidonCreate(Bundle savedInstanceState) {27 //TODO Auto-generated method stub

28 super.onCreate(savedInstanceState);29 setContentView(R.layout.activity_read_write_text_main);30

31 mInputText =(TextView) findViewById(R.id.textview_input_text);32 }33

34 protected void onActivityResult(int requestCode, intresultCode, Intent data) {35 if (requestCode == 1 && resultCode == 1) {36 mText = data.getStringExtra("text");37 mInputText.setText(mText);38 }39 }40

41 public voidonNewIntent(Intent intent) {42 //read nfc text

43 if (mText == null) {44 //显示NFC标签内容

45 Intent myIntent = new Intent(this, ShowNFCTagContentActivity.class);46 myIntent.putExtras(intent);47 myIntent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED);48 startActivity(myIntent);49

50 } else { //write nfc text

51 Tag tag =intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);52 NdefMessage ndefMessage = newNdefMessage(53 newNdefRecord[] { createTextRecord(mText) });54 writeTag(ndefMessage, tag);55 }56 }57

58 /**

59 * 将纯文本转换成NdefRecord对象。60 *@paramtext61 *@return

62 */

63 publicNdefRecord createTextRecord(String text) {64 //得到生成语言编码字节数组

65 byte[] langBytes =Locale.CHINA.getLanguage().getBytes(66 Charset.forName("US-ASCII"));67 Charset utfEncoding = Charset.forName("UTF-8");68 byte[] textBytes =text.getBytes(utfEncoding);69 int utfBit = 0;70 //状态字节

71 char status = (char) (utfBit +langBytes.length);72

73 byte[] data = new byte[1 + langBytes.length +textBytes.length];74 data[0] = (byte) status;75 //数组拷贝

76 System.arraycopy(langBytes, 0, data, 1, langBytes.length);77 System.arraycopy(textBytes, 0, data, 1 +langBytes.length,78 textBytes.length);79

80 NdefRecord ndefRecord = newNdefRecord(NdefRecord.TNF_WELL_KNOWN,81 NdefRecord.RTD_TEXT, new byte[0], data);82 returnndefRecord;83 }84

85 booleanwriteTag(NdefMessage ndefMessage, Tag tag) {86 try{87 Ndef ndef =Ndef.get(tag);88 ndef.connect();89 ndef.writeNdefMessage(ndefMessage);90 return true;91 } catch(Exception e) {92 //TODO: handle exception

93 }94 return false;95 }96

97 //向NFC标签写入文本

98 public voidonClick_InputText(View view) {99 Intent intent = new Intent(this, InputTextActivity.class);100 startActivityForResult(intent, 1);101 }102

103 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值