Example usage for android.nfc.tech NfcF get

List of usage examples for android.nfc.tech NfcF get

Introduction

In this page you can find the example usage for android.nfc.tech NfcF get.

Prototype

public static NfcF get(Tag tag) 

Source Link

Document

Get an instance of NfcF for the given tag.

Usage

From source file:com.codebutler.farebot.card.felica.FelicaTagReader.java

@NonNull
@Override/*w  w  w .  ja  va  2 s .  c om*/
protected RawFelicaCard readTag(@NonNull byte[] tagId, @NonNull Tag tag, @NonNull FelicaTech tech,
        @Nullable CardKeys cardKeys) throws Exception {
    NfcF nfcF = NfcF.get(tag);
    Log.d(TAG, "Default system code: " + ByteUtils.getHexString(nfcF.getSystemCode()));

    boolean octopusMagic = false;
    boolean sztMagic = false;

    FeliCaTag ft = new FeliCaTag(tag);

    FeliCaLib.IDm idm = ft.pollingAndGetIDm(FeliCaLib.SYSTEMCODE_ANY);
    FeliCaLib.PMm pmm = ft.getPMm();

    if (idm == null) {
        throw new Exception("Failed to read IDm");
    }

    List<FelicaSystem> systems = new ArrayList<>();

    // FIXME: Enumerate "areas" inside of systems ???
    List<FeliCaLib.SystemCode> codes = Arrays.asList(ft.getSystemCodeList());

    // Check if we failed to get a System Code
    if (codes.size() == 0) {
        // Lets try to ping for an Octopus anyway
        FeliCaLib.IDm octopusSystem = ft.pollingAndGetIDm(FeliCaLib.SYSTEMCODE_OCTOPUS);
        if (octopusSystem != null) {
            Log.d(TAG, "Detected Octopus card");
            // Octopus has a special knocking sequence to allow unprotected reads, and does not
            // respond to the normal system code listing.
            codes.add(new FeliCaLib.SystemCode(FeliCaLib.SYSTEMCODE_OCTOPUS));
            octopusMagic = true;
        }

        FeliCaLib.IDm sztSystem = ft.pollingAndGetIDm(FeliCaLib.SYSTEMCODE_SZT);
        if (sztSystem != null) {
            Log.d(TAG, "Detected Shenzhen Tong card");
            // Because Octopus and SZT are similar systems, use the same knocking sequence in
            // case they have the same bugs with system code listing.
            codes.add(new FeliCaLib.SystemCode(FeliCaLib.SYSTEMCODE_SZT));
            sztMagic = true;
        }
    }

    for (FeliCaLib.SystemCode code : codes) {
        Log.d(TAG, "Got system code: " + ByteUtils.getHexString(code.getBytes()));

        int systemCode = code.getCode();

        FeliCaLib.IDm thisIdm = ft.pollingAndGetIDm(systemCode);

        Log.d(TAG, " - Got IDm: " + ByteUtils.getHexString(thisIdm.getBytes()) + "  compare: "
                + ByteUtils.getHexString(idm.getBytes()));

        byte[] foo = idm.getBytes();
        ArrayUtils.reverse(foo);
        Log.d(TAG, " - Got Card ID? " + ByteUtils.byteArrayToInt(idm.getBytes(), 2, 6) + "  "
                + ByteUtils.byteArrayToInt(foo, 2, 6));

        Log.d(TAG, " - Got PMm: " + ByteUtils.getHexString(ft.getPMm().getBytes()) + "  compare: "
                + ByteUtils.getHexString(pmm.getBytes()));

        List<FelicaService> services = new ArrayList<>();
        FeliCaLib.ServiceCode[] serviceCodes;

        if (octopusMagic && code.getCode() == FeliCaLib.SYSTEMCODE_OCTOPUS) {
            Log.d(TAG, "Stuffing in Octopus magic service code");
            serviceCodes = new FeliCaLib.ServiceCode[] { new FeliCaLib.ServiceCode(FeliCaLib.SERVICE_OCTOPUS) };
        } else if (sztMagic && code.getCode() == FeliCaLib.SYSTEMCODE_SZT) {
            Log.d(TAG, "Stuffing in SZT magic service code");
            serviceCodes = new FeliCaLib.ServiceCode[] { new FeliCaLib.ServiceCode(FeliCaLib.SERVICE_SZT) };
        } else {
            serviceCodes = ft.getServiceCodeList();
        }

        for (FeliCaLib.ServiceCode serviceCode : serviceCodes) {
            byte[] bytes = serviceCode.getBytes();
            ArrayUtils.reverse(bytes);
            int serviceCodeInt = ByteUtils.byteArrayToInt(bytes);
            serviceCode = new FeliCaLib.ServiceCode(serviceCode.getBytes());

            List<FelicaBlock> blocks = new ArrayList<>();

            ft.polling(systemCode);

            byte addr = 0;
            ReadResponse result = ft.readWithoutEncryption(serviceCode, addr);
            while (result != null && result.getStatusFlag1() == 0) {
                blocks.add(FelicaBlock.create(addr, result.getBlockData()));
                addr++;
                result = ft.readWithoutEncryption(serviceCode, addr);
            }

            if (blocks.size() > 0) { // Most service codes appear to be empty...
                services.add(FelicaService.create(serviceCodeInt, blocks));
                Log.d(TAG, "- Service code " + serviceCodeInt + " had " + blocks.size() + " blocks");
            }
        }

        systems.add(FelicaSystem.create(code.getCode(), services));
    }

    return RawFelicaCard.create(tagId, new Date(), idm, pmm, systems);
}