Java源码示例:android.nfc.tech.NdefFormatable
示例1
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;
}
示例2
/**
* Writes an NdefMessage to a NFC tag
*/
public static void writeTag(NdefMessage message, Tag tag) throws Exception {
int size = message.toByteArray().length;
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
throw new NfcTagNotWritableException();
}
if (ndef.getMaxSize() < size) {
throw new NfcTagInsufficientMemoryException(ndef.getMaxSize(), size);
}
ndef.writeNdefMessage(message);
} else {
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
format.connect();
format.format(message);
} else {
throw new IllegalArgumentException("Ndef format is NULL");
}
}
}
示例3
/**
* Factory method that creates NfcTagHandler for a given NFC Tag.
*
* @param tag @see android.nfc.Tag
* @return NfcTagHandler or null when unsupported Tag is provided.
*/
public static NfcTagHandler create(Tag tag) {
if (tag == null) return null;
Ndef ndef = Ndef.get(tag);
if (ndef != null) return new NfcTagHandler(ndef, new NdefHandler(ndef));
NdefFormatable formattable = NdefFormatable.get(tag);
if (formattable != null) {
return new NfcTagHandler(formattable, new NdefFormattableHandler(formattable));
}
return null;
}
示例4
/**
* Initializes which intents and NfcTechnologies to filter for
*/
private void initFields() {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mIntentFilters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)};
mTechLists = new String[][]{new String[]{Ndef.class.getName()},
new String[]{NdefFormatable.class.getName()}};
}
示例5
/**
* {@inheritDoc}
*/
@Override
public boolean writeToNdefFormatableAndMakeReadonly(NdefMessage message, NdefFormatable ndefFormat) throws FormatException {
setReadOnly(true);
boolean result = writeToNdefFormatable(message, ndefFormat);
setReadOnly(false);
return result;
}
示例6
@Override
public boolean writeToTag(NdefMessage message, Tag tag) throws FormatException, ReadOnlyTagException, InsufficientCapacityException {
Ndef ndef = Ndef.get(tag);
NdefFormatable formatable = NdefFormatable.get(tag);
boolean result;
if (readOnly) {
result = writeToNdefAndMakeReadonly(message, ndef) || writeToNdefFormatableAndMakeReadonly(message, formatable);
} else {
result = writeToNdef(message, ndef) || writeToNdefFormatable(message, formatable);
}
readOnly = false;
return result;
}
示例7
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;
}
示例8
/**
* 往标签写数据的方法
*
* @param tag
*/
public void writeNFCTag(Tag tag) {
if (tag == null) {
return;
}
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{NdefRecord
.createApplicationRecord(mPackageName)});
//转换成字节获得大小
int size = ndefMessage.toByteArray().length;
try {
//2.判断NFC标签的数据类型(通过Ndef.get方法)
Ndef ndef = Ndef.get(tag);
//判断是否为NDEF标签
if (ndef != null) {
ndef.connect();
//判断是否支持可写
if (!ndef.isWritable()) {
return;
}
//判断标签的容量是否够用
if (ndef.getMaxSize() < size) {
return;
}
//3.写入数据
ndef.writeNdefMessage(ndefMessage);
Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
} else { //当我们买回来的NFC标签是没有格式化的,或者没有分区的执行此步
//Ndef格式类
NdefFormatable format = NdefFormatable.get(tag);
//判断是否获得了NdefFormatable对象,有一些标签是只读的或者不允许格式化的
if (format != null) {
//连接
format.connect();
//格式化并将信息写入标签
format.format(ndefMessage);
Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
}
}
示例9
/**
* 往标签写数据的方法
*
* @param tag
*/
public void writeNFCTag(Tag tag) {
if (tag == null) {
return;
}
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{NdefRecord
.createUri(Uri.parse("http://www.baidu.com"))});
//转换成字节获得大小
int size = ndefMessage.toByteArray().length;
try {
//2.判断NFC标签的数据类型(通过Ndef.get方法)
Ndef ndef = Ndef.get(tag);
//判断是否为NDEF标签
if (ndef != null) {
ndef.connect();
//判断是否支持可写
if (!ndef.isWritable()) {
return;
}
//判断标签的容量是否够用
if (ndef.getMaxSize() < size) {
return;
}
//3.写入数据
ndef.writeNdefMessage(ndefMessage);
Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
} else { //当我们买回来的NFC标签是没有格式化的,或者没有分区的执行此步
//Ndef格式类
NdefFormatable format = NdefFormatable.get(tag);
//判断是否获得了NdefFormatable对象,有一些标签是只读的或者不允许格式化的
if (format != null) {
//连接
format.connect();
//格式化并将信息写入标签
format.format(ndefMessage);
Toast.makeText(this, "写入成功",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
}
}
示例10
NdefFormattableHandler(NdefFormatable ndefFormattable) {
mNdefFormattable = ndefFormattable;
}
示例11
@Override
public boolean writeToNdefFormatable(NdefMessage message, NdefFormatable ndefFormatable) throws FormatException {
return mNdefWrite.writeToNdefFormatable(message, ndefFormatable);
}
示例12
@Override
public boolean writeToNdefFormatableAndMakeReadonly(NdefMessage message, NdefFormatable ndefFormat) throws FormatException {
return mNdefWrite.writeToNdefFormatableAndMakeReadonly(message, ndefFormat);
}
示例13
/**
* Write the message to an NdefFormatable
* @param message to write
* @param ndefFormatable to write to
* @return true if success, false if ndefFormatable == null || message == null
* @throws FormatException
*/
boolean writeToNdefFormatable(NdefMessage message, NdefFormatable ndefFormatable) throws FormatException;
示例14
/**
* Write the message to an NdefFormatable and make readonly
* @see NdefWrite#writeToNdefFormatable(android.nfc.NdefMessage, android.nfc.tech.NdefFormatable)
*/
boolean writeToNdefFormatableAndMakeReadonly(NdefMessage message, NdefFormatable ndefFormat) throws FormatException;