【问题标题】:Read RFID tags using NFC使用 NFC 读取 RFID 标签
【发布时间】:2015-12-25 15:55:44
【问题描述】:

我正在开发一个需要读取 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 tags nfc rfid


    【解决方案1】:

    你可以阅读这个帖子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>
    

    那么你只需要适配你的activity,如果你的app关闭了,onCreate就会被调用,如果app是打开的,那么每次检测到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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多