如何使用NFC动作

use*_*121 4 android nfc intentfilter broadcastreceiver android-broadcast

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

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

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");
    }
};
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Mic*_*and 5

这样做的目的android.nfc.action.TAG_DISCOVERED,正如所有NFC意图,是一个活动的意图,而不是广播意图.根本不可能为它注册广播接收器.您可以做的是注册活动以接收NFC意图.这可以通过清单,NFC前台调度系统或Android 4.4+通过NFC阅读器模式API完成.

1.清单

根据标签上的数据,您要么注册NDEF_DISCOVERED意图(如果标签上有NDEF结构化数据),要么注册TECH_DISCOVERED意图(如果您只想监听某些标签技术而不管数据是什么)标签).您通常不希望注册TAG_DISCOVERED意图过滤器,因为当通过AndroidManifest.xml使用时,这仅仅意味着作为回退机制(捕获未被任何其他应用程序处理的事件).

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

<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>
Run Code Online (Sandbox Code Playgroud)

如果您的标记不包含任何特定数据且可能是任何标记技术,则可以使用以下intent过滤器:

<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>
Run Code Online (Sandbox Code Playgroud)

对于这个意图过滤器的工作,你将需要ASLO XML资源xml/nfc_tech_filter.xml内部res/的应用程序的目录.如果技术过滤器应该只匹配任何标记,那么该文件将包含:

<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>
Run Code Online (Sandbox Code Playgroud)

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

@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
    }
}
Run Code Online (Sandbox Code Playgroud)

2.前景调度系统

如果您只对在前台可见活动时接收NFC发现意图感兴趣,最好使用NFC前台调度系统,而不是通过清单注册接收NFC事件.您可以在以下期间注册活动来执行此操作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);
}
Run Code Online (Sandbox Code Playgroud)

您还必须确保在以下期间取消注册onPause():

@Override
public void onPause() {
    super.onPause();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableForegroundDispatch(this);
}
Run Code Online (Sandbox Code Playgroud)

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

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

3. Reader Mode API

如果您只对检测NFC标签感兴趣,并且只有当您的活动在前台可见且您只需要定位Android 4.4+时,最好的方法可能是使用NFC阅读器模式API.您可以在以下期间注册活动来执行此操作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);
}
Run Code Online (Sandbox Code Playgroud)

您还应确保在以下期间取消注册onStop():

@Override
public void onStop() {
    super.onStop();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableReaderMode(this);
}
Run Code Online (Sandbox Code Playgroud)

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