简体   繁体   中英

Android NFC: Intent when detect any type of NFC

I'm triying to detect when a NFC tag is readed. This code works, but it doesn't for a Mifare Classic tag. 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. 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?

Thanks.

Solved! Documentation says:

enter link description here

filters the IntentFilters to override dispatching for, or null to always dispatch

So, passing null in filters parameter in method enableForegroundDispatch of NfcAdapter it will detect all type of NFC tags.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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