android.nfc.tech.MifareClassic Java Examples

The following examples show how to use android.nfc.tech.MifareClassic. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Tag.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static HashMap<String, Integer> getTechStringToCodeMap() {
    HashMap<String, Integer> techStringToCodeMap = new HashMap<String, Integer>();

    techStringToCodeMap.put(IsoDep.class.getName(), TagTechnology.ISO_DEP);
    techStringToCodeMap.put(MifareClassic.class.getName(), TagTechnology.MIFARE_CLASSIC);
    techStringToCodeMap.put(MifareUltralight.class.getName(), TagTechnology.MIFARE_ULTRALIGHT);
    techStringToCodeMap.put(Ndef.class.getName(), TagTechnology.NDEF);
    techStringToCodeMap.put(NdefFormatable.class.getName(), TagTechnology.NDEF_FORMATABLE);
    techStringToCodeMap.put(NfcA.class.getName(), TagTechnology.NFC_A);
    techStringToCodeMap.put(NfcB.class.getName(), TagTechnology.NFC_B);
    techStringToCodeMap.put(NfcF.class.getName(), TagTechnology.NFC_F);
    techStringToCodeMap.put(NfcV.class.getName(), TagTechnology.NFC_V);
    techStringToCodeMap.put(NfcBarcode.class.getName(), TagTechnology.NFC_BARCODE);

    return techStringToCodeMap;
}
 
Example #2
Source File: NfcDataWriteActivity.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void init() {
    Intent nfcIntent = new Intent(this, this.getClass());
    nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, nfcIntent, 0);
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("*/*");    /* Handles all MIME based dispatches.
                                   You should specify only the ones that you need. */
    } catch (IntentFilter.MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    IntentFilter[] intentFiltersArray = new IntentFilter[]{ndef,};
    String[][] techList = new String[1][1];
    techList[0][0] = MifareClassic.class.getName();
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
    disposable.add(
            nfcManager.requestProgressProcessor().onBackpressureBuffer()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            message -> binding.currentMessage.setText(message),
                            Timber::e
                    )
    );

    disposable.add(
            nfcManager.requestInitProcessor()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(init -> {
                                if (!init) {
                                    binding.nfcBg.animate().scaleX(0.1f).scaleY(0.1f).setDuration(1500)
                                            .withEndAction(() -> binding.nfcBg.setVisibility(View.GONE));
                                }
                            },
                            Timber::e)
    );
}
 
Example #3
Source File: MCReader.java    From MifareClassicTool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initialize a MIFARE Classic reader for the given tag.
 * @param tag The tag to operate on.
 */
private MCReader(Tag tag) {
    MifareClassic tmpMFC;
    try {
        tmpMFC = MifareClassic.get(tag);
    } catch (Exception e) {
        Log.e(LOG_TAG, "Could not create MIFARE Classic reader for the"
                + "provided tag (even after patching it).");
        throw e;
    }
    mMFC = tmpMFC;
}
 
Example #4
Source File: Tag.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private String[] generateTechStringList(int[] techList) {
    final int size = techList.length;
    String[] strings = new String[size];
    for (int i = 0; i < size; i++) {
        switch (techList[i]) {
            case TagTechnology.ISO_DEP:
                strings[i] = IsoDep.class.getName();
                break;
            case TagTechnology.MIFARE_CLASSIC:
                strings[i] = MifareClassic.class.getName();
                break;
            case TagTechnology.MIFARE_ULTRALIGHT:
                strings[i] = MifareUltralight.class.getName();
                break;
            case TagTechnology.NDEF:
                strings[i] = Ndef.class.getName();
                break;
            case TagTechnology.NDEF_FORMATABLE:
                strings[i] = NdefFormatable.class.getName();
                break;
            case TagTechnology.NFC_A:
                strings[i] = NfcA.class.getName();
                break;
            case TagTechnology.NFC_B:
                strings[i] = NfcB.class.getName();
                break;
            case TagTechnology.NFC_F:
                strings[i] = NfcF.class.getName();
                break;
            case TagTechnology.NFC_V:
                strings[i] = NfcV.class.getName();
                break;
            case TagTechnology.NFC_BARCODE:
                strings[i] = NfcBarcode.class.getName();
                break;
            default:
                throw new IllegalArgumentException("Unknown tech type " + techList[i]);
        }
    }
    return strings;
}
 
Example #5
Source File: MifareClassicAdapter.java    From external-nfc-api with Apache License 2.0 4 votes vote down vote up
public String toString() {
    return MifareClassic.class.getSimpleName();
}
 
