【问题标题】:How can I get events for all NFC types?如何获取所有 NFC 类型的事件?
【发布时间】:2013-12-23 11:13:11
【问题描述】:

我目前有一个应用程序,用户必须登录才能使用它。在不久的将来,我们可能会支持 NFC 芯片无需输入用户名和密码即可登录。我完全不知道扫描 NFC 芯片的工作原理,所以我查阅了一些教程并找到了this one。

我从教程中复制了代码,我想我理解它是如何工作的。问题是它不适用于我必须测试的卡(与教程告诉我的相反,未调用 onNewIntent 方法)。该卡可以正常工作(其他应用程序可以正常使用),所以我想问题在于它与教程中使用的类型不同。于是我把教程中提到的xml改成如下:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcB</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcF</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcV</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NdefFormatable</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareClassic</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nl.boomerweb.nfc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="nl.boomerweb.nfc.MainActivity"
            android:label="@string/app_name" >
            <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.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.NDEF_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
    </application>

</manifest>

我认为这不仅会扫描 Ndef 类型的芯片,还会扫描所有其他芯片。但是当我将卡放在手机上时,我的应用程序仍然没有响应。

这些是我使用 NFC 扫描仪应用检索到的卡详细信息:

射频技术:
A 型(ISO/IEC 14443 A 型)

标签类型:
Mifare Classic 1K

ATQA:
0004

SAK:
08

目标技术类(Android):

android.nfc.tech.MifareClassic,  
android.nfc.tech.NfcA,  
android.nfc.tech.NdefFormatable

我正在使用 android 4.1.2 的三星 Galaxy SIII 上进行测试。我在设备上启用了 nfc 和 sBeam,另一个扫描 nfc 芯片的应用程序运行良好,所以问题一定出在我的代码中。我创建了一个单独的项目来测试它,它只包含上面提到的教程最后部分中看到的代码(所以只有 MainActivity、清单和 nfc_tech_filter.xml)。

最好的解决方案是可以读取所有卡片,但现在可以读取这张卡片的东西就可以了。如果有人能解释添加新型 nfc 芯片的工作原理,我也将不胜感激。在这一种工作之后,我们可能会支持不止一种类型。

【问题讨论】:

  • 您在清单中使用了哪些意图过滤器?此外,您是希望应用程序在扫描标签时启动,还是只想在您的应用程序可见时接收标签发现事件?
  • @MichaelRoland 我在问题中添加了清单文件代码。我想在应用程序打开或关闭时获得扫描标签的事件,这无关紧要。两种情况都可以报名吗?

标签: java android nfc


【解决方案1】:

我找到了适合我的解决方案。教程实际上有两个问题,一个是我的应用程序根本没有响应卡,另一个是在我解决了第一个问题之后,即应用程序在它已经运行时重新打开了。这是我用来解决这两个问题的所有代码:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.nfc.MainActivity"
            android:label="@string/app_name" >
            <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" />
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
            <meta-data
                android:name="android.nfc.action.NDEF_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
            <meta-data
                android:name="android.nfc.action.TAG_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
    </application>

</manifest>

nfc_tech_filter.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcB</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcF</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcV</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NdefFormatable</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareClassic</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

MainActivity.java

package com.example.nfc;

import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.widget.Toast;

public class MainActivity extends Activity {
    public static final String MIME_TEXT_PLAIN = "text/plain";
    public static final String TAG = "NfcDemo";
    private NfcAdapter mNfcAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            // Stop here, we definitely need NFC
            Toast.makeText(this, "Dit apparaat ondersteund geen NFC.", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        if (!mNfcAdapter.isEnabled()) {
            Toast.makeText(this, "NFC staat niet aan", Toast.LENGTH_SHORT).show();
        }
        handleIntent(getIntent());
    }
    @Override
    protected void onResume() {
        super.onResume();
        setupForegroundDispatch(this, mNfcAdapter);
    }
    @Override
    protected void onPause() {
        stopForegroundDispatch(this, mNfcAdapter);
        super.onPause();
    }
    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }
    private void handleIntent(Intent intent) {
        if (intent != null && intent.getAction().contains("android.nfc")) {
            //if we are here, we captured an nfc event
        }
    }
    public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
        final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

        String[][] techList = new String[][]{};
        adapter.enableForegroundDispatch(activity, pendingIntent, null, techList);
    }
    public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
        adapter.disableForegroundDispatch(activity);
    }
}

如您所见,我已将所有可能的操作添加到清单中。为了确保前台调度工作,我从教程中删除了过滤器。当更多事件被触发时,这当然会产生问题,但我想我以后总是可以过滤它们。不是最优雅的解决方案,但它可以完成工作。

【讨论】:

  • 请注意,&lt;meta-data ...&gt; 部分仅适用于 TECH_DISCOVERED 意图过滤器,因此您应该删除 NDEF_DISCOVERED 和 TAG_DISCOVERED 的该部分。此外,您可以删除 &lt;action android:name="android.nfc.action.NDEF_DISCOVERED" /&gt; 意图过滤器,因为它不会由于缺少数据/类别部分而触发。此外,您应该避免使用 TAG_DISCOVERED 意图过滤器,因为它只是作为一种后备机制。
  • @MichaelRoland 感谢您的提示。假期结束后,我一定会去看看。
猜你喜欢
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多