Android NFC无法正常运行

时间:2017-02-03 11:45:13

标签: android nfc

我正在尝试通过NFC阅读信用卡。我尝试了10种不同的设备(所有这些都是Samsung Note 3和Android 5.0版)。其中一些工作正常,其他人则不然。但是其他一些NFC阅读器应用程序正在适当地运行。我在我的测试中使用相同的apk,设备和Android版本。所以我不知道为什么会出现这个问题。有没有有效的方法来捕捉NFC行动?我正在使用下面的代码来捕捉NFC动作:

NFCReadActivity - onCreate:

nfcAdapter = NfcAdapter.getDefaultAdapter(this);
            nfcPendingIntent = PendingIntent.getActivity(this, 0,
                    new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

            //endregion

            //region NFC Control
            if (nfcAdapter == null) {
                Toast.makeText(this, "NFC not supported..", Toast.LENGTH_LONG).show();
                return;
            } else if (!nfcAdapter.isEnabled()) {
                Toast.makeText(this, "NFC is closed..", Toast.LENGTH_LONG).show();
            } else {
            }
            //endregion


            onNewIntent(getIntent());

NFCReadActivity - onNewIntent:

 @Override    
    void onNewIntent(Intent intent) {
                if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
                    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                    setCardInfosFromNFC(tag);
                } else if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
                    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                    setCardInfosFromNFC(tag);
                } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
                    // In case we would still use the Tech Discovered Intent
                    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                    String[] techList = tag.getTechList();
                    String searchedTech = Ndef.class.getName();

                    for (String tech : techList) {
                        if (searchedTech.equals(tech)) {
                            setCardInfosFromNFC(tag);
                            break;
                        }
                    }
                }


            }

清单:

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

    <uses-feature android:name="android.hardware.nfc" android:required="true" />

<activity
            android:name=".Activity.ReadNFCActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED"/>
                <meta-data android:name="android.nfc.action.TECH_DISCOVERED" />
            </intent-filter>

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

        </activity>

感谢您的任何建议。

0 个答案:

没有答案