首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得所有NFC类型的事件?

如何获得所有NFC类型的事件?
EN

Stack Overflow用户
提问于 2013-12-23 11:13:11
回答 1查看 2.6K关注 0票数 2

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

我复制了教程中的代码,我想我理解它是如何工作的。问题是它不适用于我必须用它进行测试的卡(不调用onNewIntent方法,而不是教程告诉我的内容)。这张卡可以工作(其他应用程序也能很好地把它捡起来),所以我想问题是它与教程中使用的不同类型。因此,我将教程中提到的xml更改为:

代码语言:javascript
复制
<?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>

这是我的清单文件:

代码语言:javascript
复制
<?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经典1K

ATQA:

0004

SAK:

08

目标技术类:

代码语言:javascript
复制
android.nfc.tech.MifareClassic,  
android.nfc.tech.NfcA,  
android.nfc.tech.NdefFormatable

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

最好的解决方案是可以读取所有的卡片,但是现在可以读这张卡片的东西是可以的。如果有人能解释一下新的nfc芯片是如何工作的,我也会很感激。我们可能会支持一个以上的类型后,这一个工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-23 14:38:49

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

AndroidManifest.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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);
    }
}

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20742350

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档