Example usage for android.nfc NfcAdapter EXTRA_NDEF_MESSAGES

List of usage examples for android.nfc NfcAdapter EXTRA_NDEF_MESSAGES

Introduction

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

Prototype

String EXTRA_NDEF_MESSAGES

To view the source code for android.nfc NfcAdapter EXTRA_NDEF_MESSAGES.

Click Source Link

Document

Extra containing an array of NdefMessage present on the discovered tag.

This extra is mandatory for #ACTION_NDEF_DISCOVERED intents, and optional for #ACTION_TECH_DISCOVERED , and #ACTION_TAG_DISCOVERED intents.

When this extra is present there will always be at least one NdefMessage element.

Usage

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

/**
 * Parses the NDEF Message from the {@link Intent} and sets the
 * {@link TextView}'s text.// w w w .j av a2s .  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: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;//from www . j a  v  a  2s.co 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:com.piusvelte.taplock.client.core.TapLockToggle.java

@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
    mServiceInterface = ITapLockService.Stub.asInterface(binder);
    if (mUIInterface != null) {
        try {/*  w w  w  . ja  v  a  2 s.  co  m*/
            mServiceInterface.setCallback(mUIInterface);
        } catch (RemoteException e) {
            Log.e(TAG, e.toString());
        }
    }
    Intent intent = getIntent();
    if (intent != null) {
        String action = intent.getAction();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
                && intent.hasExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) {
            Log.d(TAG, "service connected, NDEF_DISCOVERED");
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            NdefMessage message;
            if (rawMsgs != null) {
                // process the first message
                message = (NdefMessage) rawMsgs[0];
                // process the first record
                NdefRecord record = message.getRecords()[0];
                if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN) {
                    try {
                        byte[] payload = record.getPayload();
                        /*
                         * payload[0] contains the "Status Byte Encodings" field, per the
                         * NFC Forum "Text Record Type Definition" section 3.2.1.
                         *
                         * bit7 is the Text Encoding Field.
                         *
                         * if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1):
                         * The text is encoded in UTF16
                         *
                         * Bit_6 is reserved for future use and must be set to zero.
                         *
                         * Bits 5 to 0 are the length of the IANA language code.
                         */
                        String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
                        int languageCodeLength = payload[0] & 0077;
                        String taggedDeviceName = new String(payload, languageCodeLength + 1,
                                payload.length - languageCodeLength - 1, textEncoding);
                        manageDevice(taggedDeviceName, ACTION_TOGGLE);
                    } catch (UnsupportedEncodingException e) {
                        // should never happen unless we get a malformed tag.
                        Log.e(TAG, e.toString());
                        finish();
                    }
                } else
                    finish();
            } else
                finish();
        } else if (intent.getData() != null) {
            String taggedDeviceName = intent.getData().getHost();
            if (taggedDeviceName != null)
                manageDevice(taggedDeviceName, ACTION_TOGGLE);
            else
                finish();
        } else if (ACTION_UNLOCK.equals(action) || ACTION_LOCK.equals(action) || ACTION_TOGGLE.equals(action))
            manageDevice(intent.getStringExtra(EXTRA_DEVICE_NAME), action);
        else
            finish();
    } else
        finish();
}

From source file:com.hivewallet.androidclient.wallet.ui.WalletActivity.java

