下面列出了怎么用android.nfc.tech.TagTechnology的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
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);
}
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;
}
protected NfcTagHandler(TagTechnology tech, TagTechnologyHandler handler) {
mTech = tech;
mTechHandler = handler;
}