Search in sources :

Example 31 with NdefMessage

use of android.nfc.NdefMessage in project weiciyuan by qii.

the class AbstractAppActivity method initNFC.

private void initNFC() {
    NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null) {
        return;
    }
    mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {

        @Override
        public NdefMessage createNdefMessage(NfcEvent event) {
            String text = (GlobalContext.getInstance().getCurrentAccountName());
            NdefMessage msg = new NdefMessage(new NdefRecord[] { createMimeRecord("application/org.qii.weiciyuan.beam", text.getBytes()), NdefRecord.createApplicationRecord(getPackageName()) });
            return msg;
        }
    }, this);
}
Also used : NdefRecord(android.nfc.NdefRecord) NfcEvent(android.nfc.NfcEvent) NdefMessage(android.nfc.NdefMessage) NfcAdapter(android.nfc.NfcAdapter)

Example 32 with NdefMessage

use of android.nfc.NdefMessage in project bitcoin-wallet by bitcoin-wallet.

the class WalletActivity method handleIntent.

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
            protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
                cannotClassify(inputType);
            }

            @Override
            protected void error(final int messageResId, final Object... messageArgs) {
                final DialogBuilder dialog = DialogBuilder.dialog(WalletActivity.this, 0, messageResId, messageArgs);
                dialog.singleDismissButton(null);
                dialog.show();
            }
        }.parse();
    }
}
Also used : NdefMessage(android.nfc.NdefMessage) BinaryInputParser(de.schildbach.wallet.ui.InputParser.BinaryInputParser) PaymentIntent(de.schildbach.wallet.data.PaymentIntent)

Example 33 with NdefMessage

use of android.nfc.NdefMessage in project robolectric by robolectric.

the class ShadowNfcAdapterTest method setNdefPushMessage_setsNonNullMessage.

@Test
public void setNdefPushMessage_setsNonNullMessage() {
    final Activity activity = Robolectric.setupActivity(Activity.class);
    final NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
    final NdefMessage message = new NdefMessage(new NdefRecord[] { new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null) });
    adapter.setNdefPushMessage(message, activity);
    assertThat(shadowOf(adapter).getNdefPushMessage()).isSameInstanceAs(message);
}
Also used : NdefRecord(android.nfc.NdefRecord) Activity(android.app.Activity) NdefMessage(android.nfc.NdefMessage) NfcAdapter(android.nfc.NfcAdapter) Test(org.junit.Test)

Example 34 with NdefMessage

use of android.nfc.NdefMessage in project iosched by google.

the class BeamUtils method setBeamSessionUri.

/**
 * Sets this activity's Android Beam message to one representing the given session.
 */
public static void setBeamSessionUri(Activity activity, Uri sessionUri) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
    if (nfcAdapter == null) {
        // No NFC :-(
        return;
    }
    nfcAdapter.setNdefPushMessage(new NdefMessage(new NdefRecord[] { new NdefRecord(NdefRecord.TNF_MIME_MEDIA, ScheduleContract.makeContentItemType(ScheduleContract.Sessions.CONTENT_TYPE_ID).getBytes(), new byte[0], sessionUri.toString().getBytes()) }), activity);
}
Also used : NdefRecord(android.nfc.NdefRecord) NdefMessage(android.nfc.NdefMessage) NfcAdapter(android.nfc.NfcAdapter)

Example 35 with NdefMessage

use of android.nfc.NdefMessage in project iosched by google.

the class BeamUtils method tryUpdateIntentFromBeam.

/**
 * Checks to see if the activity's intent ({@link android.app.Activity#getIntent()}) is
 * an NFC intent that the app recognizes. If it is, then parse the NFC message and set the
 * activity's intent (using {@link Activity#setIntent(android.content.Intent)}) to something
 * the app can recognize (i.e. a normal {@link Intent#ACTION_VIEW} intent).
 */
public static void tryUpdateIntentFromBeam(Activity activity) {
    Intent originalIntent = activity.getIntent();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(originalIntent.getAction())) {
        Parcelable[] rawMsgs = originalIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        // Record 0 contains the MIME type, record 1 is the AAR, if present.
        // In iosched, AARs are not present.
        NdefRecord mimeRecord = msg.getRecords()[0];
        if (ScheduleContract.makeContentItemType(ScheduleContract.Sessions.CONTENT_TYPE_ID).equals(new String(mimeRecord.getType()))) {
            // Re-set the activity's intent to one that represents session details.
            Intent sessionDetailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(new String(mimeRecord.getPayload())));
            activity.setIntent(sessionDetailIntent);
        }
    }
}
Also used : NdefRecord(android.nfc.NdefRecord) NdefMessage(android.nfc.NdefMessage) Intent(android.content.Intent) Parcelable(android.os.Parcelable)

Aggregations

NdefMessage (android.nfc.NdefMessage)39 NdefRecord (android.nfc.NdefRecord)23 IOException (java.io.IOException)17 FormatException (android.nfc.FormatException)10 Ndef (android.nfc.tech.Ndef)10 Parcelable (android.os.Parcelable)9 INfcTag (android.nfc.INfcTag)7 TagLostException (android.nfc.TagLostException)7 RemoteException (android.os.RemoteException)7 NfcAdapter (android.nfc.NfcAdapter)6 Intent (android.content.Intent)4 NfcEvent (android.nfc.NfcEvent)4 Tag (android.nfc.Tag)4 SuppressLint (android.annotation.SuppressLint)2 Uri (android.net.Uri)2 NdefFormatable (android.nfc.tech.NdefFormatable)2 Bundle (android.os.Bundle)2 PaymentIntent (de.schildbach.wallet.data.PaymentIntent)2 Activity (android.app.Activity)1 ContentResolver (android.content.ContentResolver)1