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[] records) 

Source Link

Document

Construct an NDEF Message from one or more NDEF Records.

Usage

From source file:ca.psiphon.ploggy.ActivitySendIdentityByNfc.java

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    try {/* w ww.j a va  2 s  .  com*/
        String payload = Json.toJson(Data.getInstance().getSelf().mPublicIdentity);
        return new NdefMessage(new NdefRecord[] { NdefRecord.createMime(NFC_MIME_TYPE, payload.getBytes()),
                NdefRecord.createApplicationRecord(NFC_AAR_PACKAGE_NAME) });
    } catch (Utils.ApplicationError e) {
        Log.addEntry(LOG_TAG, "failed to create outbound NFC message");
    }
    return null;
}

From source file:com.commonsware.android.webbeam.WebBeamActivity.java

@Override
public NdefMessage createNdefMessage(NfcEvent arg0) {
    NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
            MIME_TYPE.getBytes(Charset.forName("US-ASCII")), new byte[0],
            beamFragment.getUrl().getBytes(Charset.forName("US-ASCII")));
    NdefMessage msg = new NdefMessage(new NdefRecord[] { uriRecord,
            NdefRecord.createApplicationRecord("com.commonsware.android.webbeam") });

    return (msg);
}

From source file:org.thinkfree.axihome.NFC.TagViewer.java

void resolveIntent(Intent intent) {

    Log.e(TAG, "Tag detected");
    String action = intent.getAction();

    Log.e(TAG, action);/*from w w w.j  ava2  s.com*/

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage[] msgs;

        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        } else {
            // Unknown tag type
            Log.e(TAG, "Unknown tag type");
            byte[] empty = new byte[] {};
            NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
            NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
            msgs = new NdefMessage[] { msg };
        }
        // Setup the views
        processMessage(msgs);
    } else {

        Log.e(TAG, "Unknown intent " + intent);
        finish();
        return;
    }
}

From source file:us.rader.wyfy.WriteTagActivity.java

/**
 * Create a {@link NdefMessage} from {@link #uri}
 * // ww w.j  a  va2s.com
 * @param currentMessage
 *            ignored
 * 
 * @return {@link NdefMessage}
 */
@Override
protected NdefMessage createNdefMessage(NdefMessage currentMessage) {

    NdefRecord record = createUri(uri);
    NdefRecord aar = createAar(getClass().getPackage());
    NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] { record, aar });
    return ndefMessage;

}

From source file:us.rader.tapset.WriteTagActivity.java

/**
 * Return a {@link NdefMessage} containing the {@link Uri} returned by
 * {@link Intent#getData()} for the {@link Intent} used to launch this
 * {@link WriteTagActivity}//from  www  .  j  a  va 2 s .  com
 * 
 * @param currentContents
 *            current contents of the {@link Tag}
 * 
 * @return the {@link NdefMessage} for the requested {@link Uri}
 * 
 * @see NdefWriterActivity#createNdefMessage(NdefMessage)
 */
@Override
protected NdefMessage createNdefMessage(NdefMessage currentContents) {

    Intent intent = getIntent();
    Uri uri = intent.getData();
    NdefRecord uriRecord = NdefWriterActivity.createUri(uri);
    NdefRecord aaRecord = NdefWriterActivity.createAar(getClass().getPackage());
    return new NdefMessage(new NdefRecord[] { uriRecord, aaRecord });

}

From source file:se.anyro.nfc_reader.TagViewer.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tag_viewer);
    mTagContent = (LinearLayout) findViewById(R.id.list);
    resolveIntent(getIntent());/*  w ww .java 2  s.c  om*/

    mDialog = new AlertDialog.Builder(this).setNeutralButton("Ok", null).create();

    mAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mAdapter == null) {
        showMessage(R.string.error, R.string.no_nfc);
        finish();
        return;
    }

    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    mNdefPushMessage = new NdefMessage(
            new NdefRecord[] { newTextRecord("Message from NFC Reader :-)", Locale.ENGLISH, true) });
}

