Example usage for android.nfc NdefMessage NdefMessage

List of usage examples for android.nfc NdefMessage NdefMessage

Introduction

In this page you can find the example usage for android.nfc NdefMessage NdefMessage.

Prototype

public NdefMessage(NdefRecord record, NdefRecord... records) 

Source Link

Document

Construct an NDEF Message from one or more NDEF Records.

Usage

From source file:org.sufficientlysecure.keychain.ui.ViewKeyActivityJB.java

/**
 * NFC: Implementation for the CreateNdefMessageCallback interface
 */// w w w. j a  va 2 s  . co  m
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    /**
     * When a device receives a push with an AAR in it, the application specified in the AAR is
     * guaranteed to run. The AAR overrides the tag dispatch system. You can add it back in to
     * guarantee that this activity starts when receiving a beamed message. For now, this code
     * uses the tag dispatch system.
     */
    NdefMessage msg = new NdefMessage(NdefRecord.createMime(Constants.NFC_MIME, mSharedKeyringBytes),
            NdefRecord.createApplicationRecord(Constants.PACKAGE_NAME));
    return msg;
}

From source file:org.sufficientlysecure.keychain.ui.ViewKeyActivity.java

/**
 * NFC: Initialize NFC sharing if OS and device supports it
 */// w ww  .  jav  a  2s.  c  o  m
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void initNfc(final Uri dataUri) {
    // check if NFC Beam is supported (>= Android 4.1)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

        // Implementation for the CreateNdefMessageCallback interface
        mNdefCallback = new NfcAdapter.CreateNdefMessageCallback() {
            @Override
            public NdefMessage createNdefMessage(NfcEvent event) {
                /*
                 * When a device receives a push with an AAR in it, the application specified in the AAR is
                 * guaranteed to run. The AAR overrides the tag dispatch system. You can add it back in to
                 * guarantee that this activity starts when receiving a beamed message. For now, this code
                 * uses the tag dispatch system.
                 */
                NdefMessage msg = new NdefMessage(NdefRecord.createMime(Constants.NFC_MIME, mNfcKeyringBytes),
                        NdefRecord.createApplicationRecord(Constants.PACKAGE_NAME));
                return msg;
            }
        };

        // Implementation for the OnNdefPushCompleteCallback interface
        mNdefCompleteCallback = new NfcAdapter.OnNdefPushCompleteCallback() {
            @Override
            public void onNdefPushComplete(NfcEvent event) {
                // A handler is needed to send messages to the activity when this
                // callback occurs, because it happens from a binder thread
                mNfcHandler.obtainMessage(NFC_SENT).sendToTarget();
            }
        };

        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter != null) {
            /*
             * Retrieve mNfcKeyringBytes here asynchronously (to not block the UI)
             * and init nfc adapter afterwards.
             * mNfcKeyringBytes can not be retrieved in createNdefMessage, because this process
             * has no permissions to query the Uri.
             */
            AsyncTask<Void, Void, Void> initTask = new AsyncTask<Void, Void, Void>() {
                protected Void doInBackground(Void... unused) {
                    try {
                        Uri blobUri = KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
                        mNfcKeyringBytes = (byte[]) mProviderHelper.getGenericData(blobUri,
                                KeychainContract.KeyRingData.KEY_RING_DATA, ProviderHelper.FIELD_TYPE_BLOB);
                    } catch (ProviderHelper.NotFoundException e) {
                        Log.e(Constants.TAG, "key not found!", e);
                    }

                    // no AsyncTask return (Void)
                    return null;
                }

                protected void onPostExecute(Void unused) {
                    // Register callback to set NDEF message
                    mNfcAdapter.setNdefPushMessageCallback(mNdefCallback, ViewKeyActivity.this);
                    // Register callback to listen for message-sent success
                    mNfcAdapter.setOnNdefPushCompleteCallback(mNdefCompleteCallback, ViewKeyActivity.this);
                }
            };

            initTask.execute();
        }
    }
}