【发布时间】: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 我在问题中添加了清单文件代码。我想在应用程序打开或关闭时获得扫描标签的事件,这无关紧要。两种情况都可以报名吗?