Example #6
Source File: WriteTag.java    From MifareClassicTool with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Create an factory formatted, empty dump with a size matching
 * the current tag size and then call {@link #checkTag()}.
 * Factory (default) MIFARE Classic Access Conditions are: 0xFF0780XX
 * XX = General purpose byte (GPB): Most of the time 0x69. At the end of
 * an Tag XX = 0xBC.
 * @see #checkTag()
 */
private void createFactoryFormattedDump() {
    // This function is directly called after a key map was created.
    // So Common.getTag() will return den current present tag
    // (and its size/sector count).
    mDumpWithPos = new HashMap<>();
    int sectors = MifareClassic.get(Common.getTag()).getSectorCount();
    byte[] emptyBlock = new byte[]
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    byte[] normalSectorTrailer = new byte[] {-1, -1, -1, -1, -1, -1,
            -1, 7, -128, 105, -1, -1, -1, -1, -1, -1};
    byte[] lastSectorTrailer = new byte[] {-1, -1, -1, -1, -1, -1,
            -1, 7, -128, -68, -1, -1, -1, -1, -1, -1};
    // Empty 4 block sector.
    HashMap<Integer, byte[]> empty4BlockSector =
            new HashMap<>(4);
    for (int i = 0; i < 3; i++) {
        empty4BlockSector.put(i, emptyBlock);
    }
    empty4BlockSector.put(3, normalSectorTrailer);
    // Empty 16 block sector.
    HashMap<Integer, byte[]> empty16BlockSector =
            new HashMap<>(16);
    for (int i = 0; i < 15; i++) {
        empty16BlockSector.put(i, emptyBlock);
    }
    empty16BlockSector.put(15, normalSectorTrailer);
    // Last sector.
    HashMap<Integer, byte[]> lastSector;

    // Sector 0.
    HashMap<Integer, byte[]> firstSector =
            new HashMap<>(4);
    firstSector.put(1, emptyBlock);
    firstSector.put(2, emptyBlock);
    firstSector.put(3, normalSectorTrailer);
    mDumpWithPos.put(0, firstSector);
    // Sector 1 - (max.) 31.
    for (int i = 1; i < sectors && i < 32; i++) {
        mDumpWithPos.put(i, empty4BlockSector);
    }
    // Sector 32 - 39.
    if (sectors == 40) {
        // Add the large sectors (containing 16 blocks)
        // of a MIFARE Classic 4k tag.
        for (int i = 32; i < sectors && i < 39; i++) {
            mDumpWithPos.put(i, empty16BlockSector);
        }
        // In the last sector the Sector Trailer is different.
        lastSector = new HashMap<>(empty16BlockSector);
        lastSector.put(15, lastSectorTrailer);
    } else {
        // In the last sector the Sector Trailer is different.
        lastSector = new HashMap<>(empty4BlockSector);
        lastSector.put(3, lastSectorTrailer);
    }
    mDumpWithPos.put(sectors - 1, lastSector);
    checkTag();
}
 
Example #7
Source File: Common.java    From MifareClassicTool with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Check if the tag and the device support the MIFARE Classic technology.
 * @param tag The tag to check.
 * @param context The context of the package manager.
 * @return
 * <ul>
 * <li>0 - Device and tag support MIFARE Classic.</li>
 * <li>-1 - Device does not support MIFARE Classic.</li>
 * <li>-2 - Tag does not support MIFARE Classic.</li>
 * <li>-3 - Error (tag or context is null).</li>
 * </ul>
 */
public static int checkMifareClassicSupport(Tag tag, Context context) {
    if (tag == null || context == null) {
        // Error.
        return -3;
    }

    if (Arrays.asList(tag.getTechList()).contains(
            MifareClassic.class.getName())) {
        // Device and tag support MIFARE Classic.
        return 0;

    // This is no longer valid. There are some devices (e.g. LG's F60)
    // that have this system feature but no MIFARE Classic support.
    // (The F60 has a Broadcom NFC controller.)
    /*
    } else if (context.getPackageManager().hasSystemFeature(
            "com.nxp.mifare")){
        // Tag does not support MIFARE Classic.
        return -2;
    */

    } else {
        // Check if device does not support MIFARE Classic.
        // For doing so, check if the SAK of the tag indicate that
        // it's a MIFARE Classic tag.
        // See: https://www.nxp.com/docs/en/application-note/AN10834.pdf
        NfcA nfca = NfcA.get(tag);
        byte sak = (byte)nfca.getSak();
        if ((sak>>1 & 1) == 1) {
            // RFU.
            return -2;
        } else {
            if ((sak>>3 & 1) == 1) { // SAK bit 4 = 1?
                if((sak>>4 & 1) == 1) { // SAK bit 5 = 1?
                    // MIFARE Classic 4k
                    // MIFARE SmartMX 4K
                    // MIFARE PlusS 4K SL1
                    // MIFARE PlusX 4K SL1
                    return -1;
                } else {
                    if ((sak & 1) == 1) { // SAK bit 1 = 1?
                        // MIFARE Mini
                        return -1;
                    } else {
                        // MIFARE Classic 1k
                        // MIFARE SmartMX 1k
                        // MIFARE PlusS 2K SL1
                        // MIFARE PlusX 2K SL2
                        return -1;
                    }
                }
            } else {
                // Some MIFARE tag, but not Classic or Classic compatible.
                return -2;
            }
        }

        // Old MIFARE Classic support check. No longer valid.
        // Check if the ATQA + SAK of the tag indicate that it's a MIFARE Classic tag.
        // See: http://www.nxp.com/documents/application_note/AN10833.pdf
        // (Table 5 and 6)
        // 0x28 is for some emulated tags.
        /*
        NfcA nfca = NfcA.get(tag);
        byte[] atqa = nfca.getAtqa();
        if (atqa[1] == 0 &&
                (atqa[0] == 4 || atqa[0] == (byte)0x44 ||
                 atqa[0] == 2 || atqa[0] == (byte)0x42)) {
            // ATQA says it is most likely a MIFARE Classic tag.
            byte sak = (byte)nfca.getSak();
            if (sak == 8 || sak == 9 || sak == (byte)0x18 ||
                                        sak == (byte)0x88 ||
                                        sak == (byte)0x28) {
                // SAK says it is most likely a MIFARE Classic tag.
                // --> Device does not support MIFARE Classic.
                return -1;
            }
        }
        // Nope, it's not the device (most likely).
        // The tag does not support MIFARE Classic.
        return -2;
        */
    }
}