Search in sources :

Example 26 with NdefMessage

use of android.nfc.NdefMessage 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 27 with NdefMessage

use of android.nfc.NdefMessage 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 28 with NdefMessage

use of android.nfc.NdefMessage in project fdroidclient by f-droid.

the class RepoDetailsActivity method processIntent.

private void processIntent(Intent i) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
        Parcelable[] rawMsgs = i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        String url = new String(msg.getRecords()[0].getPayload());
        Utils.debugLog(TAG, "Got this URL: " + url);
        Toast.makeText(this, "Got this URL: " + url, Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        intent.setClass(this, ManageReposActivity.class);
        startActivity(intent);
        finish();
    }
}
Also used : NdefMessage(android.nfc.NdefMessage) Parcelable(android.os.Parcelable) Intent(android.content.Intent)

Example 29 with NdefMessage

use of android.nfc.NdefMessage 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 30 with NdefMessage

use of android.nfc.NdefMessage in project weiciyuan by qii.

the class UserInfoActivity method processIntent.

private void processIntent(Intent intent) {
    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    // only one message sent during the beam
    NdefMessage msg = (NdefMessage) rawMsgs[0];
    Toast.makeText(this, new String(msg.getRecords()[0].getPayload()), Toast.LENGTH_SHORT).show();
    bean = new UserBean();
    bean.setScreen_name(new String(msg.getRecords()[0].getPayload()));
}
Also used : UserBean(org.qii.weiciyuan.bean.UserBean) NdefMessage(android.nfc.NdefMessage) Parcelable(android.os.Parcelable)

Aggregations

NdefMessage (android.nfc.NdefMessage)39 NdefRecord (android.nfc.NdefRecord)23 IOException (java.io.IOException)17 FormatException (android.nfc.FormatException)10 Ndef (android.nfc.tech.Ndef)10 Parcelable (android.os.Parcelable)9 INfcTag (android.nfc.INfcTag)7 TagLostException (android.nfc.TagLostException)7 RemoteException (android.os.RemoteException)7 NfcAdapter (android.nfc.NfcAdapter)6 Intent (android.content.Intent)4 NfcEvent (android.nfc.NfcEvent)4 Tag (android.nfc.Tag)4 SuppressLint (android.annotation.SuppressLint)2 Uri (android.net.Uri)2 NdefFormatable (android.nfc.tech.NdefFormatable)2 Bundle (android.os.Bundle)2 PaymentIntent (de.schildbach.wallet.data.PaymentIntent)2 Activity (android.app.Activity)1 ContentResolver (android.content.ContentResolver)1