Example usage for android.nfc NdefRecord NdefRecord

List of usage examples for android.nfc NdefRecord NdefRecord

Introduction

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

Prototype

public NdefRecord(short tnf, byte[] type, byte[] id, byte[] payload) 

Source Link

Document

Construct an NDEF Record from its component fields.

Recommend to use helpers such as {#createUri} or { #createExternal where possible, since they perform stricter validation that the record is correctly formatted as per NDEF specifications.

Usage

From source file:de.gadc.moneybeam.MoneyBeamActivity.java

/**
 * Creates a custom MIME type encapsulated in an NDEF record.
 * // ww w.  jav a2s .c om
 * @return the {@link NdefRecord} for usage in the {@link NdefMessage}
 */
public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
    final NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
    return mimeRecord;
}

From source file:com.sftoolworks.nfcoptions.SelectActivity.java

protected void writeSelection(Intent intent) {
    ListView list = (ListView) findViewById(R.id.listView1);
    if (list.getCount() == 0)
        return;//from w  w  w.  j  a va  2s.c om

    try {

        Object[] results = ((OptionListAdapter) list.getAdapter()).getCheckedValues();
        JSONObject obj = new JSONObject();

        JSONArray array = new JSONArray();

        if (null != results) {
            for (Object result : results)
                array.put(result.toString());
        }

        obj.put("selection", array);
        obj.put("key", selectKey);

        SharedPreferences sharedPref = getSharedPreferences(getString(R.string.preference_file_key),
                Context.MODE_PRIVATE);

        // android studio (0.5.1) decorates this line as an error (some
        // of the time, anyway) but it's not an error.

        String identifier = sharedPref.getString(getString(R.string.user_id_key), "");
        if (identifier.length() > 0)
            obj.put("user", identifier);

        String json = obj.toString(0);

        String outbound = "\u0002en";
        outbound += json;

        NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, new byte[] { 'T' }, null,
                outbound.getBytes());

        NdefMessage message = new NdefMessage(new NdefRecord[] { textRecord });
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        Ndef ndef = Ndef.get(tag);
        ndef.connect();

        ndef.writeNdefMessage(message);
        ndef.close();

        Toast.makeText(this, R.string.write_ok, Toast.LENGTH_LONG).show();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                finish();
            }
        }, 1500);

    } catch (Exception e) {

        Log.d(TAG, e.toString());
        String err = getString(R.string.tag_write_err) + "\n" + e.getMessage();
        Toast.makeText(this, err, Toast.LENGTH_LONG).show();
    }
}

From source file:jp.co.brilliantservice.android.writertdtext.HomeActivity.java

/**
 * RTD Text Record??NdefMessage????/*from w w  w  .  j a v  a2 s.com*/
 * 
 * @param text 
 * @param languageCode (ISO/IANA)
 * @return
 */
private NdefMessage createTextMessage(String text, String languageCode) {
    try {
        byte statusByte = (byte) languageCode.length();
        byte[] rawLanguageCode = languageCode.getBytes("US-ASCII");
        byte[] rawText = text.getBytes("UTF-8");

        ByteArrayBuffer buffer = new ByteArrayBuffer(1 + rawLanguageCode.length + rawText.length);
        buffer.append(statusByte);
        buffer.append(rawLanguageCode, 0, rawLanguageCode.length);
        buffer.append(rawText, 0, rawText.length);

        byte[] payload = buffer.toByteArray();
        NdefMessage message = new NdefMessage(new NdefRecord[] {
                new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload) });
        return message;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:mai.whack.StickyNotesActivity.java

private void onDataRead(Intent intent) {
    // Parse the intent
    NdefMessage[] msgs = null;//from w  w w .  ja  v  a  2s .  c  om
    String action = intent.getAction();
    byte[] tagId = null;
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
        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
            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 };
        }
    } else {
        Log.d(TAG, "Unknown intent.");
        finish();
    }

    String msg = new String(msgs[0].getRecords()[0].getPayload());
    mHttpGetThread = new HttpGetThread("http://192.168.1.192/store/" + toHex(tagId));
    mHttpGetThread.start();
    // mHttpPostThread = new HttpPostThread("aaaaa", "bbbbb", "sdgsdfdsfs");
    // mHttpPostThread.start();
}

From source file:mobisocial.nfcserver.DesktopNfcServer.java

private static NdefMessage getHandoverNdef(String ref) {
    NdefRecord[] records = new NdefRecord[3];

    /* Handover Request */
    byte[] version = new byte[] { (0x1 << 4) | (0x2) };
    records[0] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_HANDOVER_REQUEST, new byte[0],
            version);/* w  ww. j  a  v  a2 s  .com*/

    /* Collision Resolution */
    records[1] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, new byte[] { 0x63, 0x72 }, new byte[0],
            new byte[] { 0x0, 0x0 });

    /* Handover record */
    byte[] payload = new byte[ref.length() + 1];
    System.arraycopy(ref.getBytes(), 0, payload, 1, ref.length());
    records[2] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload);

    return new NdefMessage(records);
}

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   ww  w .  j a v  a 2  s  .co 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:org.croudtrip.fragments.offer.MyTripDriverFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    setHasOptionsMenu(true);//from  w w  w.  j a  v  a  2 s  .c om

    nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
    NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], null);
    ndefMessage = new NdefMessage(new NdefRecord[] { ndefRecord });

    return inflater.inflate(R.layout.fragment_my_trip_driver, container, false);
}

From source file:jp.co.brilliantservice.android.writertduri.HomeActivity.java

private NdefMessage createUriMessage(String uri) {
    try {//from  w  ww  .ja v  a  2 s . c  o m
        int index = getProtocolIndex(uri);
        String protocol = sProtocolList.get(index);

        String uriBody = uri.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);
    }
}

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

private void resolveIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage[] msgs;//  ww  w . j av  a 2s  .c  o  m
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];

                // magic happens here
                processReadTag((NdefMessage) rawMsgs[i]);
            }

        } else {
            // Unknown tag type
            byte[] empty = new byte[0];
            byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
            Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            byte[] payload = dumpTagData(tag).getBytes();
            NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload);
            NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
            msgs = new NdefMessage[] { msg };
        }
        // Setup the views
        buildTagViews(msgs);
    }
}

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/*from   www .j  ava2s. 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);
    }
}