Search in sources :

Example 1 with FormatException

use of android.nfc.FormatException in project android_frameworks_base by ParanoidAndroid.

the class Ndef method writeNdefMessage.

/**
     * Overwrite the {@link NdefMessage} on this tag.
     *
     * <p>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.
     *
     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
     *
     * @param msg the NDEF Message to write, must not 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
     */
public void writeNdefMessage(NdefMessage msg) throws IOException, FormatException {
    checkConnected();
    try {
        INfcTag tagService = mTag.getTagService();
        if (tagService == null) {
            throw new IOException("Mock tags don't support this operation.");
        }
        int serviceHandle = mTag.getServiceHandle();
        if (tagService.isNdef(serviceHandle)) {
            int errorCode = tagService.ndefWrite(serviceHandle, msg);
            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();
            }
        } else {
            throw new IOException("Tag is not ndef");
        }
    } catch (RemoteException e) {
        Log.e(TAG, "NFC service dead", e);
    }
}
Also used : INfcTag(android.nfc.INfcTag) IOException(java.io.IOException) RemoteException(android.os.RemoteException) FormatException(android.nfc.FormatException)

Example 2 with FormatException

use of android.nfc.FormatException in project android_frameworks_base by ParanoidAndroid.

the class NdefFormatable method format.

/*package*/
void format(NdefMessage firstMessage, boolean makeReadOnly) throws IOException, FormatException {
    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);
    }
}
Also used : INfcTag(android.nfc.INfcTag) IOException(java.io.IOException) RemoteException(android.os.RemoteException) FormatException(android.nfc.FormatException)

Example 3 with FormatException

use of android.nfc.FormatException in project android_frameworks_base by ResurrectionRemix.

the class NdefFormatable method format.

/*package*/
void format(NdefMessage firstMessage, boolean makeReadOnly) throws IOException, FormatException {
    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);
    }
}
Also used : INfcTag(android.nfc.INfcTag) IOException(java.io.IOException) RemoteException(android.os.RemoteException) FormatException(android.nfc.FormatException)

Example 4 with FormatException

use of android.nfc.FormatException in project XobotOS by xamarin.

the class Ndef method getNdefMessage.

/**
     * Read the current {@link android.nfc.NdefMessage} on this tag.
     *
     * <p>This always reads the current NDEF Message stored on the tag.
     *
     * <p>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.
     *
     * @return the NDEF Message, never 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 on the tag is malformed
     */
public NdefMessage getNdefMessage() throws IOException, FormatException {
    checkConnected();
    try {
        INfcTag tagService = mTag.getTagService();
        int serviceHandle = mTag.getServiceHandle();
        if (tagService.isNdef(serviceHandle)) {
            NdefMessage msg = tagService.ndefRead(serviceHandle);
            if (msg == null) {
                int errorCode = tagService.getLastError(serviceHandle);
                switch(errorCode) {
                    case ErrorCodes.ERROR_IO:
                        throw new IOException();
                    case ErrorCodes.ERROR_INVALID_PARAM:
                        throw new FormatException();
                    default:
                        // Should not happen
                        throw new IOException();
                }
            }
            return msg;
        } else {
            return null;
        }
    } catch (RemoteException e) {
        Log.e(TAG, "NFC service dead", e);
        return null;
    }
}
Also used : INfcTag(android.nfc.INfcTag) NdefMessage(android.nfc.NdefMessage) IOException(java.io.IOException) RemoteException(android.os.RemoteException) FormatException(android.nfc.FormatException)

Example 5 with FormatException

use of android.nfc.FormatException in project platform_frameworks_base by android.

the class Ndef method writeNdefMessage.

/**
     * Overwrite the {@link NdefMessage} on this tag.
     *
     * <p>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.
     *
     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
     *
     * @param msg the NDEF Message to write, must not 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
     */
public void writeNdefMessage(NdefMessage msg) throws IOException, FormatException {
    checkConnected();
    try {
        INfcTag tagService = mTag.getTagService();
        if (tagService == null) {
            throw new IOException("Mock tags don't support this operation.");
        }
        int serviceHandle = mTag.getServiceHandle();
        if (tagService.isNdef(serviceHandle)) {
            int errorCode = tagService.ndefWrite(serviceHandle, msg);
            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();
            }
        } else {
            throw new IOException("Tag is not ndef");
        }
    } catch (RemoteException e) {
        Log.e(TAG, "NFC service dead", e);
    }
}
Also used : INfcTag(android.nfc.INfcTag) IOException(java.io.IOException) RemoteException(android.os.RemoteException) FormatException(android.nfc.FormatException)

Aggregations

FormatException (android.nfc.FormatException)24 IOException (java.io.IOException)24 INfcTag (android.nfc.INfcTag)15 RemoteException (android.os.RemoteException)15 NdefMessage (android.nfc.NdefMessage)10 NdefRecord (android.nfc.NdefRecord)9 Ndef (android.nfc.tech.Ndef)9 NdefFormatable (android.nfc.tech.NdefFormatable)2 TagLostException (android.nfc.TagLostException)1 TextView (android.widget.TextView)1