Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package com.github.rosjava.android_remocons.robot_remocon.nfc;
00018
00019 import android.app.Activity;
00020 import android.app.PendingIntent;
00021 import android.content.Intent;
00022 import android.content.IntentFilter;
00023 import android.content.IntentFilter.MalformedMimeTypeException;
00024 import android.nfc.NdefMessage;
00025 import android.nfc.NdefRecord;
00026 import android.nfc.NfcAdapter;
00027 import android.nfc.tech.*;
00028 import android.os.Bundle;
00029 import android.os.Parcelable;
00030 import android.util.Log;
00031 import android.widget.TextView;
00032
00033 import com.github.rosjava.android_remocons.robot_remocon.R;
00034
00039 public class ForegroundDispatch extends Activity {
00040 private NfcAdapter mAdapter;
00041 private PendingIntent mPendingIntent;
00042 private IntentFilter[] mFilters;
00043 private String[][] mTechLists;
00044 private TextView mText;
00045 private int mCount = 0;
00046 private String tag_data = null;
00047
00048 @Override
00049 public void onCreate(Bundle savedState) {
00050 super.onCreate(savedState);
00051
00052 setContentView(R.layout.nfc_tag_scan);
00053 mText = (TextView) findViewById(R.id.text);
00054 mText.setText("Scan a NFC tag");
00055
00056 mAdapter = NfcAdapter.getDefaultAdapter(this);
00057
00058
00059
00060
00061 mPendingIntent = PendingIntent.getActivity(this, 0,
00062 new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
00063
00064
00065 IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
00066 try {
00067 ndef.addDataType("text/plain");
00068 } catch (MalformedMimeTypeException e) {
00069 throw new RuntimeException("fail", e);
00070 }
00071 mFilters = new IntentFilter[] {
00072 ndef,
00073 };
00074
00075
00076 mTechLists = new String[][] { new String[] { NfcF.class.getName() },
00077 new String[] { NfcA.class.getName(), MifareClassic.class.getName() },
00078 new String[] { NfcA.class.getName(), MifareUltralight.class.getName() } };
00079 }
00080
00081 @Override
00082 public void onResume() {
00083 super.onResume();
00084 if (mAdapter != null) mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
00085 mTechLists);
00086 }
00087
00088 @Override
00089 public void onNewIntent(Intent intent) {
00090 Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
00091 mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);
00092
00093 if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
00094 Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
00095 if ((rawMsgs != null) && (rawMsgs.length > 0)) {
00096 NdefMessage msg = (NdefMessage) rawMsgs[0];
00097 NdefRecord[] records = msg.getRecords();
00098 if ((records != null) && (records.length > 0)) {
00099 String payload = new String(records[0].getPayload());
00100 tag_data = payload.substring(3);
00101 mText.append("\n\n\n" + tag_data);
00102 finish();
00103 }
00104 }
00105
00106
00107 }
00108
00109 mText.setText("Unrecognized NFC tag format");
00110 }
00111
00112 @Override
00113 public void onPause() {
00114 super.onPause();
00115 if (mAdapter != null) mAdapter.disableForegroundDispatch(this);
00116 }
00117
00118 @Override
00119 public void finish() {
00120
00121 Intent data = new Intent();
00122 if (tag_data != null) {
00123
00124 data.putExtra("tag_data", tag_data);
00125 setResult(RESULT_OK, data);
00126 }
00127 else {
00128 setResult(RESULT_CANCELED, data);
00129 }
00130
00131 super.finish();
00132 }
00133 }