private void handleIntent(@Nonnull final Intent intent) {
    final String action = intent.getAction();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        final String inputType = intent.getType();
        final NdefMessage ndefMessage = (NdefMessage) intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
        final byte[] input = Nfc.extractMimePayload(Constants.MIMETYPE_TRANSACTION, ndefMessage);

        new BinaryInputParser(inputType, input) {
            @Override/*from   ww w  . j a va 2  s.  co m*/
            protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
                cannotClassify(inputType);
            }

            @Override
            protected void error(final int messageResId, final Object... messageArgs) {
                dialog(WalletActivity.this, null, 0, messageResId, messageArgs);
            }
        }.parse();
    }
}

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select);

    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    Intent intent = getIntent();//from www  .j  ava  2  s.c o  m
    Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

    String title = getString(R.string.default_select_title);

    TextView textViewBottom = (TextView) findViewById(R.id.textView2);
    textViewBottom.setText("");

    if (rawMessages != null) {

        ArrayList<Object> entries = new ArrayList<Object>();

        String data = null;

        for (Parcelable rawMessage : rawMessages) {
            NdefMessage message = (NdefMessage) rawMessage;
            NdefRecord[] records = message.getRecords();
            if (records.length > 1) {
                byte[] bArray = records[1].getPayload();
                byte languageLength = bArray[0];

                languageLength++; // because of the length byte

                data = new String(bArray, languageLength, bArray.length - languageLength);
            }
        }

        try {
            JSONObject json = (JSONObject) new JSONTokener(data).nextValue();
            if (json.has("title"))
                title = json.getString("title");
            if (json.has("key"))
                selectKey = json.getString("key");
            if (json.has("options")) {
                JSONArray arr = json.getJSONArray("options");
                for (int i = 0; i < arr.length(); i++) {
                    entries.add(parseJObject(arr.getJSONObject(i)));
                }
            }

            ListView list = (ListView) findViewById(R.id.listView1);
            list.setAdapter(new OptionListAdapter(this, entries.toArray()));

            list.setOnItemClickListener(new ListView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int index, long id) {
                    CheckBox cb = (CheckBox) view.findViewById(R.id.optionListCheckBox);
                    cb.setChecked(!cb.isChecked());
                }
            });

            textViewBottom.setText(getString(R.string.tap_again));

        } catch (Exception e) {
            String message = getString(R.string.json_err);
            message += "\n";
            message += e.getMessage();

            Toast.makeText(this, message, Toast.LENGTH_LONG).show();

            Log.d(TAG, getString(R.string.json_err));
            Log.d(TAG, e.toString());
            Log.d(TAG, data);
        }
    } else {
        title = getString(R.string.no_tag);
    }

    TextView msg = (TextView) findViewById(R.id.textView1);
    msg.setText(title);

}

From source file:de.schildbach.wallet.ui.WalletActivity.java

private void handleIntent(final Intent intent) {
    final String action = intent.getAction();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        final String inputType = intent.getType();
        final NdefMessage ndefMessage = (NdefMessage) intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
        final byte[] input = Nfc.extractMimePayload(Constants.MIMETYPE_TRANSACTION, ndefMessage);

        new BinaryInputParser(inputType, input) {
            @Override/* w w  w .  j  av a2 s .  c o  m*/
            protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
                cannotClassify(inputType);
            }

            @Override
            protected void error(final int messageResId, final Object... messageArgs) {
                dialog(WalletActivity.this, null, 0, messageResId, messageArgs);
            }
        }.parse();
    }
}

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

void processIntent(Intent intent) {
    try {//from  w  ww. j  a  va 2 s.  c o m
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

        if (null != rawMsgs && rawMsgs.length > 0) {
            NdefMessage msg = (NdefMessage) rawMsgs[0];

            /*for(NdefRecord nR : msg.getRecords())
            {
            String tmpString = new String(nR.getPayload());
            Log.e("Found: ", tmpString);
            }*/

            currentDeviceID = "/zport/dmd/Devices/" + new String(msg.getRecords()[0].getPayload());

        }
    } catch (Exception e) {
        e.printStackTrace();
        BugSenseHandler.sendExceptionMessage("ViewZenossDeviceActivity", "processIntent", e);
        try {
            Toast.makeText(this, "There was a problem trying to find the device ID that was beamed",
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e1) {
            BugSenseHandler.sendExceptionMessage("ViewZenossDeviceActivity", "processIntent Toast", e1);
        }
    } finally {
        try {
            triggerUIHandler.sendEmptyMessage(UI_POPULATE);
        } catch (Exception e) {
            BugSenseHandler.sendExceptionMessage("ViewZenossDeviceActivity", "processIntent", e);
        }
    }
}

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()));
}

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

/**
 * NFC: Parses the NDEF Message from the intent and prints to the TextView
 *//*from  w w  w. j a  v a  2  s. com*/
@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);
}