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:com.example.mynsocial.BluetoothChat.java

NdefMessage[] getNdefMessages(Intent intent) {
    Log.e(TAG, "+++ getNdefMessages +++");
    NdefMessage[] msgs = null;//from w  w w  .  j  a v a 2s  .  co  m
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        } else {
            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("Peer to Peer 2", "Unknown intent.");
        finish();
    }

    return msgs;
}

From source file:com.googlecode.android_scripting.facade.ui.NFCBeamTask.java

@SuppressLint("NewApi")
NdefMessage[] getNdefMessagesFromIntent(Intent intent) {
    // Parse the intent
    NdefMessage[] msgs = null;/*from   w  ww. j a  v a2 s. c o  m*/
    String action = intent.getAction();
    if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) || action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        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.e(TAG, "Unknown intent.");
        finish();
    }
    return msgs;
}

From source file:com.googlecode.android_scripting.facade.ui.NFCBeamTask.java

@SuppressLint("NewApi")
@Override//from w  w  w.j a va  2  s .  c  o  m
public NdefMessage createNdefMessage(NfcEvent arg0) {
    String data;
    if (mEditText != null) {
        data = mEditText.getText().toString();
    } else {
        data = content;
    }

    String mimeType = "application/com.hipipal.qpy.nfc";

    byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));
    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 message = new NdefMessage(new NdefRecord[] { record });

    return message;
}

From source file:at.flack.MainActivity.java

public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
    NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
    return mimeRecord;
}

From source file:ch.bfh.instacircle.NetworkActiveActivity.java

/**
 * Creates a custom MIME type encapsulated in an NDEF record
 * /*from ww  w  . j a  v  a  2  s . c  o  m*/
 * @param mimeType
 *            The string with the mime type name
 */
public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
    NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
    return mimeRecord;
}

From source file:com.example.multi_ndef.Frag_Write.java

/**
 * Format a tag and write our NDEF message
 *///  w  ww  .j ava2s  . c  om
//@SuppressLint("NewApi")
private boolean writeTag(Tag tag) {
    // record to launch Play Store if app is not installed

    NdefRecord appRecord = NdefRecord.createApplicationRecord("com.example.multi_ndef");

    try {
        ENGLISH = Locale.ENGLISH;
        encodeInUtf8 = true;
    }

    catch (Exception e) {

        Toast toast = Toast.makeText(getApplicationContext(), "Locale Error " + e.toString(),
                Toast.LENGTH_SHORT);
        toast.show();
    }

    try {

        textRecord = createTextRecord(getText(), ENGLISH, encodeInUtf8);

    }

    catch (Exception e)

    {

        Toast toast = Toast.makeText(getApplicationContext(), "Text Conversion error " + e.toString(),
                Toast.LENGTH_SHORT);
        toast.show();

    }

    try {

        uriRecord = NdefRecord.createUri(getUri());
    }

    catch (Exception e) {

        Toast toast = Toast.makeText(getApplicationContext(), "Uri Conversion error " + e.toString(),
                Toast.LENGTH_SHORT);
        toast.show();
    }

    byte[] mimeBytes = MimeType.AppName.getBytes(Charset.forName("US-ASCII"));

    byte[] mimeBytesBT = MimeType.AppNameBT.getBytes(Charset.forName("US-ASCII"));

    byte[] v_data = VCard();

    // Here data is written
    byte[] payload = data(); // payload in hex

    //Comments by neha - 3 Jul 2014
    byte[] payloadBT = btData();

    NdefRecord VcardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/x-vcard".getBytes(), new byte[0],
            v_data);

    NdefRecord BTcardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytesBT, new byte[0], payloadBT);

    NdefRecord cardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);

    NdefRecord SMSRecord = NdefRecord.createUri(SMSpayload);

    NdefRecord MailRecord = NdefRecord.createUri(Mailpayload);

    NdefRecord TeleRecord = NdefRecord.createUri(Telepayload);

    NdefRecord LocationRecord = NdefRecord.createUri(Locationpayload);

    NdefMessage message = new NdefMessage(new NdefRecord[] { cardRecord, textRecord, uriRecord, BTcardRecord,
            VcardRecord, SMSRecord, MailRecord, TeleRecord, LocationRecord, appRecord });
    //  ringProgressDialog.dismiss();

    try {
        // see if tag is already NDEF formatted
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();

            if (!ndef.isWritable()) {
                displayMessage("Read-only tag.");

                return false;
            }

            // work out how much space we need for the data
            int size = message.toByteArray().length;
            if (ndef.getMaxSize() < size) {
                displayMessage("Tag doesn't have enough free space.");

                return false;
            }

            ndef.writeNdefMessage(message);
            displayMessage("Multiple NDEF Records written to tag successfully.");

            return true;
        } else {
            // attempt to format tag
            NdefFormatable format = NdefFormatable.get(tag);
            if (format != null) {
                try {
                    format.connect();
                    format.format(message);
                    displayMessage("Tag written successfully!\nClose this app and scan tag.");

                    return true;
                } catch (IOException e) {
                    displayMessage("Unable to format tag to NDEF.");

                    return false;
                }
            } else {
                displayMessage("Tag doesn't appear to support NDEF format.");

                return false;
            }
        }
    } catch (Exception e) {
        displayMessage("Failed to write tag");

    }

    return false;
}

From source file:com.example.multi_ndef.Frag_Write.java

public NdefRecord createTextRecord(String payload, Locale locale, boolean encodeInUtf8) {
    byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
    Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
    byte[] textBytes = payload.getBytes(utfEncoding);
    int utfBit = encodeInUtf8 ? 0 : (1 << 7);
    char status = (char) (utfBit + langBytes.length);
    byte[] data = new byte[1 + langBytes.length + textBytes.length];
    data[0] = (byte) status;
    System.arraycopy(langBytes, 0, data, 1, langBytes.length);
    System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
    return record;
}

From source file:com.mario22gmail.license.nfc_project.NavigationDrawerActivity.java

private NdefRecord createTextRecord(String content) {
    try {//from   ww w .  ja  v a  2s .c o m
        byte[] language;
        language = Locale.getDefault().getLanguage().getBytes("UTF-8");
        final byte[] text = content.getBytes("UTF-8");
        final int languageSize = language.length;
        final int textLength = text.length;
        final ByteArrayOutputStream payload = new ByteArrayOutputStream(1 + languageSize + textLength);
        payload.write((byte) (languageSize & 0x1F));
        payload.write(language, 0, languageSize);
        payload.write(text, 0, textLength);

        return new NdefRecord(android.nfc.NdefRecord.TNF_WELL_KNOWN, android.nfc.NdefRecord.RTD_TEXT,
                new byte[0], payload.toByteArray());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        Log.e("create text record", e.getMessage());
    }
    return null;
}