Example usage for android.nfc NdefRecord createApplicationRecord

List of usage examples for android.nfc NdefRecord createApplicationRecord

Introduction

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

Prototype

public static NdefRecord createApplicationRecord(String packageName) 

Source Link

Document

Create a new Android Application Record (AAR).

Usage

From source file:de.stadtrallye.rallyesoft.net.NfcCallback.java

@Override
@TargetApi(14)/* w  w  w. j a  va2  s.  c  o  m*/
public NdefMessage createNdefMessage(NfcEvent event) {
    ObjectMapper mapper = Serialization.getJsonInstance();
    String text = null;
    try {
        text = mapper.writeValueAsString(server.exportLogin());
    } catch (JsonProcessingException e) {
        Log.e(THIS, "Could not export Login", e);
    }
    NdefMessage msg = new NdefMessage(new NdefRecord[] {
            new NdefRecord(NdefRecord.TNF_MIME_MEDIA, Std.APP_MIME.getBytes(Charset.forName("US-ASCII")),
                    new byte[0], text.getBytes(Charset.forName("US_ASCII"))),
            NdefRecord.createApplicationRecord("de.stadtrallye.rallyesoft") });
    return msg;
}

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

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    try {/*from ww w  .j ava 2s.co m*/
        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:net.networksaremadeofstring.rhybudd.WriteNFCActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    BugSenseHandler.initAndStartSession(WriteNFCActivity.this, "44a76a8c");

    setContentView(R.layout.write_tag_activity);

    try {//from  ww  w .  j a  v  a2s .co m
        getActionBar().setSubtitle(getString(R.string.NFCTitle));
        getActionBar().setDisplayHomeAsUpEnabled(true);
    } catch (Exception gab) {
        BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "onCreate", gab);
    }

    //Quick test
    try {
        UID = getIntent().getExtras().getString(PAYLOAD_UID).replace("/zport/dmd/Devices/", "");

        aaRecord = NdefRecord.createApplicationRecord("net.networksaremadeofstring.rhybudd");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            //idRecord = NdefRecord.createExternal("rhybudd:tag", "z", UID.getBytes(Charset.forName("US-ASCII")));
            idRecord = NdefRecord.createMime("application/vnd.rhybudd.device", UID.getBytes());
        } else {
            idRecord = NdefRecord.createUri("rhybudd://" + UID);
        }

        ((TextView) findViewById(R.id.SizesText)).setText("This payload is "
                + (aaRecord.toByteArray().length + idRecord.toByteArray().length)
                + " bytes.\n\nAn ultralight can store up to 46 bytes.\nAn Ultralight C or NTAG203 can store up to 137 bytes.\nDespite the name a 1K can only store up to 716 bytes.");
    } catch (Exception e) {
        BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "onCreate", e);
        try {
            Toast.makeText(this, "Sorry there was error parsing the passed UID, we cannot continue.",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception t) {
            BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "onCreate", t);
        }

        finish();
    }

    try {
        mAdapter = NfcAdapter.getDefaultAdapter(this);
    } catch (Exception e) {
        BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "onCreate getDefaultAdapter", e);
        mAdapter = null;
    }

    try {
        pendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    } catch (Exception e) {
        BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "onCreate pendingIntent", e);
    }

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

    try {
        ndef.addDataType("*/*"); /* Handles all MIME based dispatches.
                                    You should specify only the ones that you need. */
    } catch (IntentFilter.MalformedMimeTypeException e) {
        BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "onCreate", e);
        throw new RuntimeException("fail", e);
    }

    try {
        IntentFilter td = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        intentFiltersArray = new IntentFilter[] { ndef, td };

        techListsArray = new String[][] { new String[] { NfcF.class.getName(), NfcA.class.getName(),
                Ndef.class.getName(), NdefFormatable.class.getName() } };
    } catch (Exception e) {
        BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "onCreate IntentFilter", e);
    }

    CreateHandlers();
}

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

/**
 * NFC: Implementation for the CreateNdefMessageCallback interface
 *//*w  w  w. j a v a 2 s  . c o 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:de.gadc.moneybeam.MoneyBeamActivity.java

/**
 * This method creates a new {@link NdefMessage} by using a
 * {@link JSONObject}.//w w  w .  ja  v a 2 s .  c  om
 * 
 * @return the {@link NdefMessage} for the message callback
 */
