NdefFormatablepublic final class NdefFormatable extends BasicTagTechnology Provide access to NDEF format operations on a {@link Tag}.
Acquire a {@link NdefFormatable} object using {@link #get}.
Android devices with NFC must only enumerate and implement this
class for tags for which it can format to NDEF.
Unfortunately the procedures to convert unformated tags to NDEF formatted
tags are not specified by NFC Forum, and are not generally well-known. So
there is no mandatory set of tags for which all Android devices with NFC
must support {@link NdefFormatable}.
Note: Methods that perform I/O operations
require the {@link android.Manifest.permission#NFC} permission. |
Fields Summary |
---|
private static final String | TAG |
Constructors Summary |
---|
public NdefFormatable(android.nfc.Tag tag)Internal constructor, to be used by NfcAdapter
super(tag, TagTechnology.NDEF_FORMATABLE);
|
Methods Summary |
---|
public void | format(android.nfc.NdefMessage firstMessage)Format a tag as NDEF, and write a {@link NdefMessage}.
This is a multi-step process, an IOException is thrown
if any one step fails.
The card is left in a read-write state after this operation.
This is an I/O operation and will block until complete. It must
not be called from the main application thread. A blocked call will be canceled with
{@link IOException} if {@link #close} is called from another thread.
Requires the {@link android.Manifest.permission#NFC} permission.
format(firstMessage, false);
| void | format(android.nfc.NdefMessage firstMessage, boolean makeReadOnly)
checkConnected();
try {
int serviceHandle = mTag.getServiceHandle();
INfcTag tagService = mTag.getTagService();
int errorCode = tagService.formatNdef(serviceHandle, MifareClassic.KEY_DEFAULT);
switch (errorCode) {
case ErrorCodes.SUCCESS:
break;
case ErrorCodes.ERROR_IO:
throw new IOException();
case ErrorCodes.ERROR_INVALID_PARAM:
throw new FormatException();
default:
// Should not happen
throw new IOException();
}
// Now check and see if the format worked
if (!tagService.isNdef(serviceHandle)) {
throw new IOException();
}
// Write a message, if one was provided
if (firstMessage != null) {
errorCode = tagService.ndefWrite(serviceHandle, firstMessage);
switch (errorCode) {
case ErrorCodes.SUCCESS:
break;
case ErrorCodes.ERROR_IO:
throw new IOException();
case ErrorCodes.ERROR_INVALID_PARAM:
throw new FormatException();
default:
// Should not happen
throw new IOException();
}
}
// optionally make read-only
if (makeReadOnly) {
errorCode = tagService.ndefMakeReadOnly(serviceHandle);
switch (errorCode) {
case ErrorCodes.SUCCESS:
break;
case ErrorCodes.ERROR_IO:
throw new IOException();
case ErrorCodes.ERROR_INVALID_PARAM:
throw new IOException();
default:
// Should not happen
throw new IOException();
}
}
} catch (RemoteException e) {
Log.e(TAG, "NFC service dead", e);
}
| public void | formatReadOnly(android.nfc.NdefMessage firstMessage)Formats a tag as NDEF, write a {@link NdefMessage}, and make read-only.
This is a multi-step process, an IOException is thrown
if any one step fails.
The card is left in a read-only state if this method returns successfully.
This is an I/O operation and will block until complete. It must
not be called from the main application thread. A blocked call will be canceled with
{@link IOException} if {@link #close} is called from another thread.
Requires the {@link android.Manifest.permission#NFC} permission.
format(firstMessage, true);
| public static android.nfc.tech.NdefFormatable | get(android.nfc.Tag tag)Get an instance of {@link NdefFormatable} for the given tag.
Does not cause any RF activity and does not block.
Returns null if {@link NdefFormatable} was not enumerated in {@link Tag#getTechList}.
This indicates the tag is not NDEF formatable by this Android device.
if (!tag.hasTech(TagTechnology.NDEF_FORMATABLE)) return null;
try {
return new NdefFormatable(tag);
} catch (RemoteException e) {
return null;
}
|
|