Example usage for android.nfc NdefMessage getRecords

List of usage examples for android.nfc NdefMessage getRecords

Introduction

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

Prototype

public NdefRecord[] getRecords() 

Source Link

Document

Get the NDEF Records inside this NDEF Message.

An NdefMessage always has one or more NDEF Records: so the following code to retrieve the first record is always safe (no need to check for null or array length >= 1):

 NdefRecord firstRecord = ndefMessage.getRecords()[0]; 

Usage

From source file:Main.java

public static String getNFCString(Parcelable[] rawMessages) {
    if (rawMessages != null && rawMessages.length == 1) {
        NdefMessage message = (NdefMessage) rawMessages[0];
        NdefRecord[] records = message.getRecords();
        if (records.length == 1) {
            byte[] payload = records[0].getPayload();

            //convert byte array to string, throw out first 3 characters (see http://stackoverflow.com/questions/7917567/strange-character-on-android-ndef-record-payload)
            return new String(payload).substring(3);
        }//from  w ww  . jav  a2s  .com
    }

    return null;
}

From source file:Main.java

public static boolean areMessagesEqual(NdefMessage message, NdefMessage otherMessage) {
    return message != null && otherMessage != null
            && areRecordArraysEqual(message.getRecords(), otherMessage.getRecords());
}

From source file:Main.java

static JSONArray messageToJSON(NdefMessage message) {
    if (message == null) {
        return null;
    }//from w  w w  . j a  v  a 2s .c  o  m

    List<JSONObject> list = new ArrayList<JSONObject>();

    for (NdefRecord ndefRecord : message.getRecords()) {
        list.add(recordToJSON(ndefRecord));
    }

    return new JSONArray(list);
}

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

private void handleIntent(Intent i) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
        Parcelable[] rawMsgs = i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        String url = new String(msg.getRecords()[0].getPayload());

        beamFragment.loadUrl(url);//from  ww w .  ja  v a  2  s  .com
    }
}

From source file:net.gpii.android.listener.activity.NfcListenerActivity.java

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

    setContentView(R.layout.nfc);/*  ww  w.j a  v a2 s  . c om*/

    statusTextView = (TextView) findViewById(R.id.nfcStatusText);
    progressView = (ProgressBar) findViewById(R.id.nfcProgressBar);

    Button closeButton = (Button) findViewById(R.id.nfcExitButton);
    closeButton.setOnClickListener(new ActivityQuitListener(this));

    try {
        Parcelable[] messages = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage message = (NdefMessage) messages[0];
        NdefRecord record = message.getRecords()[0];

        byte[] payload = record.getPayload();
        //      String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
        String textEncoding = "UTF-8";
        int languageCodeLength = payload[0] & 0077;

        String username = new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1,
                textEncoding);
        if (username.trim().length() == 0) {
            String errorMessage = "Empty username found on NFC tag.";
            statusTextView.setText(errorMessage);
            progressView.setVisibility(View.INVISIBLE);
        } else {
            GpiiLoginAsyncTask task = new GpiiLoginAsyncTask();
            task.execute(username.trim());
        }
    } catch (UnsupportedEncodingException e) {
        String errorMessage = "Error unpacking NFC text record.";
        statusTextView.setText(errorMessage);
        progressView.setVisibility(View.INVISIBLE);
        Log.e(Constants.TAG, errorMessage, e);
    }
}

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

/**
 * Parses the NDEF Message from the {@link Intent} and sets the
 * {@link TextView}'s text.//from  www. j  a v a 2s .c  om
 */
private void processIntent(Intent intent) {
    final Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    final NdefMessage msg = (NdefMessage) rawMsgs[0];

    try {
        final JSONObject object = new JSONObject(new String(msg.getRecords()[0].getPayload()));

        email = object.getString(Configuration.PREF_MAIL);
        currency = object.getString(Configuration.CURRENCY);
        money = object.getString(Configuration.DOLLAR) + "." + object.getString(Configuration.CENTS);

        final String moneyString = object.getString(Configuration.DOLLAR) + ","
                + object.getString(Configuration.CENTS) + " " + object.getString(Configuration.CURRENCY);

        requestView.setText(getString(R.string.xy_wants_z_dollar_from_you, email, moneyString));
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.google.samples.apps.iosched.ui.NfcBadgeActivity.java

private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);/*w ww  .j a v a  2 s . co m*/

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}

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

/**
 * Parses the NDEF Message from the intent and prints to the TextView
 *//*from   w  ww .j  av a2s .  com*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
void processIntent(Intent intent) {
    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

    // only one message sent during the beam
    NdefMessage msg = (NdefMessage) rawMsgs[0];
    SearchResult show = new SearchResult();
    show.tvdbid = Integer.valueOf(new String(msg.getRecords()[0].getPayload()));
    show.title = new String(msg.getRecords()[1].getPayload());
    show.overview = new String(msg.getRecords()[2].getPayload());

    // display add dialog
    AddShowDialogFragment.showAddDialog(show, getSupportFragmentManager());
}

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

/**
 * NFC: Parses the NDEF Message from the intent and prints to the TextView
 *///from w  ww . ja  v a2 s.c  o  m
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
void handleActionNdefDiscovered(Intent intent) {
    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    // only one message sent during the beam
    NdefMessage msg = (NdefMessage) rawMsgs[0];
    // record 0 contains the MIME type, record 1 is the AAR, if present
    final byte[] receivedKeyringBytes = msg.getRecords()[0].getPayload();

    importKeys(receivedKeyringBytes);
}

From source file:de.stadtrallye.rallyesoft.ConnectionAssistantActivity.java

private void processNfcIntent(Intent intent) {
    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    // only one message sent during the beam
    NdefMessage msg = (NdefMessage) rawMsgs[0];

    readServerLoginJSON(new String(msg.getRecords()[0].getPayload()));
}