From source file:root.gast.playground.speech.activation.util.TagWriterExecutionActivity.java

private NdefMessage createNdefWithJson() {

    // create a JSON object out of the values:
    JSONObject activationSpecs = new JSONObject();
    try {/*from   w w w  .ja  va 2  s .co  m*/
        activationSpecs.put("target", "placeholder");
    } catch (JSONException e) {
        Log.d(TAG, "Could not create JSON");
    }

    // create a new NDEF record and containing NDEF message using the app's
    // custom MIME type:
    String mimeType = SPEECH_ACTIVATION_TYPE;
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));
    String data = activationSpecs.toString();
    byte[] dataBytes = data.getBytes(Charset.forName("UTF-8"));
    byte[] id = new byte[0];
    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, dataBytes);
    NdefMessage m = new NdefMessage(new NdefRecord[] { record });

    // return the NDEF message
    return m;
}

From source file:com.battlelancer.seriesguide.ui.OverviewActivity.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override//w  w w  . j av  a  2s .  c  o  m
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_overview);
    setupActionBar();
    setupNavDrawer();

    mShowId = getIntent().getIntExtra(OverviewFragment.InitBundle.SHOW_TVDBID, -1);
    if (mShowId == -1) {
        finish();
        return;
    }

    setupViews(savedInstanceState);

    // Support beaming shows via Android Beam
    if (AndroidUtils.isICSOrHigher()) {
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter != null) {
            mNfcAdapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {
                @Override
                public NdefMessage createNdefMessage(NfcEvent event) {
                    final Series show = DBUtils.getShow(OverviewActivity.this, mShowId);
                    // send id, also title and overview (both can be empty)
                    return new NdefMessage(new NdefRecord[] {
                            createMimeRecord("application/com.battlelancer.seriesguide.beam",
                                    String.valueOf(mShowId).getBytes()),
                            createMimeRecord("application/com.battlelancer.seriesguide.beam",
                                    show.getTitle().getBytes()),
                            createMimeRecord("application/com.battlelancer.seriesguide.beam",
                                    show.getOverview().getBytes()) });
                }

                /**
                 * Creates a custom MIME type encapsulated in an NDEF record
                 */
                public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
                    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
                    return new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
                }
            }, this);
        }
    }

    updateShowDelayed(mShowId);
}

From source file:net.networksaremadeofstring.rhybudd.ViewZenossDeviceListActivity.java

@Override
public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
    try {//from  w w  w. ja  va  2  s .  co  m
        String UID = selectedUID.replace("/zport/dmd/Devices/", "");

        //UID = DeviceIDs.get(mViewPager.getCurrentItem());

        NdefMessage msg = new NdefMessage(new NdefRecord[] {
                createMime("application/vnd.net.networksaremadeofstring.rhybudd.devicepage", UID.getBytes())
                //,NdefRecord.createApplicationRecord("com.example.android.beam")
        });
        return msg;
    } catch (Exception e) {
        e.printStackTrace();
        BugSenseHandler.sendExceptionMessage("ViewZenossDeviceActivity", "processIntent", e);
        return null;
    }
}

From source file:jp.tomorrowkey.android.felicalitewriter.ndef.UriNdefBuilder.java

/**
 * NDEF????/*from   ww  w . j  ava 2  s.c o m*/
 * 
 * @return
 */
public NdefMessage build() {
    try {
        int index = getProtocolIndex(mUriString);
        String protocol = sProtocolList.get(index);

        String uriBody = mUriString.replace(protocol, "");
        byte[] uriBodyBytes = uriBody.getBytes("UTF-8");

        ByteArrayBuffer buffer = new ByteArrayBuffer(1 + uriBody.length());
        buffer.append((byte) index);
        buffer.append(uriBodyBytes, 0, uriBodyBytes.length);

        byte[] payload = buffer.toByteArray();
        NdefMessage message = new NdefMessage(new NdefRecord[] {
                new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload) });

        return message;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

}