Android 使用NFC通过标签协议栈读写标签数据

来源:互联网 发布:core js 编辑:程序博客网 时间:2024/06/05 02:22

欢迎转载,转载请注明出处:http://blog.sina.com.cn/s/blog_821e2bb10101fu1h.html   

    前言:最近项目需要向标签里读取与写入数据,于是研究了下Android的 NFC技术,刚开始在网上找资料,感觉不是很复杂,但后来发现项目操作的标签格式不是NDEF数据格式类型的,要用原协议栈进行读写,这里以15693协议的标签为例。

 

源码:

import java.io.IOException;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.TagLostException;
import android.nfc.tech.NfcV;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class mainActivity extends Activity {
  private NfcAdapter mAdapter;
  private PendingIntent mPendingIntent;
  private IntentFilter[] mFilters;
  private String[][] mTechLists;
  private TextView mText;
  private int mCount = 0;
  private NfcV nfcv=null;
  @Override
  public void onCreate(Bundle savedState) {
  super.onCreate(savedState);
  
  setContentView(R.layout.main);
  mText = (TextView) findViewById(R.id.TextView);
  mText.setText("扫描标签");
  
  mAdapter = NfcAdapter.getDefaultAdapter(this);
  mPendingIntent = PendingIntent.getActivity(this, 0,
  new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  
  IntentFilter nfcv= new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
  mFilters = new IntentFilter[] { nfcv};  
  mTechLists  = new String[][] { new String[] {NfcV.class.getName()} };
  }
  
  @Override
  public void onResume() {
  super.onResume();
  mAdapter.enableForegroundDispatch(this,mPendingIntent, mFilters, mTechLists);
  }
  
  @Override
  public void onNewIntent(Intent intent) {
//   Log.d("intent", "intent action="+intent.getAction());
   if(intent.getAction().trim().equals("android.nfc.action.TECH_DISCOVERED")){
    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//    Log.d("intent", "intent tag="+NfcAdapter.EXTRA_TAG+", "+NfcAdapter.EXTRA_ID);
    nfcv=NfcV.get(tagFromIntent);
    byte[] UID=tagFromIntent.getId();
    String[] techlist=tagFromIntent.getTechList();
    StringBuilder stringbuilder1=new StringBuilder();
    for(int i=0;i
     stringbuilder1.append(techlist[i]+"\n");
    }
    int length=UID.length;
    byte[] UID_zan=new byte[8];
    byte[] uidShow=new byte[8];
    for(int i=0;i
     UID_zan[i]=UID[i];
    }
    for(int i=0;i
     uidShow[length-i-1]=UID_zan[i];
    }
   mText.setText(Convert.bytesToHexString(uidShow)+"\n"+UID.length+"\n"+stringbuilder1.toString());
   
//    //读数据
    try {
     if(nfcv.isConnected())
      nfcv.close();
        nfcv.connect();
     byte[] command=new byte[11];
     byte[] responsebyte = null;
     command[0]=(byte)0x22;     //标志位  
     command[1]=(byte)0x20;     //命令位
     System.arraycopy(UID, 0, command, 2, UID.length);
     command[10]=(byte)0x1;
     try{
      responsebyte=nfcv.transceive(command);
     }catch(TagLostException e){
      e.printStackTrace();
    }
     if(responsebyte!=null){
      String data=Convert.bytesToHexString(responsebyte);
      Log.d("intent", "intent response="+Convert.toStringHex(data.substring(2, data.length())));
     }
     else
      Log.d("intent", "intent response is null");
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
   //写数据
//    try {
//     if(nfcv.isConnected())
//      nfcv.close();
//     nfcv.connect();
//     byte[] command=new byte[15];
//     byte[] response=null;
//     command[0]=(byte)0x22;
//     command[1]=(byte)0x21;
//     System.arraycopy(UID, 0, command, 2, UID.length);
//     command[10]=(byte)0x1;
//     System.arraycopy("jnmu".getBytes(), 0,command, 11, 4);
//     response=nfcv.transceive(command);
//     if(response==null)
//      Log.d("intent", "intent response is null");
//     else
//      Log.d("intent", "intent response="+Convert.bytesToHexString(response));
//    } catch (IOException e) {
//     // TODO Auto-generated catch block
//     e.printStackTrace();
//    }
   
   }
  }
  
  @Override
  public void onPause() {
  super.onPause();
  mAdapter.disableForegroundDispatch(this);
  }
 
 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  if(nfcv!=null&&nfcv.isConnected())
   try {
    nfcv.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  super.onDestroy();
 }
}

 

代码里的onNewIntent()方法包含了读取与写入第一块的代码。因为要用原协议栈所以这里使用transceive()方法,看看代码还是挺简单的,主要是要弄懂15693的协议栈是怎么读写标签的,可以参考:15693协议中文版,这个在CSDN就有资源,可以自行搜索下载。

提供个下载地址:http://download.csdn.net/detail/john610/4127139#comment

0 0