android.nfc.tech.MifareClassic

Here are the examples of the java api class android.nfc.tech.MifareClassic taken from open source projects.

1. ClassicCard#dumpTag()

Project: farebot
File: ClassicCard.java
public static ClassicCard dumpTag(byte[] tagId, Tag tag) throws Exception {
    MifareClassic tech = null;
    try {
        tech = MifareClassic.get(tag);
        tech.connect();
        ClassicCardKeys keys = (ClassicCardKeys) CardKeys.forTagId(tagId);
        List<ClassicSector> sectors = new ArrayList<>();
        for (int sectorIndex = 0; sectorIndex < tech.getSectorCount(); sectorIndex++) {
            try {
                boolean authSuccess = false;
                // Try the default keys first
                if (!authSuccess && sectorIndex == 0) {
                    authSuccess = tech.authenticateSectorWithKeyA(sectorIndex, PREAMBLE_KEY);
                }
                if (!authSuccess) {
                    authSuccess = tech.authenticateSectorWithKeyA(sectorIndex, MifareClassic.KEY_DEFAULT);
                }
                if (keys != null) {
                    // Try with a 1:1 sector mapping on our key list first
                    if (!authSuccess) {
                        ClassicSectorKey sectorKey = keys.keyForSector(sectorIndex);
                        if (sectorKey != null) {
                            if (sectorKey.getType().equals(ClassicSectorKey.TYPE_KEYA)) {
                                authSuccess = tech.authenticateSectorWithKeyA(sectorIndex, sectorKey.getKey());
                            } else {
                                authSuccess = tech.authenticateSectorWithKeyB(sectorIndex, sectorKey.getKey());
                            }
                        }
                    }
                    if (!authSuccess) {
                        // Be a little more forgiving on the key list.  Lets try all the keys!
                        //
                        // This takes longer, of course, but means that users aren't scratching
                        // their heads when we don't get the right key straight away.
                        ClassicSectorKey[] cardKeys = keys.keys();
                        for (int keyIndex = 0; keyIndex < cardKeys.length; keyIndex++) {
                            if (keyIndex == sectorIndex) {
                                // We tried this before
                                continue;
                            }
                            if (cardKeys[keyIndex].getType().equals(ClassicSectorKey.TYPE_KEYA)) {
                                authSuccess = tech.authenticateSectorWithKeyA(sectorIndex, cardKeys[keyIndex].getKey());
                            } else {
                                authSuccess = tech.authenticateSectorWithKeyB(sectorIndex, cardKeys[keyIndex].getKey());
                            }
                            if (authSuccess) {
                                // Jump out if we have the key
                                break;
                            }
                        }
                    }
                }
                if (authSuccess) {
                    List<ClassicBlock> blocks = new ArrayList<>();
                    // FIXME: First read trailer block to get type of other blocks.
                    int firstBlockIndex = tech.sectorToBlock(sectorIndex);
                    for (int blockIndex = 0; blockIndex < tech.getBlockCountInSector(sectorIndex); blockIndex++) {
                        byte[] data = tech.readBlock(firstBlockIndex + blockIndex);
                        // FIXME
                        String type = ClassicBlock.TYPE_DATA;
                        blocks.add(ClassicBlock.create(type, blockIndex, data));
                    }
                    sectors.add(new ClassicSector(sectorIndex, blocks.toArray(new ClassicBlock[blocks.size()])));
                } else {
                    sectors.add(new UnauthorizedClassicSector(sectorIndex));
                }
            } catch (IOException ex) {
                sectors.add(new InvalidClassicSector(sectorIndex, Utils.getErrorMessage(ex)));
            }
        }
        return new ClassicCard(tagId, new Date(), sectors.toArray(new ClassicSector[sectors.size()]));
    } finally {
        if (tech != null && tech.isConnected()) {
            tech.close();
        }
    }
}