简体   繁体   English

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

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

I'm triying to detect when a NFC tag is readed. 我试图检测何时读取了NFC标签。 This code works, but it doesn't for a Mifare Classic tag. 该代码有效,但不适用于Mifare Classic标签。 Mi smartphone is not compatible with this tech but I have installed an app (NFC Tools) that is able to detect when a Mifare Classic tag is near. Mi智能手机与此技术不兼容,但是我安装了一个应用程序(NFC工具),该应用程序可以检测Mifare Classic标签何时在附近。 It can't read the stored data, but I don't need it. 它无法读取存储的数据,但我不需要它。 I just need to detect the event. 我只需要检测事件。 This thrid-party app is able to do it, so I'm sure that is possible but I don't know how. 这个第三方应用程序可以做到,所以我敢肯定这是可能的,但我不知道该怎么做。

This is my code: 这是我的代码:

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[][]{};
        }
    }

What have I to do to detect Mifare Classic tags? 如何检测Mifare Classic标签?

Thanks. 谢谢。

Solved! 解决了! Documentation says: 文档说:

enter link description here 在此处输入链接说明

filters the IntentFilters to override dispatching for, or null to always dispatch 过滤IntentFilters以覆盖其调度,或者将null始终调度

So, passing null in filters parameter in method enableForegroundDispatch of NfcAdapter it will detect all type of NFC tags. 因此,在enableForegroundDispatchNfcAdapter方法的filter参数中传递null ,它将检测所有类型的NFC标签。

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

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

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