Back to project page nfc.
The source code is released under:
Apache License
If you think the Android project nfc listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bosicc.nfc; /*ww w . ja v a2 s . com*/ import android.annotation.TargetApi; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.nfc.*; import android.nfc.tech.MifareClassic; import android.nfc.tech.MifareUltralight; import android.os.*; import android.provider.Settings; import android.text.format.Time; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.bosicc.nfc.utils.NdefRecordCreator; import java.nio.charset.Charset; import java.util.Locale; @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class BeamActivity extends Activity implements NfcAdapter.CreateNdefMessageCallback, NfcAdapter.OnNdefPushCompleteCallback { private static final String TAG = "BeamActivity"; private static final String mimeType = "application/vnd.com.bosicc.nfc.beam"; private static final String defaultApp = "com.bosicc.nfc"; private NfcAdapter mNfcAdapter; private PendingIntent mPendingIntent; private TextView infoText; private EditText editText; private static final int MESSAGE_SENT = 1; @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_beam); infoText = (TextView) findViewById(R.id.textInfo); editText = (EditText) findViewById(R.id.editText); // Check for available NFC Adapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); if (mNfcAdapter == null) { infoText.setText("NFC is not available on this device."); } else { // Register callback to set NDEF message mNfcAdapter.setNdefPushMessageCallback(this, this); // Register callback to listen for message-sent success mNfcAdapter.setOnNdefPushCompleteCallback(this, this); infoText.setText("NFC is available on this device !"); } /* Prepare pending intent to handle incoming NDEF message over the beam */ mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, BeamActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); } @Override public void onResume() { super.onResume(); Log.i(TAG, "onResume() ..."); mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); Intent intent = getIntent(); /*Parse intent data*/ resolveIntent(intent); } @Override protected void onPause() { super.onPause(); mNfcAdapter.disableForegroundDispatch(this); } /** * Implementation for the CreateNdefMessageCallback interface */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public NdefMessage createNdefMessage(NfcEvent event) { String text = editText.getText().toString(); NdefRecord record = NdefRecordCreator.getMime(mimeType, text); /** * The Android Application Record (AAR) is commented out. When a device * receives a push with an AAR in it, the application specified in the AAR * is guaranteed to run. The AAR overrides the tag dispatch system. * You can add it back in to guarantee that this * activity starts when receiving a beamed message. For now, this code * uses the tag dispatch system. */ //NdefMessage msg = new NdefMessage(record); NdefMessage msg = new NdefMessage(record, NdefRecord.createApplicationRecord(defaultApp)); Log.i(TAG, "createNdefMessage() [text=" + text + "]"); return msg; } /** * Implementation for the OnNdefPushCompleteCallback interface */ @Override public void onNdefPushComplete(NfcEvent arg0) { // A handler is needed to send messages to the activity when this // callback occurs, because it happens from a binder thread mHandler.obtainMessage(MESSAGE_SENT).sendToTarget(); } /** This handler receives a message from onNdefPushComplete */ private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_SENT: Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show(); break; } } }; @Override public void onNewIntent(Intent intent) { Log.i(TAG, "onNewIntent() [getAction=" + intent.getAction() + ",type=" + intent.getType() + "]"); setIntent(intent); } 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; if (rawMsgs != null) { msgs = new NdefMessage[rawMsgs.length]; String text = ""; for (int i = 0; i < rawMsgs.length; i++) { msgs[i] = (NdefMessage) rawMsgs[i]; text += new String(msgs[i].getRecords()[0].getPayload()); Log.i(TAG, "resolveIntent() [msgs("+i+")=" + msgs[i] + "]"); } infoText.setText(text); } else { // Unknown tag type byte[] empty = new byte[0]; byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); Parcelable ndef = intent.getParcelableExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); Log.i(TAG, "onNewIntent() [ndef=" + ndef + "]"); Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String info = dumpTagData(tag); Log.i(TAG, "onNewIntent() [id=" + getHex(id) + "]"); Log.d(TAG, "onNewIntent() [info=" + info + "]"); infoText.setText(info); } } } private String dumpTagData(Parcelable p) { //TagData data = new TagData(); StringBuilder sb = new StringBuilder(); Tag tag = (Tag) p; byte[] id = tag.getId(); sb.append("Tag ID (raw): ").append(id).append("\n"); sb.append("Tag ID (hex): ").append(getHex(id)).append("\n"); sb.append("Tag ID (dec): ").append(getDec(id)).append("\n"); sb.append("ID (reversed): ").append(getReversed(id)).append("\n"); String prefix = "android.nfc.tech."; sb.append("Technologies: "); for (String tech : tag.getTechList()) { sb.append(tech.substring(prefix.length())); sb.append(", "); } sb.delete(sb.length() - 2, sb.length()); for (String tech : tag.getTechList()) { if (tech.equals(MifareClassic.class.getName())) { sb.append('\n'); MifareClassic mifareTag = MifareClassic.get(tag); String type = "Unknown"; switch (mifareTag.getType()) { case MifareClassic.TYPE_CLASSIC: type = "Classic"; break; case MifareClassic.TYPE_PLUS: type = "Plus"; break; case MifareClassic.TYPE_PRO: type = "Pro"; break; } sb.append("Mifare Classic type: "); sb.append(type); sb.append('\n'); sb.append("Mifare size: "); sb.append(mifareTag.getSize() + " bytes"); sb.append('\n'); sb.append("Mifare sectors: "); sb.append(mifareTag.getSectorCount()); sb.append('\n'); sb.append("Mifare blocks: "); sb.append(mifareTag.getBlockCount()); } if (tech.equals(MifareUltralight.class.getName())) { sb.append('\n'); MifareUltralight mifareUlTag = MifareUltralight.get(tag); String type = "Unknown"; switch (mifareUlTag.getType()) { case MifareUltralight.TYPE_ULTRALIGHT: type = "Ultralight"; break; case MifareUltralight.TYPE_ULTRALIGHT_C: type = "Ultralight C"; break; } sb.append("Mifare Ultralight type: "); sb.append(type); } } return sb.toString(); } /** * Parses the NDEF Message from the intent and prints to the TextView */ void processIntent(Intent intent) { } @Override public boolean onCreateOptionsMenu(Menu menu) { // If NFC is not available, we won't be needing this menu if (mNfcAdapter == null) { return super.onCreateOptionsMenu(menu); } MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_settings: Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } } private String getHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = bytes.length - 1; i >= 0; --i) { int b = bytes[i] & 0xff; if (b < 0x10) sb.append('0'); sb.append(Integer.toHexString(b)); if (i > 0) { sb.append(" "); } } return sb.toString(); } private long getDec(byte[] bytes) { long result = 0; long factor = 1; for (int i = 0; i < bytes.length; ++i) { long value = bytes[i] & 0xffl; result += value * factor; factor *= 256l; } return result; } private long getReversed(byte[] bytes) { long result = 0; long factor = 1; for (int i = bytes.length - 1; i >= 0; --i) { long value = bytes[i] & 0xffl; result += value * factor; factor *= 256l; } return result; } }