无网不进  
软硬件开发

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

 

源码:

 

[java] view plain copy
 
  1. package com.example.writefnc;  
  2.   
  3. import java.io.IOException;  
  4. import java.nio.charset.Charset;  
  5. import java.util.Arrays;  
  6.   
  7. import android.support.v7.app.ActionBarActivity;  
  8. import android.app.PendingIntent;  
  9. import android.content.Intent;  
  10. import android.nfc.NfcAdapter;  
  11. import android.nfc.Tag;  
  12. import android.nfc.tech.MifareUltralight;  
  13. import android.nfc.tech.Ndef;  
  14. import android.os.Bundle;  
  15. import android.view.Menu;  
  16. import android.view.MenuItem;  
  17. import android.widget.CheckBox;  
  18. import android.widget.TextView;  
  19. import android.widget.Toast;  
  20.   
  21.   
  22. public class MainActivity extends ActionBarActivity {  
  23.       
  24.     private NfcAdapter nfcAdapter;  
  25.     private PendingIntent intent;  
  26.     CheckBox checkBox1;  
  27.     TextView textView1;  
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.activity_main);  
  32.         checkBox1=(CheckBox) findViewById(R.id.checkBox1);  
  33.         textView1=(TextView) findViewById(R.id.textView1);  
  34.         nfcAdapter=NfcAdapter.getDefaultAdapter(this);  
  35.         intent=PendingIntent.getActivity(this, 0, new Intent(this,getClass()), 0);  
  36.     }  
  37.      @Override  
  38.     protected void onResume() {  
  39.         // TODO Auto-generated method stub  
  40.         super.onResume();  
  41.         nfcAdapter.enableForegroundDispatch(this, intent, null, null);  
  42.     }  
  43.      @Override  
  44.     protected void onPause() {  
  45.         // TODO Auto-generated method stub  
  46.         super.onPause();  
  47.         nfcAdapter.disableForegroundDispatch(this);  
  48.     }  
  49.     @Override  
  50.     public boolean onCreateOptionsMenu(Menu menu) {  
  51.         // Inflate the menu; this adds items to the action bar if it is present.  
  52.         getMenuInflater().inflate(R.menu.main, menu);  
  53.         return true;  
  54.     }  
  55.   
  56.     @Override  
  57.     public boolean onOptionsItemSelected(MenuItem item) {  
  58.         // Handle action bar item clicks here. The action bar will  
  59.         // automatically handle clicks on the Home/Up button, so long  
  60.         // as you specify a parent activity in AndroidManifest.xml.  
  61.         int id = item.getItemId();  
  62.         if (id == R.id.action_settings) {  
  63.             return true;  
  64.         }  
  65.         return super.onOptionsItemSelected(item);  
  66.     }  
  67.     @Override  
  68.     protected void onNewIntent(Intent intent) {  
  69.         // TODO Auto-generated method stub  
  70.         super.onNewIntent(intent);  
  71.         Tag tag=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
  72.         Toast.makeText(this, "获取tag", Toast.LENGTH_SHORT).show();  
  73.         if(checkBox1.isChecked()){  
  74.             Toast.makeText(this, "选择读取", Toast.LENGTH_SHORT).show();  
  75.             textView1.setText(readTag(tag));  
  76.         }else{  
  77.             Toast.makeText(this, "选择写入", Toast.LENGTH_SHORT).show();  
  78.             writeTag(tag);  
  79.         }  
  80.           
  81.     }  
  82.     public void writeTag(Tag tag){  
  83.         String[] techlist=tag.getTechList();  
  84.         if(Arrays.toString(techlist).contains("MifareUltralight")){  
  85.             MifareUltralight mifareUltralight=MifareUltralight.get(tag);  
  86.             try {  
  87.                 mifareUltralight.connect();  
  88.                 Toast.makeText(this, "开始写", Toast.LENGTH_SHORT).show();  
  89.                 mifareUltralight.writePage(4, "你好".getBytes(Charset.forName("GB2312")));  
  90.                 mifareUltralight.writePage(5, "中国".getBytes(Charset.forName("GB2312")));  
  91.                 mifareUltralight.writePage(6, "人们".getBytes(Charset.forName("GB2312")));  
  92.                 mifareUltralight.writePage(7, "百姓".getBytes(Charset.forName("GB2312")));            mifareUltralight.writePage(7, "百姓".getBytes(Charset.forName("GB2312")));  
  93.               
  94.   
  95.                 Toast.makeText(this, "写入完成", Toast.LENGTH_SHORT).show();  
  96.             } catch (IOException e) {  
  97.                 // TODO Auto-generated catch block  
  98.                 e.printStackTrace();  
  99.             }finally{  
  100.                 try {  
  101.                     mifareUltralight.close();  
  102.                 } catch (IOException e) {  
  103.                     // TODO Auto-generated catch block  
  104.                     e.printStackTrace();  
  105.                 }  
  106.             }  
  107.         }else{  
  108.             Toast.makeText(this, "不是MifareUltralightle类型", Toast.LENGTH_SHORT).show();  
  109.         }  
  110.           
  111.     }  
  112.     public String readTag(Tag tag){  
  113.         String[] techlist=tag.getTechList();  
  114.         if(Arrays.toString(techlist).contains("MifareUltralight")){  
  115.         MifareUltralight mifareUltralight=MifareUltralight.get(tag);  
  116.         Toast.makeText(this, "开始读", Toast.LENGTH_SHORT).show();  
  117.         try {  
  118.             mifareUltralight.connect();  
  119.               
  120.             byte[] data=mifareUltralight.readPages(4);  
  121.             return new String(data,Charset.forName("GB2312"));  
  122.               
  123.         } catch (Exception e) {  
  124.             // TODO: handle exception  
  125.         }finally{  
  126.             try {  
  127.                 mifareUltralight.close();  
  128.             } catch (IOException e) {  
  129.                 // TODO Auto-generated catch block  
  130.                 e.printStackTrace();  
  131.             }  
  132.         }  
  133.           
  134.         }else{  
  135.             Toast.makeText(this, "不是MifareUltralightle类型", Toast.LENGTH_SHORT).show();  
  136.         }  
  137.         return null;  
  138.     }  
  139.           
  140. }  
posted on 2017-12-07 09:54  无网不进  阅读(965)  评论(0)    收藏  举报