android nfc MifareUltralight读写
生活随笔
收集整理的這篇文章主要介紹了
android nfc MifareUltralight读写
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MifareUltralight :是techlist中的一種類型,共16頁,每頁4個字節,也就是一頁只能存2個漢字或者4個字母,0到3頁 ?也就是前四頁用來存卡片信息的,所以往MifareUltralight寫數據,只能從第四頁開始寫,總共可以寫入除了自帶的卡片信息,可以寫入4到15頁數據,也就是56個字節,折算出來就是最多可寫28個漢字或者56個字符。
找MifareUltralight 卡片靠近手機攝像頭附近 ?即可測試 ?demo地址
http://download.csdn.net/detail/u012303938/9282985
源碼:
package com.example.writefnc;import java.io.IOException; import java.nio.charset.Charset; import java.util.Arrays;import android.support.v7.app.ActionBarActivity; import android.app.PendingIntent; import android.content.Intent; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.MifareUltralight; import android.nfc.tech.Ndef; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends ActionBarActivity {private NfcAdapter nfcAdapter;private PendingIntent intent;CheckBox checkBox1;TextView textView1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);checkBox1=(CheckBox) findViewById(R.id.checkBox1);textView1=(TextView) findViewById(R.id.textView1);nfcAdapter=NfcAdapter.getDefaultAdapter(this);intent=PendingIntent.getActivity(this, 0, new Intent(this,getClass()), 0);}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();nfcAdapter.enableForegroundDispatch(this, intent, null, null);}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();nfcAdapter.disableForegroundDispatch(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}@Overrideprotected void onNewIntent(Intent intent) {// TODO Auto-generated method stubsuper.onNewIntent(intent);Tag tag=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);Toast.makeText(this, "獲取tag", Toast.LENGTH_SHORT).show();if(checkBox1.isChecked()){Toast.makeText(this, "選擇讀取", Toast.LENGTH_SHORT).show();textView1.setText(readTag(tag));}else{Toast.makeText(this, "選擇寫入", Toast.LENGTH_SHORT).show();writeTag(tag);}}public void writeTag(Tag tag){String[] techlist=tag.getTechList();if(Arrays.toString(techlist).contains("MifareUltralight")){MifareUltralight mifareUltralight=MifareUltralight.get(tag);try {mifareUltralight.connect();Toast.makeText(this, "開始寫", Toast.LENGTH_SHORT).show();mifareUltralight.writePage(4, "你好".getBytes(Charset.forName("GB2312")));mifareUltralight.writePage(5, "中國".getBytes(Charset.forName("GB2312")));mifareUltralight.writePage(6, "人們".getBytes(Charset.forName("GB2312")));mifareUltralight.writePage(7, "百姓".getBytes(Charset.forName("GB2312"))); mifareUltralight.writePage(7, "百姓".getBytes(Charset.forName("GB2312")));Toast.makeText(this, "寫入完成", Toast.LENGTH_SHORT).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {mifareUltralight.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}else{Toast.makeText(this, "不是MifareUltralightle類型", Toast.LENGTH_SHORT).show();}}public String readTag(Tag tag){String[] techlist=tag.getTechList();if(Arrays.toString(techlist).contains("MifareUltralight")){MifareUltralight mifareUltralight=MifareUltralight.get(tag);Toast.makeText(this, "開始讀", Toast.LENGTH_SHORT).show();try {mifareUltralight.connect();byte[] data=mifareUltralight.readPages(4);return new String(data,Charset.forName("GB2312"));} catch (Exception e) {// TODO: handle exception}finally{try {mifareUltralight.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}else{Toast.makeText(this, "不是MifareUltralightle類型", Toast.LENGTH_SHORT).show();}return null;}}
總結
以上是生活随笔為你收集整理的android nfc MifareUltralight读写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库原理归档——对于数据库原理最全的总
- 下一篇: 硬盘坏道修复白皮书