public Tag mockTag(String technology) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { // For future reference // Parameters : byte[] b = {0x8}; String ndefClass = "android.nfc.tech.Ndef"; // FieldName which marks the capacity of the tag String extraNdefMaxlength = (String) Class.forName(ndefClass).getField("EXTRA_NDEF_MAXLENGTH").get(null); // FieldName which marks the tags writability String cardWritableStateField = (String) Class.forName(ndefClass).getField("EXTRA_NDEF_CARDSTATE").get(null); // Field to mark tag R/W int cardWritable = Class.forName(ndefClass).getField("NDEF_MODE_READ_WRITE").getInt(null); Bundle techExtras = new Bundle(); techExtras.putInt(extraNdefMaxlength, 2048); techExtras.putInt(cardWritableStateField, cardWritable); Bundle[] extras = {techExtras}; int[] technologies = {TagTechnology.class.getField(technology.toUpperCase()).getInt(null)}; //https://android.googlesource.com/platform/frameworks/base.git/+/android-4.3_r2/core/java/android/nfc/tech/TagTechnology.java // https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/nfc/Tag.java :376 Parcel parcel = Parcel.obtain(); parcel.writeByteArray(b); //Sets the ID parcel.writeIntArray(technologies); // Sets the technology to NDEF parcel.writeArray(extras); // Needed to set properties for NDEF tag parcel.writeInt(1); // Service handle parcel.writeInt(1); // Indicating a mock return Tag.CREATOR.createFromParcel(parcel); }
protected NfcTagHandler(TagTechnology tech, TagTechnologyHandler handler) { mTech = tech; mTechHandler = handler; }
/** * Tegテクノロジを持っているか否かを検査します * @param tag NFCタグをセット * @param tech TagTechnologyクラスをセット * @return boolean 対象のテクノロジクラス含んでいる場合はtrueが戻ります */ static public boolean hasTech(Tag tag, Class< ? extends TagTechnology> tech) { return hasTech(tag, tech.getCanonicalName()); }