Search in sources :

Example 1 with NdefFormatable

use of android.nfc.tech.NdefFormatable 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)

Example 2 with NdefFormatable

use of android.nfc.tech.NdefFormatable in project openhab-android by openhab.

the class OpenHABWriteTagActivity method writeTag.

private void writeTag(Tag tag, String uri) {
    Log.d(TAG, "Creating tag object for URI " + uri);
    TextView writeTagMessage = findViewById(R.id.write_tag_message);
    NdefRecord[] ndefRecords = new NdefRecord[] { NdefRecord.createUri(uri) };
    NdefMessage message = new NdefMessage(ndefRecords);
    NdefFormatable ndefFormatable = NdefFormatable.get(tag);
    if (ndefFormatable != null) {
        Log.d(TAG, "Tag is uninitialized, formating");
        try {
            ndefFormatable.connect();
            ndefFormatable.format(message);
            ndefFormatable.close();
            writeTagMessage.setText(R.string.info_write_tag_finished);
            autoCloseActivity();
        } catch (IOException | FormatException e) {
            Log.e(TAG, "Writing to unformatted tag failed: " + e);
            writeTagMessage.setText(R.string.info_write_failed);
        }
    } else {
        Log.d(TAG, "Tag is initialized, writing");
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            try {
                Log.d(TAG, "Connecting");
                ndef.connect();
                Log.d(TAG, "Writing");
                if (ndef.isWritable()) {
                    ndef.writeNdefMessage(message);
                }
                Log.d(TAG, "Closing");
                ndef.close();
                writeTagMessage.setText(R.string.info_write_tag_finished);
                autoCloseActivity();
            } catch (IOException | FormatException e) {
                Log.e(TAG, "Writing to formatted tag failed: " + e);
                writeTagMessage.setText(R.string.info_write_failed);
            }
        } else {
            Log.e(TAG, "Ndef == null");
            writeTagMessage.setText(R.string.info_write_failed);
        }
    }
}
Also used : NdefRecord(android.nfc.NdefRecord) Ndef(android.nfc.tech.Ndef) NdefFormatable(android.nfc.tech.NdefFormatable) NdefMessage(android.nfc.NdefMessage) TextView(android.widget.TextView) IOException(java.io.IOException) FormatException(android.nfc.FormatException)

Example 3 with NdefFormatable

use of android.nfc.tech.NdefFormatable in project android_packages_apps_CMParts by LineageOS.

the class NFCProfileUtils method writeTag.

/*
     * Writes an NdefMessage to a NFC tag
     */
public static boolean writeTag(NdefMessage message, Tag tag) {
    int size = message.toByteArray().length;
    try {
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();
            if (!ndef.isWritable()) {
                Log.e(TAG, "Tag is not writable!");
                return false;
            }
            if (ndef.getMaxSize() < size) {
                Log.e(TAG, "Tag exceeds max ndef message size! [" + size + " > " + ndef.getMaxSize() + "]");
                return false;
            }
            ndef.writeNdefMessage(message);
            return true;
        } else {
            NdefFormatable format = NdefFormatable.get(tag);
            if (format != null) {
                try {
                    format.connect();
                    format.format(message);
                    return true;
                } catch (IOException e) {
                    Log.e(TAG, "Write error!", e);
                    return false;
                }
            } else {
                return false;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Write error!", e);
        return false;
    }
}
Also used : Ndef(android.nfc.tech.Ndef) NdefFormatable(android.nfc.tech.NdefFormatable) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

Ndef (android.nfc.tech.Ndef)3 NdefFormatable (android.nfc.tech.NdefFormatable)3 IOException (java.io.IOException)3 FormatException (android.nfc.FormatException)2 NdefMessage (android.nfc.NdefMessage)2 NdefRecord (android.nfc.NdefRecord)2 TagLostException (android.nfc.TagLostException)1 TextView (android.widget.TextView)1