1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.example.android.apis.nfc;
18 
19 import com.example.android.apis.R;
20 
21 import android.app.Activity;
22 import android.app.PendingIntent;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.IntentFilter.MalformedMimeTypeException;
26 import android.nfc.NfcAdapter;
27 import android.nfc.tech.NfcF;
28 import android.os.Bundle;
29 import android.util.Log;
30 import android.widget.TextView;
31 
32 /**
33  * An example of how to use the NFC foreground dispatch APIs. This will intercept any MIME data
34  * based NDEF dispatch as well as all dispatched for NfcF tags.
35  */
36 public class ForegroundDispatch extends Activity {
37     private NfcAdapter mAdapter;
38     private PendingIntent mPendingIntent;
39     private IntentFilter[] mFilters;
40     private String[][] mTechLists;
41     private TextView mText;
42     private int mCount = 0;
43 
44     @Override
onCreate(Bundle savedState)45     public void onCreate(Bundle savedState) {
46         super.onCreate(savedState);
47 
48         setContentView(R.layout.foreground_dispatch);
49         mText = (TextView) findViewById(R.id.text);
50         mText.setText("Scan a tag");
51 
52         mAdapter = NfcAdapter.getDefaultAdapter(this);
53 
54         // Create a generic PendingIntent that will be deliver to this activity. The NFC stack
55         // will fill in the intent with the details of the discovered tag before delivering to
56         // this activity.
57         mPendingIntent = PendingIntent.getActivity(this, 0,
58                 new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
59 
60         // Setup an intent filter for all MIME based dispatches
61         IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
62         try {
63             ndef.addDataType("*/*");
64         } catch (MalformedMimeTypeException e) {
65             throw new RuntimeException("fail", e);
66         }
67         mFilters = new IntentFilter[] {
68                 ndef,
69         };
70 
71         // Setup a tech list for all NfcF tags
72         mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
73     }
74 
75     @Override
onResume()76     public void onResume() {
77         super.onResume();
78         if (mAdapter != null) mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
79                 mTechLists);
80     }
81 
82     @Override
onNewIntent(Intent intent)83     public void onNewIntent(Intent intent) {
84         Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
85         mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);
86     }
87 
88     @Override
onPause()89     public void onPause() {
90         super.onPause();
91         if (mAdapter != null) mAdapter.disableForegroundDispatch(this);
92     }
93 }
94