我正在开发一个应用程序,我需要读取RFID标签。请您告诉我,Android设备支持哪些RFID标签,是否需要额外的硬件或其他东西来读取RFID标签,或者只能通过NFC实现。我做R& D,我知道可以通过NFC读取RFID标签,我使用开发者网站整理代码,但我无法读取RFID标签(用于Attendence的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;
}
}
答案 0 :(得分:0)
您可以阅读此帖子Reading RFID with Android phones
NFC guy评论道:
您可以将NFC标签视为RFID标签的特例。更具体地说,Android支持ISO 14443和ISO 15693兼容的RFID标签。
以下代码用于阅读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,如果应用程序已打开,则每次检测到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;