enable Ndef Exchange Mode - Android Network

Android examples for Network:NFC Ndef

Description

enable Ndef Exchange Mode

Demo Code


//package com.java2s;

import android.app.Activity;
import android.app.PendingIntent;

import android.content.IntentFilter;

import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;

public class Main {
    private static NfcAdapter mNfcAdapter;
    private static PendingIntent mNfcPendingIntent;
    private static IntentFilter[] mNdefExchangeFilters;

    private static void enableNdefExchangeMode(Activity activity) {
        mNfcAdapter/*from w  ww . ja  v a  2 s . c  o m*/
                .enableForegroundNdefPush(activity, getMessageAsNdef(""));
        mNfcAdapter.enableForegroundDispatch(activity, mNfcPendingIntent,
                mNdefExchangeFilters, null);
    }

    public static NdefMessage getMessageAsNdef(String message) {
        byte[] textBytes = message.getBytes();
        NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                "text/plain".getBytes(), new byte[] {}, textBytes);
        return new NdefMessage(new NdefRecord[] { textRecord });
    }
}

Related Tutorials