Java tutorial
//package com.java2s; /* * Copyright 2013 Gerhard Klostermeier * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.util.Arrays; import android.content.Context; import android.content.Intent; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.MifareClassic; public class Main { /** * The last detected tag. Set by {@link #treatAsNewTag(android.content.Intent, android.content.Context)} */ private static Tag mTag = null; /** * The last detected UID. Set by {@link #treatAsNewTag(android.content.Intent, android.content.Context)} */ private static byte[] mUID = null; /** * 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). * * @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; } }