blob: ca4a7d61ffd3697ecea485206b5c33bd63e2656a [file] [log] [blame]
Nick Pelly590b73b2010-10-12 13:00:50 -07001/*
Daniel Tomas90245642010-11-17 10:07:52 +01002 * Copyright (C) 2010 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.
Nick Pelly590b73b2010-10-12 13:00:50 -070015 */
16
17package android.nfc;
18
Jeff Hamiltonbb951c82011-11-08 16:55:13 -060019import java.util.HashMap;
20
Nick Pelly590b73b2010-10-12 13:00:50 -070021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Jeff Hamilton52d32032011-01-08 15:31:26 -060023import android.app.Activity;
Nick Pellyfdf90862010-10-18 11:19:41 -070024import android.app.ActivityThread;
Jeff Hamilton52d32032011-01-08 15:31:26 -060025import android.app.OnActivityPausedListener;
26import android.app.PendingIntent;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080027import android.content.Context;
Jeff Hamilton52d32032011-01-08 15:31:26 -060028import android.content.IntentFilter;
Nick Pellyfdf90862010-10-18 11:19:41 -070029import android.content.pm.IPackageManager;
30import android.content.pm.PackageManager;
Nick Pelly1d7e9062012-04-03 17:46:00 -070031import android.net.Uri;
Jeff Hamilton28319c02011-02-09 17:26:47 +090032import android.nfc.tech.MifareClassic;
33import android.nfc.tech.Ndef;
34import android.nfc.tech.NfcA;
35import android.nfc.tech.NfcF;
Nick Pelly590b73b2010-10-12 13:00:50 -070036import android.os.IBinder;
37import android.os.RemoteException;
38import android.os.ServiceManager;
39import android.util.Log;
40
Nick Pelly590b73b2010-10-12 13:00:50 -070041/**
Nick Pelly74fe6c62011-02-02 22:37:40 -080042 * Represents the local NFC adapter.
Nick Pelly590b73b2010-10-12 13:00:50 -070043 * <p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -080044 * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
45 * adapter for this Android device.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080046 *
47 * <div class="special reference">
48 * <h3>Developer Guides</h3>
49 * <p>For more information about using NFC, read the
50 * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p>
51 * </div>
Nick Pelly590b73b2010-10-12 13:00:50 -070052 */
53public final class NfcAdapter {
Nick Pellyc84c89a2011-08-22 22:27:11 -070054 static final String TAG = "NFC";
Nick Pelly50b4d8f2010-12-07 22:40:28 -080055
Nick Pelly590b73b2010-10-12 13:00:50 -070056 /**
Jeff Hamilton641dd622010-12-02 09:16:22 -060057 * Intent to start an activity when a tag with NDEF payload is discovered.
Jeff Hamilton641dd622010-12-02 09:16:22 -060058 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090059 * <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and
60 * looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the
61 * intent will contain the URI in its data field. If a MIME record is found the intent will
62 * contain the MIME type in its type field. This allows activities to register
63 * {@link IntentFilter}s targeting specific content on tags. Activities should register the
64 * most specific intent filters possible to avoid the activity chooser dialog, which can
65 * disrupt the interaction with the tag as the user interacts with the screen.
66 *
67 * <p>If the tag has an NDEF payload this intent is started before
68 * {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither
Nick Pellyf003e262011-01-31 23:27:37 -080069 * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started.
Nick Pellyc97a5522012-01-05 15:13:01 +110070 *
71 * <p>The MIME type or data URI of this intent are normalized before dispatch -
72 * so that MIME, URI scheme and URI host are always lower-case.
Jeff Hamilton641dd622010-12-02 09:16:22 -060073 */
74 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
75 public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED";
76
77 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +090078 * Intent to start an activity when a tag is discovered and activities are registered for the
79 * specific technologies on the tag.
Jeff Hamilton641dd622010-12-02 09:16:22 -060080 *
Jeff Hamilton28319c02011-02-09 17:26:47 +090081 * <p>To receive this intent an activity must include an intent filter
82 * for this action and specify the desired tech types in a
83 * manifest <code>meta-data</code> entry. Here is an example manfiest entry:
84 * <pre>
Scott Main97e0a1c2012-06-25 11:22:10 -070085 * &lt;activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"&gt;
86 * &lt;!-- Add a technology filter --&gt;
87 * &lt;intent-filter&gt;
88 * &lt;action android:name="android.nfc.action.TECH_DISCOVERED" /&gt;
89 * &lt;/intent-filter&gt;
Jeff Hamilton641dd622010-12-02 09:16:22 -060090 *
Scott Main97e0a1c2012-06-25 11:22:10 -070091 * &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
92 * android:resource="@xml/filter_nfc"
93 * /&gt;
94 * &lt;/activity&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +090095 *
96 * <p>The meta-data XML file should contain one or more <code>tech-list</code> entries
97 * each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer
98 * to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".
99 *
100 * <p>A tag matches if any of the
101 * <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each
102 * of the <code>tech-list</code>s is considered independently and the
103 * activity is considered a match is any single <code>tech-list</code> matches the tag that was
104 * discovered. This provides AND and OR semantics for filtering desired techs. Here is an
105 * example that will match any tag using {@link NfcF} or any tag using {@link NfcA},
106 * {@link MifareClassic}, and {@link Ndef}:
107 *
108 * <pre>
109 * &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
110 * &lt;!-- capture anything using NfcF --&gt;
111 * &lt;tech-list&gt;
112 * &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
113 * &lt;/tech-list&gt;
114 *
115 * &lt;!-- OR --&gt;
116 *
117 * &lt;!-- capture all MIFARE Classics with NDEF payloads --&gt;
118 * &lt;tech-list&gt;
119 * &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
120 * &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
121 * &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
122 * &lt;/tech-list&gt;
Scott Main97e0a1c2012-06-25 11:22:10 -0700123 * &lt;/resources&gt;</pre>
Jeff Hamilton28319c02011-02-09 17:26:47 +0900124 *
125 * <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before
126 * {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED}
127 * this intent will not be started. If any activities respond to this intent
128 * {@link #ACTION_TAG_DISCOVERED} will not be started.
Jeff Hamilton641dd622010-12-02 09:16:22 -0600129 */
130 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Nick Pellyf003e262011-01-31 23:27:37 -0800131 public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED";
Jeff Hamilton641dd622010-12-02 09:16:22 -0600132
133 /**
Nick Pelly11b075e2010-10-28 13:39:37 -0700134 * Intent to start an activity when a tag is discovered.
Jeff Hamilton28319c02011-02-09 17:26:47 +0900135 *
136 * <p>This intent will not be started when a tag is discovered if any activities respond to
Jason parks01425362011-05-24 02:57:37 -0700137 * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag.
Nick Pelly590b73b2010-10-12 13:00:50 -0700138 */
139 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
140 public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
141
142 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600143 * Broadcast to only the activity that handles ACTION_TAG_DISCOVERED
144 * @hide
145 */
146 public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST";
147
148 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900149 * Mandatory extra containing the {@link Tag} that was discovered for the
150 * {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
151 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly590b73b2010-10-12 13:00:50 -0700152 */
153 public static final String EXTRA_TAG = "android.nfc.extra.TAG";
154
155 /**
Nick Pellyc97a5522012-01-05 15:13:01 +1100156 * Extra containing an array of {@link NdefMessage} present on the discovered tag.<p>
157 * This extra is mandatory for {@link #ACTION_NDEF_DISCOVERED} intents,
158 * and optional for {@link #ACTION_TECH_DISCOVERED}, and
159 * {@link #ACTION_TAG_DISCOVERED} intents.<p>
160 * When this extra is present there will always be at least one
161 * {@link NdefMessage} element. Most NDEF tags have only one NDEF message,
162 * but we use an array for future compatibility.
Nick Pelly11b075e2010-10-28 13:39:37 -0700163 */
164 public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES";
165
166 /**
Jeff Hamilton28319c02011-02-09 17:26:47 +0900167 * Optional extra containing a byte array containing the ID of the discovered tag for
168 * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
169 * {@link #ACTION_TAG_DISCOVERED} intents.
Nick Pelly11b075e2010-10-28 13:39:37 -0700170 */
171 public static final String EXTRA_ID = "android.nfc.extra.ID";
172
173 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700174 * Broadcast Action: The state of the local NFC adapter has been
175 * changed.
176 * <p>For example, NFC has been turned on or off.
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800177 * <p>Always contains the extra field {@link #EXTRA_ADAPTER_STATE}
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700178 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700179 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
180 public static final String ACTION_ADAPTER_STATE_CHANGED =
181 "android.nfc.action.ADAPTER_STATE_CHANGED";
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700182
183 /**
Martijn Coenen4ba5eaf2013-01-08 12:49:47 -0800184 * Used as an int extra field in {@link #ACTION_ADAPTER_STATE_CHANGED}
Nick Pelly8d32a012011-08-09 07:03:49 -0700185 * intents to request the current power state. Possible values are:
186 * {@link #STATE_OFF},
187 * {@link #STATE_TURNING_ON},
188 * {@link #STATE_ON},
189 * {@link #STATE_TURNING_OFF},
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700190 */
Nick Pelly8d32a012011-08-09 07:03:49 -0700191 public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE";
192
Nick Pelly8d32a012011-08-09 07:03:49 -0700193 public static final int STATE_OFF = 1;
Nick Pelly8d32a012011-08-09 07:03:49 -0700194 public static final int STATE_TURNING_ON = 2;
Nick Pelly8d32a012011-08-09 07:03:49 -0700195 public static final int STATE_ON = 3;
Nick Pelly8d32a012011-08-09 07:03:49 -0700196 public static final int STATE_TURNING_OFF = 4;
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700197
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700198 /** @hide */
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800199 public static final int FLAG_NDEF_PUSH_NO_CONFIRM = 0x1;
200
201 /** @hide */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700202 public static final String ACTION_HANDOVER_TRANSFER_STARTED =
203 "android.nfc.action.HANDOVER_TRANSFER_STARTED";
204
205 /** @hide */
206 public static final String ACTION_HANDOVER_TRANSFER_DONE =
207 "android.nfc.action.HANDOVER_TRANSFER_DONE";
208
209 /** @hide */
210 public static final String EXTRA_HANDOVER_TRANSFER_STATUS =
211 "android.nfc.extra.HANDOVER_TRANSFER_STATUS";
212
213 /** @hide */
214 public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
215 /** @hide */
216 public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
217
218 /** @hide */
219 public static final String EXTRA_HANDOVER_TRANSFER_URI =
220 "android.nfc.extra.HANDOVER_TRANSFER_URI";
221
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800222 // Guarded by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700223 static boolean sIsInitialized = false;
Nick Pelly590b73b2010-10-12 13:00:50 -0700224
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800225 // Final after first constructor, except for
226 // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
227 // recovery
Nick Pellyc84c89a2011-08-22 22:27:11 -0700228 static INfcAdapter sService;
229 static INfcTag sTagService;
230
231 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600232 * The NfcAdapter object for each application context.
233 * There is a 1-1 relationship between application context and
234 * NfcAdapter object.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700235 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600236 static HashMap<Context, NfcAdapter> sNfcAdapters = new HashMap(); //guard by NfcAdapter.class
237
238 /**
239 * NfcAdapter used with a null context. This ctor was deprecated but we have
240 * to support it for backwards compatibility. New methods that require context
241 * might throw when called on the null-context NfcAdapter.
242 */
243 static NfcAdapter sNullContextNfcAdapter; // protected by NfcAdapter.class
Nick Pellyc84c89a2011-08-22 22:27:11 -0700244
245 final NfcActivityManager mNfcActivityManager;
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600246 final Context mContext;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700247
248 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700249 * A callback to be invoked when the system successfully delivers your {@link NdefMessage}
250 * to another device.
251 * @see #setOnNdefPushCompleteCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700252 */
253 public interface OnNdefPushCompleteCallback {
254 /**
255 * Called on successful NDEF push.
256 *
257 * <p>This callback is usually made on a binder thread (not the UI thread).
258 *
259 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
Scott Main2d68a6b2011-09-26 22:59:38 -0700260 * @see #setNdefPushMessageCallback
Nick Pellyc84c89a2011-08-22 22:27:11 -0700261 */
262 public void onNdefPushComplete(NfcEvent event);
263 }
264
265 /**
Scott Main2d68a6b2011-09-26 22:59:38 -0700266 * A callback to be invoked when another NFC device capable of NDEF push (Android Beam)
267 * is within range.
268 * <p>Implement this interface and pass it to {@link
269 * NfcAdapter#setNdefPushMessageCallback setNdefPushMessageCallback()} in order to create an
270 * {@link NdefMessage} at the moment that another device is within range for NFC. Using this
271 * callback allows you to create a message with data that might vary based on the
272 * content currently visible to the user. Alternatively, you can call {@link
273 * #setNdefPushMessage setNdefPushMessage()} if the {@link NdefMessage} always contains the
274 * same data.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700275 */
276 public interface CreateNdefMessageCallback {
277 /**
278 * Called to provide a {@link NdefMessage} to push.
279 *
280 * <p>This callback is usually made on a binder thread (not the UI thread).
281 *
282 * <p>Called when this device is in range of another device
283 * that might support NDEF push. It allows the application to
284 * create the NDEF message only when it is required.
285 *
286 * <p>NDEF push cannot occur until this method returns, so do not
287 * block for too long.
288 *
289 * <p>The Android operating system will usually show a system UI
290 * on top of your activity during this time, so do not try to request
291 * input from the user to complete the callback, or provide custom NDEF
292 * push UI. The user probably will not see it.
293 *
294 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set
295 * @return NDEF message to push, or null to not provide a message
296 */
297 public NdefMessage createNdefMessage(NfcEvent event);
298 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700299
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700300
301 // TODO javadoc
302 public interface CreateBeamUrisCallback {
303 public Uri[] createBeamUris(NfcEvent event);
304 }
305
Nick Pelly590b73b2010-10-12 13:00:50 -0700306 /**
Nick Pellyfdf90862010-10-18 11:19:41 -0700307 * Helper to check if this device has FEATURE_NFC, but without using
308 * a context.
309 * Equivalent to
310 * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)
311 */
312 private static boolean hasNfcFeature() {
313 IPackageManager pm = ActivityThread.getPackageManager();
314 if (pm == null) {
315 Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
316 return false;
317 }
318 try {
319 return pm.hasSystemFeature(PackageManager.FEATURE_NFC);
320 } catch (RemoteException e) {
321 Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
322 return false;
323 }
324 }
325
Nick Pellyc84c89a2011-08-22 22:27:11 -0700326 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600327 * Returns the NfcAdapter for application context,
328 * or throws if NFC is not available.
329 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700330 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600331 public static synchronized NfcAdapter getNfcAdapter(Context context) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800332 if (!sIsInitialized) {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800333 /* is this device meant to have NFC */
334 if (!hasNfcFeature()) {
335 Log.v(TAG, "this device does not have NFC support");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700336 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800337 }
338
339 sService = getServiceInterface();
340 if (sService == null) {
341 Log.e(TAG, "could not retrieve NFC service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700342 throw new UnsupportedOperationException();
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800343 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100344 try {
345 sTagService = sService.getNfcTagInterface();
346 } catch (RemoteException e) {
347 Log.e(TAG, "could not retrieve NFC Tag service");
Nick Pellyc84c89a2011-08-22 22:27:11 -0700348 throw new UnsupportedOperationException();
Nick Pelly3dd6c452011-01-10 18:14:41 +1100349 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600350
351 sIsInitialized = true;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800352 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600353 if (context == null) {
354 if (sNullContextNfcAdapter == null) {
355 sNullContextNfcAdapter = new NfcAdapter(null);
356 }
357 return sNullContextNfcAdapter;
Nick Pellyc84c89a2011-08-22 22:27:11 -0700358 }
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600359 NfcAdapter adapter = sNfcAdapters.get(context);
360 if (adapter == null) {
361 adapter = new NfcAdapter(context);
362 sNfcAdapters.put(context, adapter);
363 }
364 return adapter;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800365 }
366
Nick Pelly6d55e132010-10-27 01:14:43 -0700367 /** get handle to NFC service interface */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800368 private static INfcAdapter getServiceInterface() {
Nick Pelly6d55e132010-10-27 01:14:43 -0700369 /* get a handle to NFC service */
370 IBinder b = ServiceManager.getService("nfc");
371 if (b == null) {
372 return null;
373 }
374 return INfcAdapter.Stub.asInterface(b);
375 }
376
Nick Pellyfdf90862010-10-18 11:19:41 -0700377 /**
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800378 * Helper to get the default NFC Adapter.
379 * <p>
380 * Most Android devices will only have one NFC Adapter (NFC Controller).
381 * <p>
382 * This helper is the equivalent of:
Scott Main97e0a1c2012-06-25 11:22:10 -0700383 * <pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800384 * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Scott Main97e0a1c2012-06-25 11:22:10 -0700385 * NfcAdapter adapter = manager.getDefaultAdapter();</pre>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800386 * @param context the calling application's context
387 *
388 * @return the default NFC adapter, or null if no NFC adapter exists
389 */
390 public static NfcAdapter getDefaultAdapter(Context context) {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600391 if (context == null) {
392 throw new IllegalArgumentException("context cannot be null");
393 }
394 context = context.getApplicationContext();
Nick Pellyb04cce02011-11-21 17:02:02 -0800395 if (context == null) {
396 throw new IllegalArgumentException(
397 "context not associated with any application (using a mock context?)");
398 }
399 /* use getSystemService() for consistency */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800400 NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
Nick Pellya5193b242011-11-16 16:46:27 -0800401 if (manager == null) {
402 // NFC not available
403 return null;
404 }
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800405 return manager.getDefaultAdapter();
406 }
407
408 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600409 * Legacy NfcAdapter getter, always use {@link #getDefaultAdapter(Context)} instead.<p>
410 * This method was deprecated at API level 10 (Gingerbread MR1) because a context is required
411 * for many NFC API methods. Those methods will fail when called on an NfcAdapter
412 * object created from this method.<p>
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800413 * @deprecated use {@link #getDefaultAdapter(Context)}
Nick Pellya356bf12011-12-13 15:36:31 -0800414 * @hide
Nick Pelly590b73b2010-10-12 13:00:50 -0700415 */
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800416 @Deprecated
Nick Pelly590b73b2010-10-12 13:00:50 -0700417 public static NfcAdapter getDefaultAdapter() {
Nick Pellyc97a5522012-01-05 15:13:01 +1100418 // introduced in API version 9 (GB 2.3)
Nick Pellya356bf12011-12-13 15:36:31 -0800419 // deprecated in API version 10 (GB 2.3.3)
420 // removed from public API in version 16 (ICS MR2)
Nick Pellyc97a5522012-01-05 15:13:01 +1100421 // should maintain as a hidden API for binary compatibility for a little longer
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800422 Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " +
423 "NfcAdapter.getDefaultAdapter(Context) instead", new Exception());
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600424
425 return NfcAdapter.getNfcAdapter(null);
426 }
427
428 NfcAdapter(Context context) {
429 mContext = context;
430 mNfcActivityManager = new NfcActivityManager(this);
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800431 }
Nick Pelly590b73b2010-10-12 13:00:50 -0700432
Nick Pellyc84c89a2011-08-22 22:27:11 -0700433 /**
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600434 * @hide
Nick Pellyc84c89a2011-08-22 22:27:11 -0700435 */
Jeff Hamiltonbb951c82011-11-08 16:55:13 -0600436 public Context getContext() {
437 return mContext;
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800438 }
439
440 /**
441 * Returns the binder interface to the service.
442 * @hide
443 */
444 public INfcAdapter getService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800445 isEnabled(); // NOP call to recover sService if it is stale
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800446 return sService;
Nick Pelly590b73b2010-10-12 13:00:50 -0700447 }
448
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600449 /**
Nick Pelly3dd6c452011-01-10 18:14:41 +1100450 * Returns the binder interface to the tag service.
451 * @hide
452 */
453 public INfcTag getTagService() {
Nick Pelly253c5092011-01-13 09:22:57 -0800454 isEnabled(); // NOP call to recover sTagService if it is stale
Nick Pelly3dd6c452011-01-10 18:14:41 +1100455 return sTagService;
456 }
457
458 /**
Jeff Hamilton6be655c2010-11-12 12:28:16 -0600459 * NFC service dead - attempt best effort recovery
460 * @hide
461 */
462 public void attemptDeadServiceRecovery(Exception e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700463 Log.e(TAG, "NFC service dead - attempting to recover", e);
464 INfcAdapter service = getServiceInterface();
465 if (service == null) {
466 Log.e(TAG, "could not retrieve NFC service during service recovery");
Nick Pelly3dd6c452011-01-10 18:14:41 +1100467 // nothing more can be done now, sService is still stale, we'll hit
468 // this recovery path again later
Nick Pelly6d55e132010-10-27 01:14:43 -0700469 return;
470 }
Nick Pelly3dd6c452011-01-10 18:14:41 +1100471 // assigning to sService is not thread-safe, but this is best-effort code
472 // and on a well-behaved system should never happen
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800473 sService = service;
Nick Pelly3dd6c452011-01-10 18:14:41 +1100474 try {
475 sTagService = service.getNfcTagInterface();
476 } catch (RemoteException ee) {
477 Log.e(TAG, "could not retrieve NFC tag service during service recovery");
478 // nothing more can be done now, sService is still stale, we'll hit
479 // this recovery path again later
480 }
481
Nick Pelly6d55e132010-10-27 01:14:43 -0700482 return;
483 }
484
Nick Pelly590b73b2010-10-12 13:00:50 -0700485 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700486 * Return true if this NFC Adapter has any features enabled.
Nick Pelly590b73b2010-10-12 13:00:50 -0700487 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800488 * <p>If this method returns false, the NFC hardware is guaranteed not to
Nick Pellycccf01d2011-10-31 14:49:40 -0700489 * generate or respond to any NFC communication over its NFC radio.
490 * <p>Applications can use this to check if NFC is enabled. Applications
491 * can request Settings UI allowing the user to toggle NFC using:
492 * <p><pre>startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))</pre>
Nick Pelly74fe6c62011-02-02 22:37:40 -0800493 *
Nick Pellycccf01d2011-10-31 14:49:40 -0700494 * @see android.provider.Settings#ACTION_NFC_SETTINGS
Nick Pelly74fe6c62011-02-02 22:37:40 -0800495 * @return true if this NFC Adapter has any features enabled
Nick Pelly590b73b2010-10-12 13:00:50 -0700496 */
Nick Pelly7ea5c452010-10-20 18:39:11 -0700497 public boolean isEnabled() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700498 try {
Nick Pelly8d32a012011-08-09 07:03:49 -0700499 return sService.getState() == STATE_ON;
Nick Pelly590b73b2010-10-12 13:00:50 -0700500 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700501 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700502 return false;
503 }
504 }
505
506 /**
Nick Pelly8d32a012011-08-09 07:03:49 -0700507 * Return the state of this NFC Adapter.
508 *
509 * <p>Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON},
510 * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}.
511 *
512 * <p>{@link #isEnabled()} is equivalent to
513 * <code>{@link #getAdapterState()} == {@link #STATE_ON}</code>
514 *
515 * @return the current state of this NFC adapter
516 *
517 * @hide
518 */
519 public int getAdapterState() {
520 try {
521 return sService.getState();
522 } catch (RemoteException e) {
523 attemptDeadServiceRecovery(e);
524 return NfcAdapter.STATE_OFF;
525 }
526 }
527
528 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700529 * Enable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700530 *
531 * <p>This call is asynchronous. Listen for
532 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
533 * operation is complete.
534 *
535 * <p>If this returns true, then either NFC is already on, or
536 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
537 * to indicate a state transition. If this returns false, then
538 * there is some problem that prevents an attempt to turn
539 * NFC on (for example we are in airplane mode and NFC is not
540 * toggleable in airplane mode on this platform).
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700541 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700542 * @hide
543 */
Nick Pelly7ea5c452010-10-20 18:39:11 -0700544 public boolean enable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700545 try {
Nick Pelly50b4d8f2010-12-07 22:40:28 -0800546 return sService.enable();
Nick Pelly590b73b2010-10-12 13:00:50 -0700547 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700548 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700549 return false;
550 }
551 }
552
553 /**
Nick Pelly7ea5c452010-10-20 18:39:11 -0700554 * Disable NFC hardware.
Nick Pelly8d32a012011-08-09 07:03:49 -0700555 *
556 * <p>No NFC features will work after this call, and the hardware
Nick Pelly7ea5c452010-10-20 18:39:11 -0700557 * will not perform or respond to any NFC communication.
Nick Pelly8d32a012011-08-09 07:03:49 -0700558 *
559 * <p>This call is asynchronous. Listen for
560 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the
561 * operation is complete.
562 *
563 * <p>If this returns true, then either NFC is already off, or
564 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent
565 * to indicate a state transition. If this returns false, then
566 * there is some problem that prevents an attempt to turn
567 * NFC off.
Brad Fitzpatrickafb082d2010-10-18 14:02:58 -0700568 *
Nick Pelly590b73b2010-10-12 13:00:50 -0700569 * @hide
570 */
Sunil Jogi3bba8d02012-04-10 13:12:26 -0700571
Nick Pelly7ea5c452010-10-20 18:39:11 -0700572 public boolean disable() {
Nick Pelly590b73b2010-10-12 13:00:50 -0700573 try {
Sunil Jogi3bba8d02012-04-10 13:12:26 -0700574 return sService.disable(true);
Nick Pelly590b73b2010-10-12 13:00:50 -0700575 } catch (RemoteException e) {
Nick Pelly6d55e132010-10-27 01:14:43 -0700576 attemptDeadServiceRecovery(e);
Nick Pelly590b73b2010-10-12 13:00:50 -0700577 return false;
578 }
579 }
580
Martijn Coenen2c103112012-05-15 10:32:15 -0700581 /**
582 * Set one or more {@link Uri}s to send using Android Beam (TM). Every
583 * Uri you provide must have either scheme 'file' or scheme 'content'.
584 *
585 * <p>For the data provided through this method, Android Beam tries to
586 * switch to alternate transports such as Bluetooth to achieve a fast
587 * transfer speed. Hence this method is very suitable
588 * for transferring large files such as pictures or songs.
589 *
590 * <p>The receiving side will store the content of each Uri in
591 * a file and present a notification to the user to open the file
592 * with a {@link android.content.Intent} with action
593 * {@link android.content.Intent#ACTION_VIEW}.
594 * If multiple URIs are sent, the {@link android.content.Intent} will refer
595 * to the first of the stored files.
596 *
597 * <p>This method may be called at any time before {@link Activity#onDestroy},
598 * but the URI(s) are only made available for Android Beam when the
599 * specified activity(s) are in resumed (foreground) state. The recommended
600 * approach is to call this method during your Activity's
601 * {@link Activity#onCreate} - see sample
602 * code below. This method does not immediately perform any I/O or blocking work,
603 * so is safe to call on your main thread.
604 *
605 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
606 * have priority over both {@link #setNdefPushMessage} and
607 * {@link #setNdefPushMessageCallback}.
608 *
609 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
610 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
611 * then the Uri push will be completely disabled for the specified activity(s).
612 *
613 * <p>Code example:
614 * <pre>
615 * protected void onCreate(Bundle savedInstanceState) {
616 * super.onCreate(savedInstanceState);
617 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
618 * if (nfcAdapter == null) return; // NFC not available on this device
619 * nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700620 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700621 * And that is it. Only one call per activity is necessary. The Android
622 * OS will automatically release its references to the Uri(s) and the
623 * Activity object when it is destroyed if you follow this pattern.
624 *
625 * <p>If your Activity wants to dynamically supply Uri(s),
626 * then set a callback using {@link #setBeamPushUrisCallback} instead
627 * of using this method.
628 *
629 * <p class="note">Do not pass in an Activity that has already been through
630 * {@link Activity#onDestroy}. This is guaranteed if you call this API
631 * during {@link Activity#onCreate}.
632 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700633 * <p class="note">If this device does not support alternate transports
634 * such as Bluetooth or WiFI, calling this method does nothing.
635 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700636 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
637 *
638 * @param uris an array of Uri(s) to push over Android Beam
639 * @param activity activity for which the Uri(s) will be pushed
640 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700641 public void setBeamPushUris(Uri[] uris, Activity activity) {
Nick Pelly1d7e9062012-04-03 17:46:00 -0700642 if (activity == null) {
643 throw new NullPointerException("activity cannot be null");
644 }
Martijn Coenen2c103112012-05-15 10:32:15 -0700645 if (uris != null) {
646 for (Uri uri : uris) {
647 if (uri == null) throw new NullPointerException("Uri not " +
648 "allowed to be null");
649 String scheme = uri.getScheme();
650 if (scheme == null || (!scheme.equalsIgnoreCase("file") &&
651 !scheme.equalsIgnoreCase("content"))) {
652 throw new IllegalArgumentException("URI needs to have " +
653 "either scheme file or scheme content");
654 }
655 }
656 }
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700657 mNfcActivityManager.setNdefPushContentUri(activity, uris);
658 }
659
Martijn Coenen2c103112012-05-15 10:32:15 -0700660 /**
661 * Set a callback that will dynamically generate one or more {@link Uri}s
662 * to send using Android Beam (TM). Every Uri the callback provides
663 * must have either scheme 'file' or scheme 'content'.
664 *
665 * <p>For the data provided through this callback, Android Beam tries to
666 * switch to alternate transports such as Bluetooth to achieve a fast
667 * transfer speed. Hence this method is very suitable
668 * for transferring large files such as pictures or songs.
669 *
670 * <p>The receiving side will store the content of each Uri in
671 * a file and present a notification to the user to open the file
672 * with a {@link android.content.Intent} with action
673 * {@link android.content.Intent#ACTION_VIEW}.
674 * If multiple URIs are sent, the {@link android.content.Intent} will refer
675 * to the first of the stored files.
676 *
677 * <p>This method may be called at any time before {@link Activity#onDestroy},
678 * but the URI(s) are only made available for Android Beam when the
679 * specified activity(s) are in resumed (foreground) state. The recommended
680 * approach is to call this method during your Activity's
681 * {@link Activity#onCreate} - see sample
682 * code below. This method does not immediately perform any I/O or blocking work,
683 * so is safe to call on your main thread.
684 *
685 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback}
686 * have priority over both {@link #setNdefPushMessage} and
687 * {@link #setNdefPushMessageCallback}.
688 *
689 * <p>If {@link #setBeamPushUris} is called with a null Uri array,
690 * and/or {@link #setBeamPushUrisCallback} is called with a null callback,
691 * then the Uri push will be completely disabled for the specified activity(s).
692 *
693 * <p>Code example:
694 * <pre>
695 * protected void onCreate(Bundle savedInstanceState) {
696 * super.onCreate(savedInstanceState);
697 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
698 * if (nfcAdapter == null) return; // NFC not available on this device
699 * nfcAdapter.setBeamPushUrisCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700700 * }</pre>
Martijn Coenen2c103112012-05-15 10:32:15 -0700701 * And that is it. Only one call per activity is necessary. The Android
702 * OS will automatically release its references to the Uri(s) and the
703 * Activity object when it is destroyed if you follow this pattern.
704 *
705 * <p class="note">Do not pass in an Activity that has already been through
706 * {@link Activity#onDestroy}. This is guaranteed if you call this API
707 * during {@link Activity#onCreate}.
708 *
Martijn Coenen3b6ecf02012-06-06 13:07:47 -0700709 * <p class="note">If this device does not support alternate transports
710 * such as Bluetooth or WiFI, calling this method does nothing.
711 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700712 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
713 *
714 * @param callback callback, or null to disable
715 * @param activity activity for which the Uri(s) will be pushed
716 */
Martijn Coenen20e8dd92012-04-12 16:37:18 -0700717 public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) {
718 if (activity == null) {
719 throw new NullPointerException("activity cannot be null");
720 }
721 mNfcActivityManager.setNdefPushContentUriCallback(activity, callback);
Nick Pelly1d7e9062012-04-03 17:46:00 -0700722 }
723
Jeff Hamilton52d32032011-01-08 15:31:26 -0600724 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -0700725 * Set a static {@link NdefMessage} to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -0700726 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700727 * <p>This method may be called at any time before {@link Activity#onDestroy},
728 * but the NDEF message is only made available for NDEF push when the
729 * specified activity(s) are in resumed (foreground) state. The recommended
730 * approach is to call this method during your Activity's
731 * {@link Activity#onCreate} - see sample
732 * code below. This method does not immediately perform any I/O or blocking work,
733 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700734 *
735 * <p>Only one NDEF message can be pushed by the currently resumed activity.
736 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -0700737 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -0700738 * the callback will take priority.
739 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700740 * <p>If neither {@link #setNdefPushMessage} or
741 * {@link #setNdefPushMessageCallback} have been called for your activity, then
742 * the Android OS may choose to send a default NDEF message on your behalf,
743 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700744 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700745 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
746 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
747 * then NDEF push will be completely disabled for the specified activity(s).
748 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -0700749 * otherwise sent on your behalf for those activity(s).
750 *
751 * <p>If you want to prevent the Android OS from sending default NDEF
752 * messages completely (for all activities), you can include a
Scott Main97e0a1c2012-06-25 11:22:10 -0700753 * {@code &lt;meta-data>} element inside the {@code &lt;application>}
Martijn Coenen854e0772012-04-02 14:52:10 -0700754 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -0700755 * <pre>
756 * &lt;application ...>
757 * &lt;meta-data android:name="android.nfc.disable_beam_default"
758 * android:value="true" />
759 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700760 *
761 * <p>The API allows for multiple activities to be specified at a time,
762 * but it is strongly recommended to just register one at a time,
763 * and to do so during the activity's {@link Activity#onCreate}. For example:
764 * <pre>
765 * protected void onCreate(Bundle savedInstanceState) {
766 * super.onCreate(savedInstanceState);
767 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
768 * if (nfcAdapter == null) return; // NFC not available on this device
769 * nfcAdapter.setNdefPushMessage(ndefMessage, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700770 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700771 * And that is it. Only one call per activity is necessary. The Android
772 * OS will automatically release its references to the NDEF message and the
773 * Activity object when it is destroyed if you follow this pattern.
774 *
775 * <p>If your Activity wants to dynamically generate an NDEF message,
776 * then set a callback using {@link #setNdefPushMessageCallback} instead
777 * of a static message.
778 *
779 * <p class="note">Do not pass in an Activity that has already been through
780 * {@link Activity#onDestroy}. This is guaranteed if you call this API
781 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -0700782 *
Martijn Coenen2c103112012-05-15 10:32:15 -0700783 * <p class="note">For sending large content such as pictures and songs,
784 * consider using {@link #setBeamPushUris}, which switches to alternate transports
785 * such as Bluetooth to achieve a fast transfer rate.
786 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700787 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
788 *
789 * @param message NDEF message to push over NFC, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -0700790 * @param activity activity for which the NDEF message will be pushed
791 * @param activities optional additional activities, however we strongly recommend
792 * to only register one at a time, and to do so in that activity's
793 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -0700794 */
Nick Pelly82328bf2011-08-30 09:37:25 -0700795 public void setNdefPushMessage(NdefMessage message, Activity activity,
796 Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -0700797 int targetSdkVersion = getSdkVersion();
798 try {
799 if (activity == null) {
800 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -0700801 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800802 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -0700803 for (Activity a : activities) {
804 if (a == null) {
805 throw new NullPointerException("activities cannot contain null");
806 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800807 mNfcActivityManager.setNdefPushMessage(a, message, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -0700808 }
809 } catch (IllegalStateException e) {
810 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
811 // Less strict on old applications - just log the error
812 Log.e(TAG, "Cannot call API with Activity that has already " +
813 "been destroyed", e);
814 } else {
815 // Prevent new applications from making this mistake, re-throw
816 throw(e);
817 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700818 }
819 }
820
821 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800822 * @hide
823 */
824 public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) {
825 if (activity == null) {
826 throw new NullPointerException("activity cannot be null");
827 }
828 mNfcActivityManager.setNdefPushMessage(activity, message, flags);
829 }
830
831 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -0700832 * Set a callback that dynamically generates NDEF messages to send using Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -0700833 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700834 * <p>This method may be called at any time before {@link Activity#onDestroy},
835 * but the NDEF message callback can only occur when the
836 * specified activity(s) are in resumed (foreground) state. The recommended
837 * approach is to call this method during your Activity's
838 * {@link Activity#onCreate} - see sample
839 * code below. This method does not immediately perform any I/O or blocking work,
840 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700841 *
842 * <p>Only one NDEF message can be pushed by the currently resumed activity.
843 * If both {@link #setNdefPushMessage} and
Nick Pelly8ce7a272012-03-21 15:14:09 -0700844 * {@link #setNdefPushMessageCallback} are set, then
Nick Pellyc84c89a2011-08-22 22:27:11 -0700845 * the callback will take priority.
846 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700847 * <p>If neither {@link #setNdefPushMessage} or
848 * {@link #setNdefPushMessageCallback} have been called for your activity, then
849 * the Android OS may choose to send a default NDEF message on your behalf,
850 * such as a URI for your application.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700851 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700852 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message,
853 * and/or {@link #setNdefPushMessageCallback} is called with a null callback,
854 * then NDEF push will be completely disabled for the specified activity(s).
855 * This also disables any default NDEF message the Android OS would have
Martijn Coenen854e0772012-04-02 14:52:10 -0700856 * otherwise sent on your behalf for those activity(s).
857 *
858 * <p>If you want to prevent the Android OS from sending default NDEF
859 * messages completely (for all activities), you can include a
Scott Main97e0a1c2012-06-25 11:22:10 -0700860 * {@code &lt;meta-data>} element inside the {@code &lt;application>}
Martijn Coenen854e0772012-04-02 14:52:10 -0700861 * element of your AndroidManifest.xml file, like this:
Scott Main97e0a1c2012-06-25 11:22:10 -0700862 * <pre>
863 * &lt;application ...>
864 * &lt;meta-data android:name="android.nfc.disable_beam_default"
865 * android:value="true" />
866 * &lt;/application></pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700867 *
868 * <p>The API allows for multiple activities to be specified at a time,
869 * but it is strongly recommended to just register one at a time,
870 * and to do so during the activity's {@link Activity#onCreate}. For example:
871 * <pre>
872 * protected void onCreate(Bundle savedInstanceState) {
873 * super.onCreate(savedInstanceState);
874 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
875 * if (nfcAdapter == null) return; // NFC not available on this device
876 * nfcAdapter.setNdefPushMessageCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700877 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700878 * And that is it. Only one call per activity is necessary. The Android
879 * OS will automatically release its references to the callback and the
880 * Activity object when it is destroyed if you follow this pattern.
881 *
882 * <p class="note">Do not pass in an Activity that has already been through
883 * {@link Activity#onDestroy}. This is guaranteed if you call this API
884 * during {@link Activity#onCreate}.
Martijn Coenen2c103112012-05-15 10:32:15 -0700885 * <p class="note">For sending large content such as pictures and songs,
886 * consider using {@link #setBeamPushUris}, which switches to alternate transports
887 * such as Bluetooth to achieve a fast transfer rate.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700888 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
889 *
890 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -0700891 * @param activity activity for which the NDEF message will be pushed
892 * @param activities optional additional activities, however we strongly recommend
893 * to only register one at a time, and to do so in that activity's
894 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -0700895 */
Nick Pelly82328bf2011-08-30 09:37:25 -0700896 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
Nick Pellyc84c89a2011-08-22 22:27:11 -0700897 Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -0700898 int targetSdkVersion = getSdkVersion();
899 try {
900 if (activity == null) {
901 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -0700902 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800903 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -0700904 for (Activity a : activities) {
905 if (a == null) {
906 throw new NullPointerException("activities cannot contain null");
907 }
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800908 mNfcActivityManager.setNdefPushMessageCallback(a, callback, 0);
Nick Pelly8ce7a272012-03-21 15:14:09 -0700909 }
910 } catch (IllegalStateException e) {
911 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
912 // Less strict on old applications - just log the error
913 Log.e(TAG, "Cannot call API with Activity that has already " +
914 "been destroyed", e);
915 } else {
916 // Prevent new applications from making this mistake, re-throw
917 throw(e);
918 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700919 }
920 }
921
922 /**
Martijn Coenen1fa2aff2013-02-27 09:21:22 -0800923 * @hide
924 */
925 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
926 int flags) {
927 if (activity == null) {
928 throw new NullPointerException("activity cannot be null");
929 }
930 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, flags);
931 }
932
933 /**
Nick Pelly8ce7a272012-03-21 15:14:09 -0700934 * Set a callback on successful Android Beam (TM).
Nick Pellyc84c89a2011-08-22 22:27:11 -0700935 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700936 * <p>This method may be called at any time before {@link Activity#onDestroy},
937 * but the callback can only occur when the
938 * specified activity(s) are in resumed (foreground) state. The recommended
939 * approach is to call this method during your Activity's
940 * {@link Activity#onCreate} - see sample
941 * code below. This method does not immediately perform any I/O or blocking work,
942 * so is safe to call on your main thread.
Nick Pellyc84c89a2011-08-22 22:27:11 -0700943 *
Nick Pelly8ce7a272012-03-21 15:14:09 -0700944 * <p>The API allows for multiple activities to be specified at a time,
945 * but it is strongly recommended to just register one at a time,
946 * and to do so during the activity's {@link Activity#onCreate}. For example:
947 * <pre>
948 * protected void onCreate(Bundle savedInstanceState) {
949 * super.onCreate(savedInstanceState);
950 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
951 * if (nfcAdapter == null) return; // NFC not available on this device
952 * nfcAdapter.setOnNdefPushCompleteCallback(callback, this);
Scott Main97e0a1c2012-06-25 11:22:10 -0700953 * }</pre>
Nick Pelly8ce7a272012-03-21 15:14:09 -0700954 * And that is it. Only one call per activity is necessary. The Android
955 * OS will automatically release its references to the callback and the
956 * Activity object when it is destroyed if you follow this pattern.
957 *
958 * <p class="note">Do not pass in an Activity that has already been through
959 * {@link Activity#onDestroy}. This is guaranteed if you call this API
960 * during {@link Activity#onCreate}.
Nick Pelly82328bf2011-08-30 09:37:25 -0700961 *
Nick Pellyc84c89a2011-08-22 22:27:11 -0700962 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
963 *
964 * @param callback callback, or null to disable
Nick Pelly8ce7a272012-03-21 15:14:09 -0700965 * @param activity activity for which the NDEF message will be pushed
966 * @param activities optional additional activities, however we strongly recommend
967 * to only register one at a time, and to do so in that activity's
968 * {@link Activity#onCreate}
Nick Pellyc84c89a2011-08-22 22:27:11 -0700969 */
970 public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback,
Nick Pelly82328bf2011-08-30 09:37:25 -0700971 Activity activity, Activity ... activities) {
Nick Pelly8ce7a272012-03-21 15:14:09 -0700972 int targetSdkVersion = getSdkVersion();
973 try {
974 if (activity == null) {
975 throw new NullPointerException("activity cannot be null");
Nick Pelly82328bf2011-08-30 09:37:25 -0700976 }
Nick Pelly8ce7a272012-03-21 15:14:09 -0700977 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback);
978 for (Activity a : activities) {
979 if (a == null) {
980 throw new NullPointerException("activities cannot contain null");
981 }
982 mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback);
983 }
984 } catch (IllegalStateException e) {
985 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
986 // Less strict on old applications - just log the error
987 Log.e(TAG, "Cannot call API with Activity that has already " +
988 "been destroyed", e);
989 } else {
990 // Prevent new applications from making this mistake, re-throw
991 throw(e);
992 }
Nick Pellyc84c89a2011-08-22 22:27:11 -0700993 }
994 }
995
996 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -0800997 * Enable foreground dispatch to the given Activity.
Jeff Hamilton52d32032011-01-08 15:31:26 -0600998 *
Nick Pelly74fe6c62011-02-02 22:37:40 -0800999 * <p>This will give give priority to the foreground activity when
1000 * dispatching a discovered {@link Tag} to an application.
1001 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001002 * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents
1003 * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and
1004 * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED}
1005 * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled
1006 * by passing in the tech lists separately. Each first level entry in the tech list represents
1007 * an array of technologies that must all be present to match. If any of the first level sets
1008 * match then the dispatch is routed through the given PendingIntent. In other words, the second
1009 * level is ANDed together and the first level entries are ORed together.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001010 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001011 * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters
1012 * that acts a wild card and will cause the foreground activity to receive all tags via the
1013 * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001014 *
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001015 * <p>This method must be called from the main thread, and only when the activity is in the
1016 * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before
1017 * the completion of their {@link Activity#onPause} callback to disable foreground dispatch
1018 * after it has been enabled.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001019 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001020 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1021 *
Jeff Hamilton52d32032011-01-08 15:31:26 -06001022 * @param activity the Activity to dispatch to
1023 * @param intent the PendingIntent to start for the dispatch
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001024 * @param filters the IntentFilters to override dispatching for, or null to always dispatch
Jeff Hamilton167d9e42011-02-09 12:27:01 +09001025 * @param techLists the tech lists used to perform matching for dispatching of the
1026 * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
Nick Pelly74fe6c62011-02-02 22:37:40 -08001027 * @throws IllegalStateException if the Activity is not currently in the foreground
Jeff Hamilton52d32032011-01-08 15:31:26 -06001028 */
1029 public void enableForegroundDispatch(Activity activity, PendingIntent intent,
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001030 IntentFilter[] filters, String[][] techLists) {
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001031 if (activity == null || intent == null) {
Jeff Hamilton52d32032011-01-08 15:31:26 -06001032 throw new NullPointerException();
1033 }
1034 if (!activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001035 throw new IllegalStateException("Foreground dispatch can only be enabled " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001036 "when your activity is resumed");
1037 }
1038 try {
Jeff Hamiltond88e9aa2011-01-24 14:53:00 -06001039 TechListParcel parcel = null;
1040 if (techLists != null && techLists.length > 0) {
1041 parcel = new TechListParcel(techLists);
1042 }
Jeff Hamilton52d32032011-01-08 15:31:26 -06001043 ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001044 mForegroundDispatchListener);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001045 sService.setForegroundDispatch(intent, filters, parcel);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001046 } catch (RemoteException e) {
1047 attemptDeadServiceRecovery(e);
1048 }
1049 }
1050
1051 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001052 * Disable foreground dispatch to the given activity.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001053 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001054 * <p>After calling {@link #enableForegroundDispatch}, an activity
1055 * must call this method before its {@link Activity#onPause} callback
1056 * completes.
Jeff Hamilton52d32032011-01-08 15:31:26 -06001057 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001058 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001059 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001060 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1061 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001062 * @param activity the Activity to disable dispatch to
1063 * @throws IllegalStateException if the Activity has already been paused
Jeff Hamilton52d32032011-01-08 15:31:26 -06001064 */
1065 public void disableForegroundDispatch(Activity activity) {
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001066 ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
1067 mForegroundDispatchListener);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001068 disableForegroundDispatchInternal(activity, false);
1069 }
1070
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001071 OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001072 @Override
1073 public void onPaused(Activity activity) {
1074 disableForegroundDispatchInternal(activity, true);
1075 }
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001076 };
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001077
Jeff Hamilton52d32032011-01-08 15:31:26 -06001078 void disableForegroundDispatchInternal(Activity activity, boolean force) {
1079 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001080 sService.setForegroundDispatch(null, null, null);
Jeff Hamilton52d32032011-01-08 15:31:26 -06001081 if (!force && !activity.isResumed()) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001082 throw new IllegalStateException("You must disable foreground dispatching " +
Jeff Hamilton52d32032011-01-08 15:31:26 -06001083 "while your activity is still resumed");
1084 }
1085 } catch (RemoteException e) {
1086 attemptDeadServiceRecovery(e);
1087 }
1088 }
1089
Nick Pelly590b73b2010-10-12 13:00:50 -07001090 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001091 * Enable NDEF message push over NFC while this Activity is in the foreground.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001092 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001093 * <p>You must explicitly call this method every time the activity is
1094 * resumed, and you must call {@link #disableForegroundNdefPush} before
1095 * your activity completes {@link Activity#onPause}.
1096 *
1097 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1098 * instead: it automatically hooks into your activity life-cycle,
1099 * so you do not need to call enable/disable in your onResume/onPause.
1100 *
1101 * <p>For NDEF push to function properly the other NFC device must
1102 * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or
1103 * Android's "com.android.npp" (Ndef Push Protocol). This was optional
1104 * on Gingerbread level Android NFC devices, but SNEP is mandatory on
1105 * Ice-Cream-Sandwich and beyond.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001106 *
1107 * <p>This method must be called from the main thread.
Jeff Hamilton9f20cd72011-01-23 12:14:42 -06001108 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001109 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1110 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001111 * @param activity foreground activity
1112 * @param message a NDEF Message to push over NFC
1113 * @throws IllegalStateException if the activity is not currently in the foreground
1114 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001115 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001116 @Deprecated
1117 public void enableForegroundNdefPush(Activity activity, NdefMessage message) {
1118 if (activity == null || message == null) {
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001119 throw new NullPointerException();
1120 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001121 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001122 mNfcActivityManager.setNdefPushMessage(activity, message, 0);
Jason parks01425362011-05-24 02:57:37 -07001123 }
1124
1125 /**
Nick Pelly74fe6c62011-02-02 22:37:40 -08001126 * Disable NDEF message push over P2P.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001127 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001128 * <p>After calling {@link #enableForegroundNdefPush}, an activity
1129 * must call this method before its {@link Activity#onPause} callback
1130 * completes.
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001131 *
Nick Pellyc84c89a2011-08-22 22:27:11 -07001132 * <p>Strongly recommend to use the new {@link #setNdefPushMessage}
1133 * instead: it automatically hooks into your activity life-cycle,
1134 * so you do not need to call enable/disable in your onResume/onPause.
1135 *
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001136 * <p>This method must be called from the main thread.
Nick Pelly74fe6c62011-02-02 22:37:40 -08001137 *
Nick Pelly39cf3a42011-02-07 17:04:21 +09001138 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
1139 *
Nick Pelly74fe6c62011-02-02 22:37:40 -08001140 * @param activity the Foreground activity
1141 * @throws IllegalStateException if the Activity has already been paused
Nick Pellyc84c89a2011-08-22 22:27:11 -07001142 * @deprecated use {@link #setNdefPushMessage} instead
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001143 */
Nick Pellyc97a5522012-01-05 15:13:01 +11001144 @Deprecated
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001145 public void disableForegroundNdefPush(Activity activity) {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001146 if (activity == null) {
1147 throw new NullPointerException();
1148 }
1149 enforceResumed(activity);
Martijn Coenen1fa2aff2013-02-27 09:21:22 -08001150 mNfcActivityManager.setNdefPushMessage(activity, null, 0);
1151 mNfcActivityManager.setNdefPushMessageCallback(activity, null, 0);
Nick Pellyc84c89a2011-08-22 22:27:11 -07001152 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, null);
Jeff Hamilton33ff2402011-01-17 07:59:03 -08001153 }
1154
Nick Pellyc84c89a2011-08-22 22:27:11 -07001155 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001156 * Enable NDEF Push feature.
1157 * <p>This API is for the Settings application.
1158 * @hide
1159 */
1160 public boolean enableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001161 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001162 return sService.enableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001163 } catch (RemoteException e) {
1164 attemptDeadServiceRecovery(e);
1165 return false;
1166 }
1167 }
1168
1169 /**
Nick Pellyc84c89a2011-08-22 22:27:11 -07001170 * Disable NDEF Push feature.
1171 * <p>This API is for the Settings application.
Martijn Coenen6d748942011-07-21 09:34:25 +02001172 * @hide
1173 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001174 public boolean disableNdefPush() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001175 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001176 return sService.disableNdefPush();
Martijn Coenen6d748942011-07-21 09:34:25 +02001177 } catch (RemoteException e) {
1178 attemptDeadServiceRecovery(e);
1179 return false;
1180 }
1181 }
1182
1183 /**
Nick Pellycccf01d2011-10-31 14:49:40 -07001184 * Return true if the NDEF Push (Android Beam) feature is enabled.
1185 * <p>This function will return true only if both NFC is enabled, and the
1186 * NDEF Push feature is enabled.
1187 * <p>Note that if NFC is enabled but NDEF Push is disabled then this
1188 * device can still <i>receive</i> NDEF messages, it just cannot send them.
1189 * <p>Applications cannot directly toggle the NDEF Push feature, but they
1190 * can request Settings UI allowing the user to toggle NDEF Push using
1191 * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code>
1192 * <p>Example usage in an Activity that requires NDEF Push:
1193 * <p><pre>
1194 * protected void onResume() {
1195 * super.onResume();
1196 * if (!nfcAdapter.isEnabled()) {
1197 * startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
1198 * } else if (!nfcAdapter.isNdefPushEnabled()) {
1199 * startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
1200 * }
Scott Main97e0a1c2012-06-25 11:22:10 -07001201 * }</pre>
Martijn Coenen6d748942011-07-21 09:34:25 +02001202 *
Nick Pellycccf01d2011-10-31 14:49:40 -07001203 * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS
Nick Pellyc84c89a2011-08-22 22:27:11 -07001204 * @return true if NDEF Push feature is enabled
Martijn Coenen6d748942011-07-21 09:34:25 +02001205 */
Nick Pellyc84c89a2011-08-22 22:27:11 -07001206 public boolean isNdefPushEnabled() {
Martijn Coenen6d748942011-07-21 09:34:25 +02001207 try {
Nick Pellyc84c89a2011-08-22 22:27:11 -07001208 return sService.isNdefPushEnabled();
Martijn Coenen6d748942011-07-21 09:34:25 +02001209 } catch (RemoteException e) {
1210 attemptDeadServiceRecovery(e);
1211 return false;
1212 }
1213 }
1214
1215 /**
Nick Pellyc97a5522012-01-05 15:13:01 +11001216 * Inject a mock NFC tag.<p>
1217 * Used for testing purposes.
1218 * <p class="note">Requires the
1219 * {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission.
1220 * @hide
1221 */
Nick Pelly1f5badc2012-01-24 13:22:58 -08001222 public void dispatch(Tag tag) {
Nick Pellyc97a5522012-01-05 15:13:01 +11001223 if (tag == null) {
1224 throw new NullPointerException("tag cannot be null");
1225 }
1226 try {
Nick Pelly1f5badc2012-01-24 13:22:58 -08001227 sService.dispatch(tag);
Nick Pellyc97a5522012-01-05 15:13:01 +11001228 } catch (RemoteException e) {
1229 attemptDeadServiceRecovery(e);
1230 }
1231 }
1232
1233 /**
Daniel Tomas90245642010-11-17 10:07:52 +01001234 * @hide
1235 */
Martijn Coenen188cddb2012-01-31 22:16:15 -08001236 public void setP2pModes(int initiatorModes, int targetModes) {
1237 try {
1238 sService.setP2pModes(initiatorModes, targetModes);
1239 } catch (RemoteException e) {
1240 attemptDeadServiceRecovery(e);
1241 }
1242 }
1243
1244 /**
1245 * @hide
1246 */
Nick Pelly367f41f2011-03-08 11:43:30 -08001247 public INfcAdapterExtras getNfcAdapterExtrasInterface() {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001248 if (mContext == null) {
1249 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
1250 + " NFC extras APIs");
1251 }
Daniel Tomas90245642010-11-17 10:07:52 +01001252 try {
Jeff Hamiltonbb951c82011-11-08 16:55:13 -06001253 return sService.getNfcAdapterExtrasInterface(mContext.getPackageName());
Daniel Tomas90245642010-11-17 10:07:52 +01001254 } catch (RemoteException e) {
Nick Pelly367f41f2011-03-08 11:43:30 -08001255 attemptDeadServiceRecovery(e);
Daniel Tomas90245642010-11-17 10:07:52 +01001256 return null;
1257 }
1258 }
Nick Pellyc84c89a2011-08-22 22:27:11 -07001259
1260 void enforceResumed(Activity activity) {
1261 if (!activity.isResumed()) {
1262 throw new IllegalStateException("API cannot be called while activity is paused");
1263 }
1264 }
Nick Pelly8ce7a272012-03-21 15:14:09 -07001265
1266 int getSdkVersion() {
1267 if (mContext == null) {
1268 return android.os.Build.VERSION_CODES.GINGERBREAD; // best guess
1269 } else {
1270 return mContext.getApplicationInfo().targetSdkVersion;
1271 }
1272 }
Nick Pelly590b73b2010-10-12 13:00:50 -07001273}