Back to project page android-nfcv-reader.
The source code is released under:
MIT License
If you think the Android project android-nfcv-reader 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.nathansizemore.nfcvreader; /* w w w .j a v a 2 s. c o m*/ import java.util.Arrays; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.NfcV; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.content.IntentFilter; import android.util.Log; import android.widget.TextView; import com.nathansizemore.nfcvreader.R; public class MainActivity extends Activity { //Properties... private NfcAdapter nfcAdapter; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView); nfcAdapter = NfcAdapter.getDefaultAdapter(this); if (nfcAdapter.isEnabled() == false) { textView.setText("NFC is disabled."); } handleIntent(getIntent()); } @Override public void onResume() { super.onResume(); setupForegroundDispatch(this, nfcAdapter); } @Override public void onPause() { stopForegroundDispatch(this, nfcAdapter); super.onPause(); } @Override protected void onNewIntent(Intent intent) { handleIntent(intent); } /** * * @param intent The calling {@link Intent} (When an NfcV device is recognized) */ private void handleIntent(Intent intent){ if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); Log.d("Action", "ACTION_TAG_DISCOVERED"); Log.d("Tag", tag.toString()); String[] techList = tag.getTechList(); String searchedTech = NfcV.class.getName(); for (String tech : techList) { if (searchedTech.equals(tech)) { Log.d("Tech", tech); new NfcVReaderTask().execute(tag); break; } } } } /** * * @param activity The {@link Activity} requesting foreground dispatch. * @param adapter The {@link NfcAdapter} used for the foreground dispatch. */ public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) { final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); IntentFilter[] filters = new IntentFilter[1]; String[][] techList = new String[][]{}; filters[0] = new IntentFilter(); filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED); filters[0].addCategory(Intent.CATEGORY_DEFAULT); adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); } /** * * @param activity The {@link BaseActivty} requesting to stop the foreground dispatch * @param adapter The {@link NfcAdapter} used for the foreground dispatch */ public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) { adapter.disableForegroundDispatch(activity); } private class NfcVReaderTask extends AsyncTask<Tag, Void, String> { @Override protected String doInBackground(Tag... params) { Tag tag = params[0]; NfcV nfcvTag = NfcV.get(tag); byte[] tagID = nfcvTag.getTag().getId(); Log.d("Hex ID", getHex(tagID)); return Arrays.toString(tagID); } /** * * @param bytes Array of bytes read from {@link NfcV.get(Tag)} * @return String representing ID as Hex */ private String getHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int x = 0; x < bytes.length; x++) { int bit = bytes[x] & 0xFF; if (bit < 0x10) { sb.append('0'); } sb.append(Integer.toHexString(bit)); sb.append(" "); } return sb.toString(); } @Override protected void onPostExecute(String result) { if (result != null) { textView.setText("Hex ID: " + result); } } } }