Example usage for android.nfc Tag getTechList

List of usage examples for android.nfc Tag getTechList

Introduction

In this page you can find the example usage for android.nfc Tag getTechList.

Prototype

public String[] getTechList() 

Source Link

Document

Get the technologies available in this tag, as fully qualified class names.

Usage

From source file:Main.java

public static String[] getTagType(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    String[] techList = tag.getTechList();
    return techList;
}

From source file:com.codebutler.farebot.mifare.Card.java

public static Card dumpTag(byte[] tagId, Tag tag) throws Exception {
    final String[] techs = tag.getTechList();
    if (ArrayUtils.contains(techs, "android.nfc.tech.NfcB"))
        return CEPASCard.dumpTag(tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.IsoDep"))
        return DesfireCard.dumpTag(tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.NfcF"))
        return FelicaCard.dumpTag(tagId, tag);
    else//from  w w w. j a  va 2 s.  co m
        throw new UnsupportedTagException(techs, Utils.getHexString(tag.getId()));
}

From source file:com.codebutler.farebot.card.Card.java

public static Card dumpTag(byte[] tagId, Tag tag) throws Exception {
    final String[] techs = tag.getTechList();
    if (ArrayUtils.contains(techs, "android.nfc.tech.NfcB"))
        return CEPASCard.dumpTag(tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.IsoDep"))
        return DesfireCard.dumpTag(tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.NfcF"))
        return FelicaCard.dumpTag(tagId, tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.MifareClassic"))
        return ClassicCard.dumpTag(tagId, tag);
    else/* w ww. j  a va  2s .c om*/
        throw new UnsupportedTagException(techs, Utils.getHexString(tag.getId()));
}

From source file:Main.java

/**
 * Repairs the broken tag on HTC devices running Android 5.x
 * <p/>//from   w ww .jav a  2s  .c om
 * "It seems, the reason of this bug in TechExtras of NfcA is null. However, TechList contains MifareClassic." -bildin
 * For more information please refer to https://github.com/ikarus23/MifareClassicTool/issues/52#issuecomment-103797115
 * <p/>
 * Code source: https://github.com/ikarus23/MifareClassicTool/issues/52#issuecomment-104277445
 *
 * @param oTag The broken tag
 * @return The fixed tag
 */
public static Tag repairTag(Tag oTag) {
    if (oTag == null)
        return null;

    String[] sTechList = oTag.getTechList();

    Parcel oParcel, nParcel;

    oParcel = Parcel.obtain();
    oTag.writeToParcel(oParcel, 0);
    oParcel.setDataPosition(0);

    int len = oParcel.readInt();
    byte[] id = null;
    if (len >= 0) {
        id = new byte[len];
        oParcel.readByteArray(id);
    }
    int[] oTechList = new int[oParcel.readInt()];
    oParcel.readIntArray(oTechList);
    Bundle[] oTechExtras = oParcel.createTypedArray(Bundle.CREATOR);
    int serviceHandle = oParcel.readInt();
    int isMock = oParcel.readInt();
    IBinder tagService;
    if (isMock == 0) {
        tagService = oParcel.readStrongBinder();
    } else {
        tagService = null;
    }
    oParcel.recycle();

    int nfca_idx = -1;
    int mc_idx = -1;

    for (int idx = 0; idx < sTechList.length; idx++) {
        if (sTechList[idx].equals(NfcA.class.getName())) {
            nfca_idx = idx;
        } else if (sTechList[idx].equals(MifareClassic.class.getName())) {
            mc_idx = idx;
        }
    }

    if (nfca_idx >= 0 && mc_idx >= 0 && oTechExtras[mc_idx] == null) {
        oTechExtras[mc_idx] = oTechExtras[nfca_idx];
    } else {
        return oTag;
    }

    nParcel = Parcel.obtain();
    nParcel.writeInt(id.length);
    nParcel.writeByteArray(id);
    nParcel.writeInt(oTechList.length);
    nParcel.writeIntArray(oTechList);
    nParcel.writeTypedArray(oTechExtras, 0);
    nParcel.writeInt(serviceHandle);
    nParcel.writeInt(isMock);
    if (isMock == 0) {
        nParcel.writeStrongBinder(tagService);
    }
    nParcel.setDataPosition(0);

    Tag nTag = Tag.CREATOR.createFromParcel(nParcel);

    nParcel.recycle();

    return nTag;
}

From source file:Main.java

static JSONObject ndefToJSON(Ndef ndef) {
    JSONObject json = new JSONObject();

    if (ndef != null) {
        try {//from  w  w w .  j av  a  2 s.  com

            Tag tag = ndef.getTag();
            // tag is going to be null for NDEF_FORMATABLE until NfcUtil.parseMessage is refactored
            if (tag != null) {
                json.put("id", byteArrayToJSON(tag.getId()));
                json.put("techTypes", new JSONArray(Arrays.asList(tag.getTechList())));
            }

            json.put("type", translateType(ndef.getType()));
            json.put("maxSize", ndef.getMaxSize());
            json.put("isWritable", ndef.isWritable());
            json.put("ndefMessage", messageToJSON(ndef.getCachedNdefMessage()));
            // Workaround for bug in ICS (Android 4.0 and 4.0.1) where
            // mTag.getTagService(); of the Ndef object sometimes returns null
            // see http://issues.mroland.at/index.php?do=details&task_id=47
            try {
                json.put("canMakeReadOnly", ndef.canMakeReadOnly());
            } catch (NullPointerException e) {
                json.put("canMakeReadOnly", JSONObject.NULL);
            }
        } catch (JSONException e) {
            Log.e(TAG, "Failed to convert ndef into json: " + ndef.toString(), e);
        }
    }
    return json;
}

From source file:Main.java

/**
 * For Activities which want to treat new Intents as Intents with a new Tag attached. If the given Intent has a Tag extra, the {@link #mTag} and
 * {@link #mUID} will be updated and a Toast message will be shown in the calling Context (Activity). This method will also check if the
 * device/tag supports Mifare Classic (see return values).
 * /* w  w  w  . j  a v  a2s  .co m*/
 * @param intent The Intent which should be checked for a new Tag.
 * @param context The Context in which the Toast will be shown.
 * @return <ul>
 *         <li>1 - The device/tag supports Mifare Classic</li>
 *         <li>0 - The device/tag does not support Mifare Classic</li>
 *         <li>-1 - Wrong Intent (action is not "ACTION_TECH_DISCOVERED").</li>
 *         </ul>
 * @see #mTag
 * @see #mUID
 */
public static int treatAsNewTag(Intent intent, Context context) {
    // Check if Intent has a NFC Tag.
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        mTag = tag;
        mUID = tag.getId();

        // Show Toast message with UID.
        //         String id = " (UID: ";
        //         id += byte2HexString(tag.getId());
        //         id += ")";
        //         Toast.makeText(context, id, Toast.LENGTH_LONG).show();

        // Return "1" if device supports Mifare Classic. "0" otherwise.
        return (Arrays.asList(tag.getTechList()).contains(MifareClassic.class.getName())) ? 1 : 0;
    }
    return -1;
}

From source file:com.codebutler.farebot.TagReaderFactory.java

@NonNull
public TagReader getTagReader(@NonNull byte[] tagId, @NonNull Tag tag, @Nullable CardKeys cardKeys)
        throws UnsupportedTagException {
    String[] techs = tag.getTechList();
    if (ArrayUtils.contains(techs, "android.nfc.tech.IsoDep")) {
        return new DesfireTagReader(tagId, tag);
    } else if (ArrayUtils.contains(techs, "android.nfc.tech.NfcB")) {
        return new CEPASTagReader(tagId, tag);
    } else if (ArrayUtils.contains(techs, "android.nfc.tech.NfcF")) {
        return new FelicaTagReader(tagId, tag);
    } else if (ArrayUtils.contains(techs, "android.nfc.tech.MifareClassic")) {
        return new ClassicTagReader(tagId, tag, (ClassicCardKeys) cardKeys);
    } else if (ArrayUtils.contains(techs, "android.nfc.tech.MifareUltralight")) {
        return new UltralightTagReader(tagId, tag);
    } else {/*from  ww  w  . j  a  v a2 s.  c  o  m*/
        throw new UnsupportedTagException(techs, ByteUtils.getHexString(tag.getId()));
    }
}

From source file:com.codebutler.farebot.activities.ReadingTagActivity.java

private void resolveIntent(Intent intent) {
    final TextView textView = (TextView) findViewById(R.id.textView);

    try {//from  w  ww . j  a v  a 2  s .c  o m
        Bundle extras = intent.getExtras();

        final Tag tag = (Tag) extras.getParcelable("android.nfc.extra.TAG");
        ;
        final String[] techs = tag.getTechList();

        new AsyncTask<Void, String, MifareCard>() {
            Exception mException;

            @Override
            protected MifareCard doInBackground(Void... params) {
                try {
                    if (ArrayUtils.contains(techs, "android.nfc.tech.NfcB"))
                        return CEPASCard.dumpTag(tag.getId(), tag);
                    else if (ArrayUtils.contains(techs, "android.nfc.tech.IsoDep"))
                        return DesfireCard.dumpTag(tag.getId(), tag);
                    else
                        throw new UnsupportedTagException(techs, Utils.getHexString(tag.getId()));
                } catch (Exception ex) {
                    mException = ex;
                    return null;
                }
            }

            @Override
            protected void onPostExecute(MifareCard card) {
                if (mException != null) {
                    if (mException instanceof UnsupportedTagException) {
                        UnsupportedTagException ex = (UnsupportedTagException) mException;
                        new AlertDialog.Builder(ReadingTagActivity.this).setTitle("Unsupported Tag")
                                .setMessage(ex.getMessage()).setCancelable(false)
                                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        finish();
                                    }
                                }).show();
                    } else {
                        Utils.showErrorAndFinish(ReadingTagActivity.this, mException);
                    }
                    return;
                }

                try {
                    String cardXml = Utils.xmlNodeToString(card.toXML().getOwnerDocument());

                    ContentValues values = new ContentValues();
                    values.put(CardsTableColumns.TYPE, card.getCardType().toInteger());
                    values.put(CardsTableColumns.TAG_SERIAL, Utils.getHexString(card.getTagId()));
                    values.put(CardsTableColumns.DATA, cardXml);
                    values.put(CardsTableColumns.SCANNED_AT, card.getScannedAt().getTime());

                    Uri uri = getContentResolver().insert(CardProvider.CONTENT_URI_CARD, values);
                    startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    finish();
                } catch (Exception ex) {
                    Utils.showErrorAndFinish(ReadingTagActivity.this, ex);
                }
            }

            @Override
            protected void onProgressUpdate(String... values) {
                textView.setText(values[0]);
            }

        }.execute();

    } catch (Exception ex) {
        Utils.showErrorAndFinish(this, ex);
    }
}

From source file:org.esupportail.nfctagdroid.authentication.DesfireAuthProvider.java

public String desfireAuth(Tag tag) throws ExecutionException, InterruptedException {
    String response = "ERROR";

    IsoDep isoDep = null;/*from  w ww  .  j a  va2 s  .  c  o  m*/
    String[] techList = tag.getTechList();
    for (String tech : techList) {
        if (tech.equals(IsoDep.class.getName())) {
            isoDep = IsoDep.get(tag);
            log.info("Detected Desfire tag with id : " + HexaUtils.swapPairs(tag.getId()));
        }
    }
    if (isoDep == null) {
        throw new NfcTagDroidException("Did not detect a Desfire tag ");
    }

    try {
        String[] command = new String[2];
        String result = "";
        command[1] = "1";
        isoDep.connect();

        while (!command[1].equals("OK") && !command[1].equals("ERROR")) {

            DesfireHttpRequestAsync desfireHttpRequestAsync = new DesfireHttpRequestAsync();
            response = desfireHttpRequestAsync.execute(new String[] { command[1] + "/?result=" + result })
                    .get(time, TimeUnit.MILLISECONDS);
            try {
                JSONArray jsonArr = new JSONArray(response);
                command[0] = jsonArr.getString(0);
                command[1] = jsonArr.getString(1);

                if (!command[1].equals("OK") && !command[1].equals("ERROR")) {
                    byte[] byteResult = isoDep.transceive(HexaUtils.hexStringToByteArray(command[0]));
                    result = HexaUtils.byteArrayToHexString(byteResult);
                }
                log.debug("command step: " + command[1]);
                log.debug("command to send: " + command[0]);
                log.debug("result : " + result);
            } catch (Exception e) {
                throw new NfcTagDroidException(e);
            }
        }
        response = command[0];

    } catch (TimeoutException e) {
        log.warn("Time out");
        throw new NfcTagDroidException("Time out Desfire", e);
    } catch (TagLostException e) {
        throw new NfcTagDroidPleaseRetryTagException("TagLostException - authentication aborted", e);
    } catch (NfcTagDroidException e) {
        throw new NfcTagDroidInvalidTagException("nfctagdroidInvalidTagException - tag not valid", e);
    } catch (IOException e) {
        throw new NfcTagDroidInvalidTagException("IOException - authentication aborted", e);
    } catch (ExecutionException e) {
        throw new NfcTagDroidPleaseRetryTagException("ExecutionException - authentication aborted", e);
    } catch (InterruptedException e) {
        throw new NfcTagDroidPleaseRetryTagException("InterruptedException - authentication aborted", e);
    } finally {
        try {
            isoDep.close();
        } catch (IOException e) {
            throw new NfcTagDroidException(e);
        }
    }

    return response;
}

From source file:com.codebutler.farebot.activities.AddKeyActivity.java

@Override
protected void onNewIntent(Intent intent) {
    Tag tag = (Tag) intent.getParcelableExtra("android.nfc.extra.TAG");
    mTagId = Utils.getHexString(tag.getId(), "");

    if (ArrayUtils.contains(tag.getTechList(), "android.nfc.tech.MifareClassic")) {
        mCardType = "MifareClassic";
        ((TextView) findViewById(R.id.card_type)).setText("MIFARE Classic");
        ((TextView) findViewById(R.id.card_id)).setText(mTagId);
        ((TextView) findViewById(R.id.key_data)).setText(Utils.getHexString(mKeyData, "").toUpperCase());

        findViewById(R.id.directions).setVisibility(View.GONE);
        findViewById(R.id.info).setVisibility(View.VISIBLE);
        findViewById(R.id.add).setVisibility(View.VISIBLE);

    } else {/*from   ww w  . j  av a  2s  .  c  o  m*/
        new AlertDialog.Builder(this).setMessage(R.string.card_keys_not_supported)
                .setPositiveButton(android.R.string.ok, null).show();
    }
}