Search in sources :

Example 21 with FormatException

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

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

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

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

use of android.nfc.FormatException 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 24 with FormatException

use of android.nfc.FormatException in project platform_packages_apps_Settings by BlissRoms.

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)

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