Search in sources :

Example 36 with NdefMessage

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

Example 37 with NdefMessage

use of android.nfc.NdefMessage in project packages_apps_Contacts by AOKP.

the class NfcHandler method createNdefMessage.

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    ContentResolver resolver = mContext.getContentResolver();
    if (mContactUri != null) {
        final String lookupKey = Uri.encode(mContactUri.getPathSegments().get(2));
        final Uri shareUri;
        // of determining this.
        if (lookupKey.equals(PROFILE_LOOKUP_KEY)) {
            shareUri = Profile.CONTENT_VCARD_URI.buildUpon().appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").build();
        } else {
            shareUri = Contacts.CONTENT_VCARD_URI.buildUpon().appendPath(lookupKey).appendQueryParameter(Contacts.QUERY_PARAMETER_VCARD_NO_PHOTO, "true").build();
        }
        ByteArrayOutputStream ndefBytes = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int r;
        try {
            InputStream vcardInputStream = resolver.openInputStream(shareUri);
            while ((r = vcardInputStream.read(buffer)) > 0) {
                ndefBytes.write(buffer, 0, r);
            }
            NdefRecord record = NdefRecord.createMime("text/x-vcard", ndefBytes.toByteArray());
            return new NdefMessage(record);
        } catch (IOException e) {
            Log.e(TAG, "IOException creating vcard.");
            return null;
        }
    } else {
        Log.w(TAG, "No contact URI to share.");
        return null;
    }
}
Also used : NdefRecord(android.nfc.NdefRecord) InputStream(java.io.InputStream) NdefMessage(android.nfc.NdefMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

Example 38 with NdefMessage

use of android.nfc.NdefMessage in project i2p.i2p-bote by i2p.

the class EditContactActivity method processIntent.

/**
 * Parses the NDEF Message from the intent
 */
private void processIntent(Intent intent) {
    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    if (rawMsgs == null || rawMsgs.length < 1)
        // TODO notify user?
        return;
    NdefMessage msg = (NdefMessage) rawMsgs[0];
    NdefRecord[] records = msg.getRecords();
    if (records.length != 2 || records[0].getTnf() != NdefRecord.TNF_EXTERNAL_TYPE || !Arrays.equals(records[0].getType(), Constants.NDEF_LEGACY_TYPE_CONTACT.getBytes()) || records[1].getTnf() != NdefRecord.TNF_EXTERNAL_TYPE || !Arrays.equals(records[1].getType(), Constants.NDEF_LEGACY_TYPE_CONTACT_DESTINATION.getBytes()))
        // TODO notify user?
        return;
    String name = new String(records[0].getPayload());
    String destination = new String(records[1].getPayload());
    EditContactFragment f = EditContactFragment.newInstance(name, destination);
    getSupportFragmentManager().beginTransaction().replace(R.id.container, f).commit();
}
Also used : NdefRecord(android.nfc.NdefRecord) NdefMessage(android.nfc.NdefMessage) Parcelable(android.os.Parcelable)

Example 39 with NdefMessage

use of android.nfc.NdefMessage in project i2p.i2p-bote by i2p.

the class ViewIdentityActivity method onCreate.

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        String key = null;
        Bundle args = getIntent().getExtras();
        if (args != null)
            key = args.getString(ViewIdentityFragment.ADDRESS);
        if (key == null) {
            setResult(RESULT_CANCELED);
            finish();
            return;
        }
        ViewIdentityFragment f = ViewIdentityFragment.newInstance(key);
        getSupportFragmentManager().beginTransaction().add(android.R.id.content, f).commit();
    }
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {

            @Override
            public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
                return getNdefMessage();
            }
        }, this);
    }
}
Also used : NfcEvent(android.nfc.NfcEvent) Bundle(android.os.Bundle) NdefMessage(android.nfc.NdefMessage) NfcAdapter(android.nfc.NfcAdapter) SuppressLint(android.annotation.SuppressLint)

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