Search in sources :

Example 16 with FormatException

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

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 17 with FormatException

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

the class WriteWifiConfigToNfcDialog method handleWriteNfcEvent.

private void handleWriteNfcEvent(Tag tag) {
    Ndef ndef = Ndef.get(tag);
    if (ndef != null) {
        if (ndef.isWritable()) {
            NdefRecord record = NdefRecord.createMime(NFC_TOKEN_MIME_TYPE, hexStringToByteArray(mWpsNfcConfigurationToken));
            try {
                ndef.connect();
                ndef.writeNdefMessage(new NdefMessage(record));
                getOwnerActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        mProgressBar.setVisibility(View.GONE);
                    }
                });
                setViewText(mLabelView, R.string.status_write_success);
                setViewText(mCancelButton, com.android.internal.R.string.done_label);
            } catch (IOException e) {
                setViewText(mLabelView, R.string.status_failed_to_write);
                Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
                return;
            } catch (FormatException e) {
                setViewText(mLabelView, R.string.status_failed_to_write);
                Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
                return;
            }
        } else {
            setViewText(mLabelView, R.string.status_tag_not_writable);
            Log.e(TAG, "Tag is not writable");
        }
    } else {
        setViewText(mLabelView, R.string.status_tag_not_writable);
        Log.e(TAG, "Tag does not support NDEF");
    }
}
Also used : NdefRecord(android.nfc.NdefRecord) Ndef(android.nfc.tech.Ndef) NdefMessage(android.nfc.NdefMessage) IOException(java.io.IOException) FormatException(android.nfc.FormatException)

Example 18 with FormatException

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

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 19 with FormatException

use of android.nfc.FormatException in project android_packages_apps_Settings by LineageOS.

the class WriteWifiConfigToNfcDialog method handleWriteNfcEvent.

private void handleWriteNfcEvent(Tag tag) {
    Ndef ndef = Ndef.get(tag);
    if (ndef != null) {
        if (ndef.isWritable()) {
            NdefRecord record = NdefRecord.createMime(NFC_TOKEN_MIME_TYPE, hexStringToByteArray(mWpsNfcConfigurationToken));
            try {
                ndef.connect();
                ndef.writeNdefMessage(new NdefMessage(record));
                getOwnerActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        mProgressBar.setVisibility(View.GONE);
                    }
                });
                setViewText(mLabelView, R.string.status_write_success);
                setViewText(mCancelButton, com.android.internal.R.string.done_label);
            } catch (IOException e) {
                setViewText(mLabelView, R.string.status_failed_to_write);
                Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
                return;
            } catch (FormatException e) {
                setViewText(mLabelView, R.string.status_failed_to_write);
                Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
                return;
            }
        } else {
            setViewText(mLabelView, R.string.status_tag_not_writable);
            Log.e(TAG, "Tag is not writable");
        }
    } else {
        setViewText(mLabelView, R.string.status_tag_not_writable);
        Log.e(TAG, "Tag does not support NDEF");
    }
}
Also used : NdefRecord(android.nfc.NdefRecord) Ndef(android.nfc.tech.Ndef) NdefMessage(android.nfc.NdefMessage) IOException(java.io.IOException) FormatException(android.nfc.FormatException)

Example 20 with FormatException

use of android.nfc.FormatException in project PhoneProfilesPlus by henrichg.

the class NFCTagReadWriteManager method writeTag.

/*
     * Writes a text to a tag
     * @param context x
     * @param tag x
     * @param data x
     * @throws NFCTagWriteException
     */
private void writeTag(/*Context context, */
Tag tag, String data) throws NFCTagWriteException {
    // Record with actual data we care about
    // NdefRecord relayRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, null, data.getBytes());
    byte[] textBytes = data.getBytes();
    NdefRecord relayRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "application/vnd.phoneprofilesplus.events".getBytes(), new byte[] {}, textBytes);
    // Complete NDEF message with both records
    NdefMessage message = new NdefMessage(new NdefRecord[] { relayRecord });
    Ndef ndef = Ndef.get(tag);
    if (ndef != null) {
        // If the tag is already formatted, just write the message to it
        try {
            ndef.connect();
        } catch (IOException e) {
            throw new NFCTagWriteException(NFCTagWriteException.NFCErrorType.unknownError);
        }
        // Make sure the tag is writable
        if (!ndef.isWritable()) {
            throw new NFCTagWriteException(NFCErrorType.ReadOnly);
        }
        // Check if there's enough space on the tag for the message
        int size = message.toByteArray().length;
        if (ndef.getMaxSize() < size) {
            throw new NFCTagWriteException(NFCErrorType.NoEnoughSpace);
        }
        try {
            // Write the data to the tag
            ndef.writeNdefMessage(message);
        } catch (TagLostException tle) {
            throw new NFCTagWriteException(NFCTagWriteException.NFCErrorType.tagLost, tle);
        } catch (IOException | FormatException ioe) {
            // nfcFormattingErrorTitle
            throw new NFCTagWriteException(NFCErrorType.formattingError, ioe);
        }
    } else {
        // If the tag is not formatted, format it with the message
        NdefFormatable format = NdefFormatable.get(tag);
        if (format != null) {
            try {
                format.connect();
                format.format(message);
            } catch (TagLostException tle) {
                throw new NFCTagWriteException(NFCErrorType.tagLost, tle);
            } catch (IOException | FormatException ioe) {
                throw new NFCTagWriteException(NFCErrorType.formattingError, ioe);
            }
        } else {
            throw new NFCTagWriteException(NFCErrorType.noNdefError);
        }
    }
}
Also used : NdefRecord(android.nfc.NdefRecord) Ndef(android.nfc.tech.Ndef) NdefFormatable(android.nfc.tech.NdefFormatable) NdefMessage(android.nfc.NdefMessage) IOException(java.io.IOException) FormatException(android.nfc.FormatException) TagLostException(android.nfc.TagLostException)

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