博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读取NfcA格式数据
阅读量:5100 次
发布时间:2019-06-13

本文共 5939 字,大约阅读时间需要 19 分钟。

   如何读取数据?

   Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

   NfcA nfcA = NfcA.get(tag);

   nfcA.connect();

   byte[] SELECT = {(byte) 0x30, (byte) 0x05};//我读取的NFC卡片使用的是NTAG216的芯片,这里的指令参数是根据其datasheet的说明写的。

   byte[] result = nfcA.transceive(SELECT);//这里会返回16个字节的数据,根据芯片不同会有差异

   该芯片的快速读写命令是0x3A,可以指定读取页数范围,在使用快速读写命令时,发现读取范围超过70字节android就会报错,所以使用了每次最多读取64字节的方式。

 

   

package com.yorkg.android.nfc;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Arrays;import android.app.Activity;import android.app.AlertDialog;import android.app.PendingIntent;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.nfc.NfcAdapter;import android.nfc.Tag;import android.nfc.tech.MifareClassic;import android.nfc.tech.NfcA;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import com.yorkg.android.nfc.dataobject.mifare.MifareBlock;import com.yorkg.android.nfc.dataobject.mifare.MifareClassCard;import com.yorkg.android.nfc.dataobject.mifare.MifareSector;import com.yorkg.android.nfc.util.Converter;public class MyFirstNFCDemoActivity extends Activity {	/** Called when the activity is first created. */		private Button clearBtn = null;		private EditText sealCompanyName = null;	private EditText sealName = null;	private EditText sealNumber = null;	private EditText sealTaxId = null;	private EditText sealCode = null;	private EditText sealMaterial = null;	private EditText sealSize = null;		private EditText companyMadedName = null;	private EditText companyMadedTime = null;		private EditText companyCheckedName = null;	private NfcAdapter mAdapter;	private PendingIntent mPendingIntent;	private IntentFilter[] mFilters;	private String[][] mTechLists;		private static final int AUTH = 1;	private static final int EMPTY_BLOCK_0 = 2;	private static final int EMPTY_BLOCK_1 = 3;	private static final int NETWORK = 4;	private static final int NFC_OFF = 5;	private static final int NFC_TYPE_ERROR = 6;	private static final String TAG = "NfcDemo";		private static boolean READ_LOCK = false;			private void initView(){		sealCompanyName = (EditText) this.findViewById(R.id.edittext_seal_company_name);		sealName = (EditText) this.findViewById(R.id.edittext_seal_name);		sealNumber = (EditText) this.findViewById(R.id.edittext_seal_number);		sealTaxId = (EditText) this.findViewById(R.id.edittext_tax_id);		sealCode = (EditText) this.findViewById(R.id.edittext_code);		sealMaterial = (EditText) this.findViewById(R.id.edittext_seal_material);		sealSize = (EditText) this.findViewById(R.id.edittext_seal_size);				companyMadedName = (EditText) this.findViewById(R.id.edittext_company_maded_name);		companyMadedTime = (EditText) this.findViewById(R.id.edittext_company_maded_time);				companyCheckedName = (EditText) this.findViewById(R.id.edittext_company_checked_name);				clearBtn = (Button) this.findViewById(R.id.clear_btn);		clearBtn.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub				cleanData();			}		});	}		//清除数据信息	private void cleanData(){		sealCompanyName.setText("");		sealName.setText("");		sealNumber.setText("");		sealTaxId.setText("");		sealCode.setText("");		sealMaterial.setText("");		sealSize.setText("");				companyMadedName.setText("");		companyMadedTime.setText("");		companyCheckedName.setText("");	}			@Override	public void onCreate(Bundle savedState) {		super.onCreate(savedState);		requestWindowFeature(Window.FEATURE_NO_TITLE);		setContentView(R.layout.main);					initView();				mAdapter = NfcAdapter.getDefaultAdapter(this);		mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);		// Setup an intent filter for all MIME based dispatches		IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);//		try {//			ndef.addDataType("*/*");//		} catch (MalformedMimeTypeException e) {//			throw new RuntimeException("fail", e);//		}		mFilters = new IntentFilter[] { ndef, };		mTechLists = new String[][] { new String[] { MifareClassic.class				.getName() } , new String[] {NfcA.class.getName()}};		//得到是否检测到ACTION_TECH_DISCOVERED触发  		if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {  			//处理该intent  			resolveIntentNfcA(getIntent());		} 	}	@Override	public void onResume() {		super.onResume();				if (mAdapter!=null && (!mAdapter.isEnabled())) {  			showAlert(NFC_OFF, getString(R.string.error5));		} 				if (mAdapter!=null) {			mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,					mTechLists);		}					}	@Override	public void onNewIntent(Intent intent) {		resolveIntentNfcA(intent);	}	@Override	public void onPause() {		super.onPause();		if (mAdapter!=null){			mAdapter.disableForegroundDispatch(this);		}	}	void resolveIntentNfcA(Intent intent){		if (READ_LOCK==false){			READ_LOCK = true;			Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);			if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()))			{			    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  			    MyLog.i(TAG, Arrays.toString(tagFromIntent.getTechList()));			    			    try			    {			    	NfcA nfcA = NfcA.get(tag);			    	nfcA.connect();			        byte[] SELECT = { 			        	(byte) 0x30,			        	(byte) 0x05,			        };			        byte[] result = nfcA.transceive(SELECT);			        int data_len = ((result[0]&0x0f)<<8)+((result[1]&0xff));			        MyLog.i(TAG, "是否已写入数据"+result[0]+",写入数据长度:"+data_len);			        byte[] buf_res = new byte[data_len/2+4];			        if (result[0]!=0 && data_len!=0){			        	int count = data_len/2/64;			        	int i = 0;			        	for (i=0; i

 

  

  

转载于:https://www.cnblogs.com/suxiaoqi/p/4442268.html

你可能感兴趣的文章
Hmailserver搭建邮件服务器
查看>>
django之多表查询-2
查看>>
快速幂
查看>>
改善C#公共程序类库质量的10种方法
查看>>
AIO 开始不定时的抛异常: java.io.IOException: 指定的网络名不再可用
查看>>
MyBaits动态sql语句
查看>>
HDU4405(期望DP)
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>
[Data Structure & Algorithm] 有向无环图的拓扑排序及关键路径
查看>>
cassandra vs mongo (1)存储引擎
查看>>
Visual Studio基于CMake配置opencv1.0.0、opencv2.2
查看>>
Vue音乐项目笔记(三)
查看>>
遍历Map对象
查看>>