使用NFC读取RFID标签

问题描述:

我正在研究一个需要读取RFID标签的应用程序。请告诉我,Android设备支持哪些RFID标签,是否需要额外的硬件或其他东西来读取RFID标签,或者只能通过NFC进行。我做就可以[R & d我知道的话,可以通过读取NFC RFID标签和使用我开发者网站integerated的代码,但我不能能够读取(用于考勤RFID标签)的RFID标签使用NFC读取RFID标签

public class NFCForegroundUtil { 
    private NfcAdapter nfc; 

    private Activity activity; 
    private IntentFilter intentFiltersArray[]; 
    private PendingIntent intent; 
    private String techListsArray[][]; 

    public NFCForegroundUtil(Activity activity) { 
     super(); 
     this.activity = activity; 
     nfc = NfcAdapter.getDefaultAdapter(activity.getApplicationContext()); 

     intent = PendingIntent.getActivity(activity, 0, new Intent(activity, 
       activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

     IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); 

     try { 
      ndef.addDataType("*/*"); 
     } catch (IntentFilter.MalformedMimeTypeException e) { 
      throw new RuntimeException("Unable to speciy */* Mime Type", e); 
     } 
     intentFiltersArray = new IntentFilter[] { ndef }; 

     techListsArray = new String[][] { new String[] {IsoDep.class.getName(),NfcV.class.getName(), NfcA.class.getName(), NfcB.class.getName(), NfcF.class.getName(), Ndef.class.getName(), NdefFormatable.class.getName(), MifareClassic.class.getName(), MifareUltralight.class.getName()} }; 

    } 

    public void enableForeground() 
    { 
     Log.d("demo", "Foreground NFC dispatch enabled"); 
     nfc.enableForegroundDispatch(
       activity, intent, intentFiltersArray, techListsArray); 
    } 

    public void disableForeground() 
    { 
     Log.d("demo", "Foreground NFC dispatch disabled"); 
     nfc.disableForegroundDispatch(activity); 
    } 

    public NfcAdapter getNfc() { 
     return nfc; 
    } 
} 

你可以阅读这个线程Reading RFID with Android phones

NFC guy评论:

你可以考虑NFC标签RFID标签的特殊情况。更具体地说,Android支持ISO 14443和ISO 15693兼容的RFID标签。

的folloing代码用于读取NFCv(ISO 15693) 在清单:

<uses-permission android:name="android.permission.NFC" /> 

    <activity 
     android:name=".SplashActivity"> 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

     <intent-filter> 
      <action android:name="android.nfc.action.TECH_DISCOVERED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

     <meta-data 
      android:name="android.nfc.action.TECH_DISCOVERED" 
      android:resource="@xml/nfc_tech_list"> 
     </meta-data> 
    </activity> 

nfc_tech_list.xml会是这样的:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
<tech-list> 
    <tech>android.nfc.tech.NfcV</tech>  
</tech-list> 

然后你只需要调整你的活动,如果你的应用程序关闭,onCreate被调用,如果应用程序打开,然后t每次检测到nfc时,都会调用onNewIntent。先注册NFC:

@Override 
public void onResume() { 
    super.onResume(); 
    if(checkNFCAvailability()) 
     mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    mNfcAdapter.disableForegroundDispatch(this); 

} 

private boolean checkNFCAvailability() { 
    PackageManager pm = getPackageManager(); 

    //NFC NOT AVAILABLE 
    if(!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) { 
     return false; 
    } else { 
     mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
     mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 

     //NFC AVAILABLE BUT DISABLED 
     if(Build.VERSION.SDK_INT >= 16) { 
      startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)); 
     } else { 
      startActivity(new Intent(Settings.ACTION_SETTINGS)); 
     } 

     //NFC AVAILABLE AND ENABLED 
     else {     
      mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
      IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); 
      mFilters = new IntentFilter[] {ndef,}; 
      mTechLists = new String[][] { new String[] { android.nfc.tech.NfcV.class.getName() } };  
     } 
    } 
} 


@Override 
public void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) { 

     myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 

     //Do your logic using operations like 
     NfcV nfcvTag = NfcV.get(myTag); 
     nfcvTag.connect(); 
     response = nfcvTag.transceive(new byte[]); //your address 
     nfcvTag.close(); 

    } 
} 

您必须导入

import android.nfc.Tag; 
import android.nfc.tech.NfcV; 
import android.content.pm.PackageManager; 
import android.nfc.NfcAdapter;