public NdefMessage createNdefMessage(NfcEvent event) {
    NdefMessage msg = null;
    String text = null;
    try {
        text = generateJSON().toString();
        msg = new NdefMessage(
                new NdefRecord[] { createMimeRecord("application/paypal-moneybeam", text.getBytes()),
                        NdefRecord.createApplicationRecord(getPackageName()) });

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return msg;
}

From source file:st.alr.homA.ActivityQuickpublishNfc.java

private boolean write(String text, Tag tag) {
    boolean success = true;

    NdefRecord aar = NdefRecord.createApplicationRecord("st.alr.homA");
    NdefRecord[] records = { createRecord(text), aar };
    NdefMessage ndefMsg = new NdefMessage(records);

    try {//  w ww .  j  a va 2  s  .  co  m
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();

            if (!ndef.isWritable()) {
                publishProgress(getResources().getString(R.string.nfcWriteDialogTagReadOnly), false);
                success = false;
            }
            if (ndef.getMaxSize() < ndefMsg.getByteArrayLength()) {
                publishProgress(
                        getResources().getString(R.string.nfcWriteDialogTagCapacityIs) + ndef.getMaxSize()
                                + " byte" + getResources().getString(R.string.nfcWriteDialogTagMessageSizeIs)
                                + ndefMsg.getByteArrayLength() + "byte.",
                        false);
                success = false;
            }

            ndef.writeNdefMessage(ndefMsg);
            success = true;
        } else {
            NdefFormatable format = NdefFormatable.get(tag);
            if (format != null) {
                try {
                    format.connect();
                    format.format(ndefMsg);
                    publishProgress(getResources().getString(R.string.nfcWriteDialogTagFormatedAndWrote), true);
                    success = true;
                } catch (IOException e) {
                    publishProgress(getResources().getString(R.string.nfcWriteDialogTagFailedToFormat), false);
                    success = false;
                }
            } else {
                publishProgress(getResources().getString(R.string.nfcWriteDialogTagNoNDEFSupport), false);
                success = false;
            }
        }
    } catch (Exception e) {
        Log.e(this.toString(),
                getResources().getString(R.string.nfcWriteDialogTagFailedToWrite) + " (" + e.getMessage() + ")",
                e);
    }

    return success;
}

From source file:net.eledge.android.europeana.gui.activity.SearchActivity.java

@TargetApi(14)
private void createNdefPushMessageCallback() {
    NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter != null) {
        mNfcAdapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {
            @Override/*from   w w w  .j  a  v  a2s  . c  o m*/
            public NdefMessage createNdefMessage(NfcEvent event) {
                return new NdefMessage(new NdefRecord[] {
                        new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                                "application/vnd.net.eledge.android.europeana.search".getBytes(), new byte[0],
                                searchController.getPortalUrl().getBytes()),
                        NdefRecord.createApplicationRecord(getPackageName()) });
            }
        }, this);
    }
}

From source file:net.eledge.android.europeana.gui.activity.RecordActivity.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void createNdefPushMessageCallback() {
    NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter != null) {
        mNfcAdapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {
            @Override/* w ww.j av a 2  s  .  c o  m*/
            public NdefMessage createNdefMessage(NfcEvent event) {
                return new NdefMessage(new NdefRecord[] {
                        new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                                "application/vnd.net.eledge.android.europeana.record".getBytes(), new byte[0],
                                recordController.getPortalUrl().getBytes()),
                        NdefRecord.createApplicationRecord(getPackageName()) });
            }
        }, this);
    }
}

From source file:com.hybris.mobile.activity.AbstractProductDetailActivity.java

/**
 * Get the NDEF for a product code//from w  w  w  . j  a va 2 s.c  om
 * 
 * @param productCode
 * @return
 */
private NdefMessage getNDEF(String productCode) {
    NdefMessage msg = null;
    try {
        NdefRecord uriRecord;
        uriRecord = NdefRecord.createUri(getString(R.string.nfc_url, URLEncoder.encode(productCode, "UTF-8")));
        NdefRecord appRecord = NdefRecord.createApplicationRecord(getPackageName());
        msg = new NdefMessage(new NdefRecord[] { uriRecord, appRecord });
    } catch (UnsupportedEncodingException e) {
        LoggingUtils.e(LOG_TAG, "Error trying to encode product code to UTF-8. " + e.getLocalizedMessage(),
                Hybris.getAppContext());
    }

    return msg;
}