繁体   English   中英

使用NFC读取RFID标签

[英]Read RFID tags using NFC

我正在开发一个需要读取RFID标签的应用程序。 您能告诉我,Android设备支持哪些RFID标签,我是否需要额外的硬件或某些东西来读取RFID标签,或者只能通过NFC来读取。 我在上面进行研发,我知道可以通过NFC读取RFID标签,并且我在开发人员网站上对代码进行了整数化处理,但是我无法读取RFID标签(用于出席的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;
    }
}

您可以阅读此主题使用Android手机读取RFID

NFC家伙评论:

您可以将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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM