FileDocCategorySizeDatePackage
NdefFormatable.javaAPI DocAndroid 5.1 API6986Thu Mar 12 22:22:10 GMT 2015android.nfc.tech

NdefFormatable

public 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

hide

        super(tag, TagTechnology.NDEF_FORMATABLE);
    
Methods Summary
public voidformat(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.

param
firstMessage the NDEF message to write after formatting, can be null
throws
TagLostException if the tag leaves the field
throws
IOException if there is an I/O failure, or the operation is canceled
throws
FormatException if the NDEF Message to write is malformed

        format(firstMessage, false);
    
voidformat(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 voidformatReadOnly(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.

param
firstMessage the NDEF message to write after formatting
throws
TagLostException if the tag leaves the field
throws
IOException if there is an I/O failure, or the operation is canceled
throws
FormatException if the NDEF Message to write is malformed

        format(firstMessage, true);
    
public static android.nfc.tech.NdefFormatableget(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.

param
tag an NDEF formatable tag
return
NDEF formatable object


                                                              
         
        if (!tag.hasTech(TagTechnology.NDEF_FORMATABLE)) return null;
        try {
            return new NdefFormatable(tag);
        } catch (RemoteException e) {
            return null;
        }