如何使用NFC ACTION [英] How to use NFC ACTIONS

查看:296
本文介绍了如何使用NFC ACTION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式注册接收器,以便在检测到NFC标签后得到通知.如我的代码所示,我注册了所需的操作,并以编程方式创建了广播接收器.我还在清单文件中添加了所需的权限,但问题是从未调用onReceive.

I am trying to register a receiver programmatically to get notified once an NFC tag is detected. As shown in my code I registered for the desired action and I created the broadcast receiver programmatically. I also added the required permission in the manifest file but the problem is that onReceive is never called.

请让我知道我做错了什么以及如何解决.

Please let me know what I am doing wrong and how to fix it.

IntentFilter intentFilter1 = new IntentFilter();
intentFilter1.addAction("android.nfc.action.TAG_DISCOVERED");
registerReceiver(mBCR_TAG_DISCOVERED, intentFilter1);

private BroadcastReceiver mBCR_TAG_DISCOVERED = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        mTv.setText("mBCR_TAG_DISCOVERED");
    }
};

AndroidManifest.xml :

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

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

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
</application>

</manifest>

推荐答案

与所有NFC意图一样,意图android.nfc.action.TAG_DISCOVERED是活动意图,而不是广播意图.根本无法为其注册广播接收器.您可以做的是注册一个活动来接收NFC意图.这既可以通过清单,NFC前台调度系统来完成,也可以在Android 4.4+上通过NFC读取器模式API来完成.

The intent android.nfc.action.TAG_DISCOVERED, just as all NFC intents, is an activity intent and not a broadcast intent. It's simply not possible to register a broadcast receiver for it. What you can instead do is register an activity to receive NFC intents. This can be either done through the manifest, through the NFC foreground dispatch system, or on Android 4.4+ through the NFC reader mode API.

根据标签上包含的数据,您可能想要注册NDEF_DISCOVERED意图(如果标签上有NDEF结构化数据),或者想要注册TECH_DISCOVERED意图(如果您只是想侦听某些标签技术,无论标签上的数据).您通常不希望注册TAG_DISCOVERED意向过滤器,因为通过AndroidManifest.xml使用时,这仅是一种备用机制(以捕获任何其他应用程序未处理的事件).

Depending on what data is on your tag you would either want to register for the NDEF_DISCOVERED intent (if there is NDEF structured data on the tag) or for the TECH_DISCOVERED intent (if your just want to listen for certain tag technologies regardless of the data on the tags). You typically do not want to register for the TAG_DISCOVERED intent filter since that is only meant as a fallback mechanism (to catch events not handled by any other app) when used through the AndroidManifest.xml.

例如如果您的代码包含URL http://www.example.com/,则可以使用以下意图过滤器:

E.g. if your tag contains a URL http://www.example.com/, you could use the following intent filter:

<activity android:name=".MainActivity">
    ...
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="http" android:host="www.example.com" />
    </intent-filter>
</activity>

如果您的标签不包含任何特定数据并且可以采用任何标签技术,则可以使用以下意图过滤器:

If your tag does not contain any specific data and may be of any tag technology, you could use the following intent filter:

<activity android:name=".MainActivity">
    ...
    <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_filter" />
</activity>

为使该意图过滤器正常工作,您还需要在应用程序的res/目录中包含XML资源xml/nfc_tech_filter.xml.如果技术过滤器应仅匹配任何标签,则该文件将包含以下内容:

For this intent filter to work, you will aslo need an XML resource xml/nfc_tech_filter.xml inside the res/ directory of your app. If the tech filter should match just any tag, that file would contain this:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <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.NfcBarcode</tech>
    </tech-list>
</resources>

一旦您的活动注册为接收这些事件,则可以通过onCreate()(如果您的活动是由NFC事件启动)或通过onNewIntent()(如果您的活动接收到后续的NFC)在活动中接收这些意图.打开时的意图):

Once your activity is registered to receive those events, you can receive these intents within your activity through either onCreate() (if your activity is started by an NFC event) or through onNewIntent() (if your activity receives a subsequent NFC intent while open):

@Override
public void onCreate(Bundle savedInstanceState) {

    [...]

    Intent startIntent = getIntent();
    if ((startIntent != null) &&
        (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(startIntent.getAction()) ||
        NfcAdapter.ACTION_TECH_DISCOVERED.equals(startIntent.getAction()))) {
        // TODO: process intent
    }
}

@Override
protected void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
        NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
        // TODO: process intent
    }
}

2.前景调度系统

如果仅在前台可见活动时才对接收NFC发现意图感兴趣,那么最好使用NFC前台分派系统,而不要注册通过清单来接收NFC事件.为此,您可以在onResume()期间注册您的活动:

2. Foreground Dispatch System

If you are only interested in receiving NFC discovery intents while your activity is visible in the foreground, you are better off using the NFC foreground dispatch system instead of registering to receive NFC events through the manifest. You do this by registering your activity during onResume():

@Override
public void onResume() {
    super.onResume();

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}

您还必须确保在onPause()期间注销:

You also have to make sure to unregister during onPause():

@Override
public void onPause() {
    super.onPause();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableForegroundDispatch(this);
}

然后您将通过onNewIntent()以TAG_DISCOVERED意图接收NFC事件:

You will then receive NFC events as TAG_DISCOVERED intents through onNewIntent():

@Override
public void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        // TODO: process intent
    }
}

3.读者模式API

如果您仅对检测NFC标签感兴趣,并且仅当您的活动在前台可见时才需要,而您只需要定位Android 4.4+,那么最好的方法可能是使用NFC读取器模式API.为此,您可以在onStart()期间注册活动:

3. Reader Mode API

If you are only interested in detecting NFC tags and only while your activity is visible in the foreground and you only need to target Android 4.4+, the best method is probably to use the NFC reader mode API. You do this by registering your activity during onStart():

@Override
public void onStart() {
    super.onStart();

    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.enableReaderMode(this, new NfcAdapter.ReaderCallback() {
        @Override
        public void onTagDiscovered(Tag tag) {
            // TODO: use NFC tag
        }
    }, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B | NfcAdapter.FLAG_READER_NFC_F | NfcAdapter.FLAG_READER_NFC_V | NfcAdapter.FLAG_READER_NFC_BARCODE, null);
}

您还应该确保在onStop()期间注销:

You also should make sure to unregister during onStop():

@Override
public void onStop() {
    super.onStop();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableReaderMode(this);
}

您通过onTagDiscovered(Tag tag)回调方法收到发现的标记句柄.

You receive discovered tag handles through the onTagDiscovered(Tag tag) callback method.

这篇关于如何使用NFC ACTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