ForegroundDispatch.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 The Android Open Source Project
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License
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         // Create a generic PendingIntent that will be deliver to this activity. The NFC stack
00059         // will fill in the intent with the details of the discovered tag before delivering to
00060         // this activity.
00061         mPendingIntent = PendingIntent.getActivity(this, 0,
00062                 new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
00063 
00064         // Setup an intent filter for all MIME based dispatches
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         // Setup a tech list for all NfcF tags
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);  // Remove data type (1 byte) and language code (2 bytes)
00101                     mText.append("\n\n\n" + tag_data);
00102                     finish();  // TODO add ok/cancel buttons instead
00103                 }
00104             }
00105 
00106 //            if (payload2[0] != NdefRecord.TNF_MIME_MEDIA)
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         // Prepare data in tent
00121         Intent data = new Intent();
00122         if (tag_data != null) {
00123             // Activity finished ok, return the data
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 }


android_remocons
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Wed Aug 26 2015 10:40:28