@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pendingIntent = PendingIntent.getActivity( this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter mifare = new IntentFilter((NfcAdapter.ACTION_TECH_DISCOVERED)); filters = new IntentFilter[] { mifare }; techs = new String[][] { new String[] {NfcA.class.getName() } }; if(adapter==null) { adapter = NfcAdapter.getDefaultAdapter(this); } imageView = (ImageView) findViewById(R.id.imageView); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation rotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotation); imageView.startAnimation(rotation); } }); }
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(); } }
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(); } }
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; }
@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); } }
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) { final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); //IntentFilter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); IntentFilter[] filters = new IntentFilter[1]; String[][] techList = new String[][]{new String[] { NfcA.class.getName()}, new String[] {NfcB.class.getName()}}; // Notice that this is the same filter as in our manifest. filters[0] = new IntentFilter(); filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED); filters[0].addCategory(Intent.CATEGORY_DEFAULT); /* try { filters[0].addDataType(MIME_TEXT_PLAIN); } catch (MalformedMimeTypeException e) { throw new RuntimeException("Check your mime type."); } */ adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); }
private void ListenForNFCIntent(){ mAdapter = NfcAdapter.getDefaultAdapter(this); mPendingIntent = PendingIntent.getActivity( this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); filter.addDataScheme("vnd.android.nfc"); techListsArray = new String[][] { new String[] { NfcA.class.getName() } }; Intent launchIntent = getIntent(); if(launchIntent != null && launchIntent.getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) { onNewIntent(launchIntent); } }
public void onCreate(Bundle savedInstanceState) { // setup NFC // http://stackoverflow.com/questions/5685946/nfc-broadcastreceiver-problem // http://stackoverflow.com/questions/5685770/nfc-intent-get-info-from-the-tag // NOTE on devices without NFC, this method call returns NULL mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity); mNfcPendingIntent = PendingIntent.getActivity(mActivity, 0, new Intent(mActivity, mActivity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); try { tech.addDataType("*/*"); } catch (MalformedMimeTypeException e) { throw new RuntimeException("fail", e); } mNfcFilters = new IntentFilter[] { tech }; // Mifare Classic are also NfcA, but in contrary to NfcA, MifareClassic support is optional // http://developer.android.com/reference/android/nfc/tech/MifareClassic.html mNfcTechLists = new String[][] { new String[] { NfcA.class.getName() } }; }
private void setForegroundListener() { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); boolean handleFormatable = preferences.getBoolean("format_ndef_formatable_tags", false); pi = PendingIntent.getActivity(this, 0, new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); intentFiltersArray = null; if(handleFormatable) techList = new String[][]{ new String[]{ NfcA.class.getName(),Ndef.class.getName()}, new String[]{ NfcB.class.getName(),Ndef.class.getName()}, new String[]{ NfcF.class.getName(),Ndef.class.getName()}, new String[]{ NfcV.class.getName(),Ndef.class.getName()}, new String[]{ NfcA.class.getName(),NdefFormatable.class.getName()}, new String[]{ NfcB.class.getName(),NdefFormatable.class.getName()}, new String[]{ NfcF.class.getName(),NdefFormatable.class.getName()}, new String[]{ NfcV.class.getName(),NdefFormatable.class.getName()}}; else techList = new String[][]{ new String[]{ NfcA.class.getName(),Ndef.class.getName()}, new String[]{ NfcB.class.getName(),Ndef.class.getName()}, new String[]{ NfcF.class.getName(),Ndef.class.getName()}, new String[]{ NfcV.class.getName(),Ndef.class.getName()}}; }
@Override public void onTagDiscovered(Tag tag) { remoteTag = NfcA.get(tag); try { String str = readUID(); LocalBroadcastManager.getInstance(mContext).sendBroadcast(new Intent("tag-detected")); } catch (Exception e) { LocalBroadcastManager.getInstance(mContext).sendBroadcast(new Intent("error")); } }
@Override public void onResume() { super.onResume(); if (nfcAdapter != null) { PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), KentKartInformationActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{intentFilter}, new String[][]{new String[]{NfcA.class.getName()}}); } }
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(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dump_raw); // Show the Up button in the action bar. setupActionBar(); mAdapter = NfcAdapter.getDefaultAdapter(this); mPendingIntent = PendingIntent.getActivity( this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); // Setup an intent filter for all MIME based dispatches IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); try { ndef.addDataType("*/*"); } catch (MalformedMimeTypeException e) { throw new RuntimeException("fail", e); } IntentFilter td = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); mFilters = new IntentFilter[] { ndef, td }; // Setup a tech list for all NfcF tags mTechLists = new String[][] { new String[] { /*NfcV.class.getName(), NfcF.class.getName(),*/ NfcA.class.getName(), //NfcB.class.getName() } }; txtRaw = (TextView) findViewById(R.id.txtRaw); }
private void setUpNfcStuff(){ // intercept all NFC related Intents and redirect them to this activity while this activity is activated and on the front // this is called the "foreground dispatch" mAdapter = NfcAdapter.getDefaultAdapter(this); mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0); IntentFilter nfcTech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); mFilters = new IntentFilter[]{nfcTech}; mTechLists = new String[][] { new String[] { IsoDep.class.getName() }, {NfcA.class.getName()} }; }
@Override public Element handle(Tag tag) { NfcA nfca = NfcA.get(tag); List<AbstractElement> elements = new LinkedList<AbstractElement>(); elements.add(TextElement.keyValueFrom("sak", "SAK/SEL_RES", Short.toString(nfca.getSak()))); elements.add(TextElement.keyValueFrom("atqa", "ATQA/SENS_RES", SharedStringUtil.byteToHexString(nfca.getAtqa()))); Element element = new Element("nfca_tech", "NFC-A (ISO 14443-3A) Technology Information"); element.addChildElements(elements); return element; }
public NfcNtag(Tag tag) { nfca = NfcA.get(tag); maxTransceiveLength = nfca.getMaxTransceiveLength(); }
/** * Repairs the broken tag on HTC devices running Android 5.x * <p/> * "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; }