Example usage for android.nfc NdefMessage NdefMessage

List of usage examples for android.nfc NdefMessage NdefMessage

Introduction

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

Prototype

public NdefMessage(NdefRecord[] records) 

Source Link

Document

Construct an NDEF Message from one or more NDEF Records.

Usage

From source file:org.ounl.lifelonglearninghub.mediaplayer.cast.refplayer.NFCVideoBrowserActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate() is called");

    VideoCastManager.checkGooglePlayServices(this);
    setContentView(R.layout.video_browser);

    mCastManager = CastApplication.getCastManager(this);

    // -- Adding MiniController
    mMini = (MiniController) findViewById(R.id.miniController1);
    mCastManager.addMiniController(mMini);

    mCastConsumer = new VideoCastConsumerImpl() {

        @Override//from  www.  j a  v a2  s .  co m
        public void onFailed(int resourceId, int statusCode) {

        }

        @Override
        public void onConnectionSuspended(int cause) {
            Log.d(TAG, "onConnectionSuspended() was called with cause: " + cause);
            org.ounl.lifelonglearninghub.mediaplayer.cast.refplayer.utils.Utils
                    .showToast(NFCVideoBrowserActivity.this, R.string.connection_temp_lost);
        }

        @Override
        public void onConnectivityRecovered() {
            org.ounl.lifelonglearninghub.mediaplayer.cast.refplayer.utils.Utils
                    .showToast(NFCVideoBrowserActivity.this, R.string.connection_recovered);
        }

        @Override
        public void onCastDeviceDetected(final RouteInfo info) {
            if (!CastPreference.isFtuShown(NFCVideoBrowserActivity.this) && mIsHoneyCombOrAbove) {
                CastPreference.setFtuShown(NFCVideoBrowserActivity.this);

                Log.d(TAG, "Route is visible: " + info);
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        if (mediaRouteMenuItem.isVisible()) {
                            Log.d(TAG, "Cast Icon is visible: " + info.getName());
                            showFtu();
                        }
                    }
                }, 1000);
            }
        }
    };

    setupActionBar();

    mCastManager.reconnectSessionIfPossible(this, false);

    //
    // NFC
    //      
    resolveIntent(getIntent());
    mDialog = new AlertDialog.Builder(this).setNeutralButton("Ok", null).create();

    mAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mAdapter == null) {
        showMessage(R.string.error, R.string.no_nfc);
    }
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    mNdefPushMessage = new NdefMessage(
            new NdefRecord[] { buildTextRecord("Message from NFC Reader :-)", Locale.ENGLISH, true) });

}

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  w  w .ja  va  2  s  .  c  o m*/

    /* 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:jp.co.brilliantservice.android.writertduri.HomeActivity.java

private NdefMessage createUriMessage(String uri) {
    try {/*from   ww  w  .  jav  a 2 s.co 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:org.croudtrip.fragments.offer.MyTripDriverFragment.java

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

    setHasOptionsMenu(true);/*from w  ww  . j a va 2  s  .  co m*/

    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:org.mariotaku.twidere.fragment.UserListFragment.java

@Override
public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final FragmentActivity activity = getActivity();
    setHasOptionsMenu(true);/*from  w  ww  .  j av  a 2s  .  c om*/

    Utils.setNdefPushMessageCallback(activity, new CreateNdefMessageCallback() {

        @Override
        public NdefMessage createNdefMessage(NfcEvent event) {
            final ParcelableUserList userList = getUserList();
            if (userList == null)
                return null;
            return new NdefMessage(new NdefRecord[] { NdefRecord.createUri(
                    LinkCreator.getTwitterUserListLink(userList.user_screen_name, userList.name)), });
        }
    });

    getUserListInfo(false);
}

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  .jav  a2 s .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//  w ww  .  j  ava  2 s.  c om
            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);
    }
}

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

@Override
public NdefMessage createNdefMessage(NfcEvent nfcEvent) {
    try {/*from  ww  w  .  j a  v a 2s  . co m*/
        //String UID = currentDeviceID.replace("/zport/dmd/Devices/","");

        int item = 0;
        if (mViewPager.getCurrentItem() > 0)
            item = mViewPager.getCurrentItem();

        String UID = DeviceIDs.get(item).replace("/zport/dmd/Devices/", "");

        //Log.e("createNdefMessage",UID);

        NdefMessage msg = new NdefMessage(new NdefRecord[] {
                createMime("application/vnd.net.networksaremadeofstring.rhybudd.devicepage", UID.getBytes())
                //,NdefRecord.createApplicationRecord("com.example.android.beam")
        });
        return msg;
    } catch (Exception e) {
        e.printStackTrace();
        BugSenseHandler.sendExceptionMessage("ViewZenossDeviceActivity", "processIntent", e);
        return null;
    }
}

From source file:biz.wiz.android.wallet.ui.WalletAddressFragment.java

@Override
public NdefMessage createNdefMessage(final NfcEvent event) {
    final String uri = currentAddressUriRef.get();
    if (uri != null)
        return new NdefMessage(new NdefRecord[] { NdefRecord.createUri(uri) });
    else/*w  ww . ja  v a2s  .c o  m*/
        return null;
}

From source file:com.brq.wallet.activity.receive.ReceiveCoinsActivity.java

@TargetApi(16)
protected void shareByNfc() {
    if (Build.VERSION.SDK_INT < 16) {
        // the function isNdefPushEnabled is only available for SdkVersion >= 16
        // We would be theoretically able to push the message over Ndef, but it is not
        // possible to check if Ndef/NFC is available or not - so dont try it at all, if
        // SdkVersion is too low
        return;/*w  ww . ja v  a2 s .c  o m*/
    }

    NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
    if (nfc != null && nfc.isNdefPushEnabled()) {
        nfc.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
            @Override
            public NdefMessage createNdefMessage(NfcEvent event) {
                NdefRecord uriRecord = NdefRecord.createUri(getPaymentUri());
                return new NdefMessage(new NdefRecord[] { uriRecord });
            }
        }, this);
        ivNfc.setVisibility(View.VISIBLE);
        ivNfc.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.showSimpleMessageDialog(ReceiveCoinsActivity.this,
                        getString(R.string.nfc_payment_request_hint));
            }
        });
    } else {
        ivNfc.setVisibility(View.GONE);
    }
}