android.nfc.tech.NfcA Java Examples

The following examples show how to use android.nfc.tech.NfcA. 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: AmiiboCommands.java    From amiibo with GNU General Public License v2.0 6 votes vote down vote up
private void setProt(NfcA tag, boolean prot, int authlim) {
    byte[] response = new byte[0];
    try {
        response = tag.transceive(new byte[]{
                (byte) Constants.COMMAND_READ, // COMMAND_READ
                (byte) 0x84    // page address
        });
        if ((response != null)) {  // read always returns 4 pages
            byte[] write = new byte[]{
                    (byte) 0xA2, // COMMAND_WRITE
                    (byte) 38,   // page address
                    (byte) ((response[0] & 0x078) | (prot ? 0x080 : 0x000) | (authlim & 0x007)),
                    response[1], response[2], response[3]  // keep old value for bytes 1-3, you could also simply set them to 0 as they are currently RFU and must always be written as 0 (response[1], response[2], response[3] will contain 0 too as they contain the read RFU value)
            };
            response = tag.transceive(write);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: AmiiboCommands.java    From amiibo with GNU General Public License v2.0 6 votes vote down vote up
private void setAuth0(NfcA tag, int auth0) {
    byte[] response = new byte[0];
    try {
        response = tag.transceive(new byte[]{
                (byte) Constants.COMMAND_READ, // COMMAND_READ
                (byte) 0x83    // page address
        });
        if ((response != null) && (response.length >= 16)) {  // read always returns 4 pages
            byte[] write = new byte[]{
                    (byte) 0xA2, // COMMAND_WRITE
                    (byte) 37,   // page address
                    response[0], // keep old value for byte 0
                    response[1], // keep old value for byte 1
                    response[2], // keep old value for byte 2
                    (byte) (auth0 & 0x0ff)
            };
            response = tag.transceive(write);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #4
Source File: AmiiboIO.java    From amiibo with GNU General Public License v2.0 6 votes vote down vote up
public static boolean authenticateAmiibo(NfcA tag, byte[] uid) {
    byte[] password = AmiiboMethods.keygen(uid);

    byte[] auth = new byte[]{
            (byte) 0x1B,
            password[0],
            password[1],
            password[2],
            password[3]
    };
    byte[] response = new byte[0];
    try {
        response = tag.transceive(auth);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #5
Source File: MainActivity.java    From amiibo with GNU General Public License v2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
protected void onNewIntent(Intent paramIntent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(paramIntent.getAction())) {
        Tag tag = paramIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        byte[] uid = paramIntent.getByteArrayExtra(NfcAdapter.EXTRA_ID);


        NfcA ntag215 = NfcA.get(tag);

        if (_stack_controller != null) {
            PopableFragment popable = _stack_controller.head();
            if (popable != null) {
                if (popable instanceof ScanFragment) {
                    ((ScanFragment) popable).tryReadingAmiibo(ntag215, uid);
                } else if (popable instanceof ScanToWriteFragment) {
                    ((ScanToWriteFragment) popable).tryWriteAmiibo(ntag215, uid);
                }
            }
        }
    } else {
        setIntent(paramIntent);
    }
}
 
Example #6
Source File: AmiiboCommands.java    From amiibo with GNU General Public License v2.0 5 votes vote down vote up
private void setLock(NfcA tag, boolean lock_128_129,
                     boolean lock_112_127,
                     boolean lock_96_111,
                     boolean lock_80_95,
                     boolean lock_64_79,
                     boolean lock_48_63,
                     boolean lock_32_47,
                     boolean lock_16_31) {
    byte[] response = new byte[0];
    try {
        response = tag.transceive(new byte[]{
                (byte) Constants.COMMAND_READ, // COMMAND_READ
                (byte) 0x82    // page address
        });
        byte lock = (byte) ((lock_128_129 ? 1 << 7 : 0)
                + (lock_112_127 ? 1 << 6 : 0)
                + (lock_96_111 ? 1 << 5 : 0)
                + (lock_80_95 ? 1 << 4 : 0)
                + (lock_64_79 ? 1 << 3 : 0)
                + (lock_48_63 ? 1 << 2 : 0)
                + (lock_32_47 ? 1 << 1 : 0)
                + (lock_16_31 ? 1 : 0));
        if ((response != null)) {  // read always returns 4 pages
            byte[] write = new byte[]{
                    (byte) 0xA2, // COMMAND_WRITE
                    (byte) 38,   // page address
                    lock,
                    response[1], response[2], response[3]  // keep old value for bytes 1-3, you could also simply set them to 0 as they are currently RFU and must always be written as 0 (response[1], response[2], response[3] will contain 0 too as they contain the read RFU value)
            };
            response = tag.transceive(write);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #7
Source File: Common.java    From MifareClassicTool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Enables the NFC foreground dispatch system for the given Activity.
 * @param targetActivity The Activity that is in foreground and wants to
 * have NFC Intents.
 * @see #disableNfcForegroundDispatch(Activity)
 */
public static void enableNfcForegroundDispatch(Activity targetActivity) {
    if (mNfcAdapter != null && mNfcAdapter.isEnabled()) {

        Intent intent = new Intent(targetActivity,
                targetActivity.getClass()).addFlags(
                        Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                targetActivity, 0, intent, 0);
        mNfcAdapter.enableForegroundDispatch(
                targetActivity, pendingIntent, null, new String[][] {
                        new String[] { NfcA.class.getName() } });
    }
}
 
Example #8
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 #9
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;
        */
    }
}