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;
- @Override
- protected 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);
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- nfcAdapter.enableForegroundDispatch(this, intent, null, null);
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- nfcAdapter.disableForegroundDispatch(this);
- }
- @Override
- public 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;
- }
- @Override
- public 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);
- }
- @Override
- protected void onNewIntent(Intent intent) {
- // TODO Auto-generated method stub
- super.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 block
- e.printStackTrace();
- }finally{
- try {
- mifareUltralight.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.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 block
- e.printStackTrace();
- }
- }
- }else{
- Toast.makeText(this, "不是MifareUltralightle类型", Toast.LENGTH_SHORT).show();
- }
- return null;
- }
- }

浙公网安备 33010602011771号