繁体   English   中英

Android NFC:检测到任何类型的NFC时的意图

[英]Android NFC: Intent when detect any type of NFC

我试图检测何时读取了NFC标签。 该代码有效,但不适用于Mifare Classic标签。 Mi智能手机与此技术不兼容,但是我安装了一个应用程序(NFC工具),该应用程序可以检测Mifare Classic标签何时在附近。 它无法读取存储的数据,但我不需要它。 我只需要检测事件。 这个第三方应用程序可以做到,所以我敢肯定这是可能的,但我不知道该怎么做。

这是我的代码:

private void prepareIntent() {
        mAdapter = NfcAdapter.getDefaultAdapter(this);
        if (hasNfcCapabilities()) {
            mPendingIntent = PendingIntent.getActivity(
                    this, 0, new Intent(this, ((Object) this).getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            IntentFilter techDiscovered = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
            IntentFilter tagDiscovered = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
            IntentFilter ndefDiscovered = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
            try {
                techDiscovered.addDataType("*/*");
                tagDiscovered.addDataType("*/*");
                ndefDiscovered.addDataType("*/*");
            } catch (IntentFilter.MalformedMimeTypeException e) {
                e.printStackTrace();
            }
            mFilters = new IntentFilter[]{techDiscovered, tagDiscovered, ndefDiscovered};
            mTechLists = new String[][]{};
        }
    }

如何检测Mifare Classic标签?

谢谢。

解决了! 文档说:

在此处输入链接说明

过滤IntentFilters以覆盖其调度,或者将null始终调度

因此,在enableForegroundDispatchNfcAdapter方法的filter参数中传递null ,它将检测所有类型的NFC标签。

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, mTechLists);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM