1 /* 2 * 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. 15 */ 16 17 package android.nfc; 18 19 import java.util.HashMap; 20 21 import android.annotation.SdkConstant; 22 import android.annotation.SdkConstant.SdkConstantType; 23 import android.annotation.SystemApi; 24 import android.app.Activity; 25 import android.app.ActivityThread; 26 import android.app.OnActivityPausedListener; 27 import android.app.PendingIntent; 28 import android.content.Context; 29 import android.content.IntentFilter; 30 import android.content.pm.IPackageManager; 31 import android.content.pm.PackageManager; 32 import android.net.Uri; 33 import android.nfc.tech.MifareClassic; 34 import android.nfc.tech.Ndef; 35 import android.nfc.tech.NfcA; 36 import android.nfc.tech.NfcF; 37 import android.os.Bundle; 38 import android.os.Handler; 39 import android.os.IBinder; 40 import android.os.RemoteException; 41 import android.os.ServiceManager; 42 import android.util.Log; 43 44 import java.io.IOException; 45 46 /** 47 * Represents the local NFC adapter. 48 * <p> 49 * Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC 50 * adapter for this Android device. 51 * 52 * <div class="special reference"> 53 * <h3>Developer Guides</h3> 54 * <p>For more information about using NFC, read the 55 * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p> 56 * <p>To perform basic file sharing between devices, read 57 * <a href="{@docRoot}training/beam-files/index.html">Sharing Files with NFC</a>. 58 * </div> 59 */ 60 public final class NfcAdapter { 61 static final String TAG = "NFC"; 62 63 /** 64 * Intent to start an activity when a tag with NDEF payload is discovered. 65 * 66 * <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and 67 * looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the 68 * intent will contain the URI in its data field. If a MIME record is found the intent will 69 * contain the MIME type in its type field. This allows activities to register 70 * {@link IntentFilter}s targeting specific content on tags. Activities should register the 71 * most specific intent filters possible to avoid the activity chooser dialog, which can 72 * disrupt the interaction with the tag as the user interacts with the screen. 73 * 74 * <p>If the tag has an NDEF payload this intent is started before 75 * {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither 76 * {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started. 77 * 78 * <p>The MIME type or data URI of this intent are normalized before dispatch - 79 * so that MIME, URI scheme and URI host are always lower-case. 80 */ 81 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 82 public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED"; 83 84 /** 85 * Intent to start an activity when a tag is discovered and activities are registered for the 86 * specific technologies on the tag. 87 * 88 * <p>To receive this intent an activity must include an intent filter 89 * for this action and specify the desired tech types in a 90 * manifest <code>meta-data</code> entry. Here is an example manfiest entry: 91 * <pre> 92 * <activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"> 93 * <!-- Add a technology filter --> 94 * <intent-filter> 95 * <action android:name="android.nfc.action.TECH_DISCOVERED" /> 96 * </intent-filter> 97 * 98 * <meta-data android:name="android.nfc.action.TECH_DISCOVERED" 99 * android:resource="@xml/filter_nfc" 100 * /> 101 * </activity></pre> 102 * 103 * <p>The meta-data XML file should contain one or more <code>tech-list</code> entries 104 * each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer 105 * to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA". 106 * 107 * <p>A tag matches if any of the 108 * <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each 109 * of the <code>tech-list</code>s is considered independently and the 110 * activity is considered a match is any single <code>tech-list</code> matches the tag that was 111 * discovered. This provides AND and OR semantics for filtering desired techs. Here is an 112 * example that will match any tag using {@link NfcF} or any tag using {@link NfcA}, 113 * {@link MifareClassic}, and {@link Ndef}: 114 * 115 * <pre> 116 * <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 117 * <!-- capture anything using NfcF --> 118 * <tech-list> 119 * <tech>android.nfc.tech.NfcF</tech> 120 * </tech-list> 121 * 122 * <!-- OR --> 123 * 124 * <!-- capture all MIFARE Classics with NDEF payloads --> 125 * <tech-list> 126 * <tech>android.nfc.tech.NfcA</tech> 127 * <tech>android.nfc.tech.MifareClassic</tech> 128 * <tech>android.nfc.tech.Ndef</tech> 129 * </tech-list> 130 * </resources></pre> 131 * 132 * <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before 133 * {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED} 134 * this intent will not be started. If any activities respond to this intent 135 * {@link #ACTION_TAG_DISCOVERED} will not be started. 136 */ 137 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 138 public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED"; 139 140 /** 141 * Intent to start an activity when a tag is discovered. 142 * 143 * <p>This intent will not be started when a tag is discovered if any activities respond to 144 * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag. 145 */ 146 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) 147 public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED"; 148 149 /** 150 * Broadcast to only the activity that handles ACTION_TAG_DISCOVERED 151 * @hide 152 */ 153 public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST"; 154 155 /** 156 * Mandatory extra containing the {@link Tag} that was discovered for the 157 * {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and 158 * {@link #ACTION_TAG_DISCOVERED} intents. 159 */ 160 public static final String EXTRA_TAG = "android.nfc.extra.TAG"; 161 162 /** 163 * Extra containing an array of {@link NdefMessage} present on the discovered tag.<p> 164 * This extra is mandatory for {@link #ACTION_NDEF_DISCOVERED} intents, 165 * and optional for {@link #ACTION_TECH_DISCOVERED}, and 166 * {@link #ACTION_TAG_DISCOVERED} intents.<p> 167 * When this extra is present there will always be at least one 168 * {@link NdefMessage} element. Most NDEF tags have only one NDEF message, 169 * but we use an array for future compatibility. 170 */ 171 public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES"; 172 173 /** 174 * Optional extra containing a byte array containing the ID of the discovered tag for 175 * the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and 176 * {@link #ACTION_TAG_DISCOVERED} intents. 177 */ 178 public static final String EXTRA_ID = "android.nfc.extra.ID"; 179 180 /** 181 * Broadcast Action: The state of the local NFC adapter has been 182 * changed. 183 * <p>For example, NFC has been turned on or off. 184 * <p>Always contains the extra field {@link #EXTRA_ADAPTER_STATE} 185 */ 186 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 187 public static final String ACTION_ADAPTER_STATE_CHANGED = 188 "android.nfc.action.ADAPTER_STATE_CHANGED"; 189 190 /** 191 * Used as an int extra field in {@link #ACTION_ADAPTER_STATE_CHANGED} 192 * intents to request the current power state. Possible values are: 193 * {@link #STATE_OFF}, 194 * {@link #STATE_TURNING_ON}, 195 * {@link #STATE_ON}, 196 * {@link #STATE_TURNING_OFF}, 197 */ 198 public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE"; 199 200 public static final int STATE_OFF = 1; 201 public static final int STATE_TURNING_ON = 2; 202 public static final int STATE_ON = 3; 203 public static final int STATE_TURNING_OFF = 4; 204 205 /** 206 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. 207 * <p> 208 * Setting this flag enables polling for Nfc-A technology. 209 */ 210 public static final int FLAG_READER_NFC_A = 0x1; 211 212 /** 213 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. 214 * <p> 215 * Setting this flag enables polling for Nfc-B technology. 216 */ 217 public static final int FLAG_READER_NFC_B = 0x2; 218 219 /** 220 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. 221 * <p> 222 * Setting this flag enables polling for Nfc-F technology. 223 */ 224 public static final int FLAG_READER_NFC_F = 0x4; 225 226 /** 227 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. 228 * <p> 229 * Setting this flag enables polling for Nfc-V (ISO15693) technology. 230 */ 231 public static final int FLAG_READER_NFC_V = 0x8; 232 233 /** 234 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. 235 * <p> 236 * Setting this flag enables polling for NfcBarcode technology. 237 */ 238 public static final int FLAG_READER_NFC_BARCODE = 0x10; 239 240 /** 241 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. 242 * <p> 243 * Setting this flag allows the caller to prevent the 244 * platform from performing an NDEF check on the tags it 245 * finds. 246 */ 247 public static final int FLAG_READER_SKIP_NDEF_CHECK = 0x80; 248 249 /** 250 * Flag for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. 251 * <p> 252 * Setting this flag allows the caller to prevent the 253 * platform from playing sounds when it discovers a tag. 254 */ 255 public static final int FLAG_READER_NO_PLATFORM_SOUNDS = 0x100; 256 257 /** 258 * Int Extra for use with {@link #enableReaderMode(Activity, ReaderCallback, int, Bundle)}. 259 * <p> 260 * Setting this integer extra allows the calling application to specify 261 * the delay that the platform will use for performing presence checks 262 * on any discovered tag. 263 */ 264 public static final String EXTRA_READER_PRESENCE_CHECK_DELAY = "presence"; 265 266 /** @hide */ 267 @SystemApi 268 public static final int FLAG_NDEF_PUSH_NO_CONFIRM = 0x1; 269 270 /** @hide */ 271 public static final String ACTION_HANDOVER_TRANSFER_STARTED = 272 "android.nfc.action.HANDOVER_TRANSFER_STARTED"; 273 274 /** @hide */ 275 public static final String ACTION_HANDOVER_TRANSFER_DONE = 276 "android.nfc.action.HANDOVER_TRANSFER_DONE"; 277 278 /** @hide */ 279 public static final String EXTRA_HANDOVER_TRANSFER_STATUS = 280 "android.nfc.extra.HANDOVER_TRANSFER_STATUS"; 281 282 /** @hide */ 283 public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0; 284 /** @hide */ 285 public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1; 286 287 /** @hide */ 288 public static final String EXTRA_HANDOVER_TRANSFER_URI = 289 "android.nfc.extra.HANDOVER_TRANSFER_URI"; 290 291 // Guarded by NfcAdapter.class 292 static boolean sIsInitialized = false; 293 294 // Final after first constructor, except for 295 // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort 296 // recovery 297 static INfcAdapter sService; 298 static INfcTag sTagService; 299 static INfcCardEmulation sCardEmulationService; 300 static INfcFCardEmulation sNfcFCardEmulationService; 301 302 /** 303 * The NfcAdapter object for each application context. 304 * There is a 1-1 relationship between application context and 305 * NfcAdapter object. 306 */ 307 static HashMap<Context, NfcAdapter> sNfcAdapters = new HashMap(); //guard by NfcAdapter.class 308 309 /** 310 * NfcAdapter used with a null context. This ctor was deprecated but we have 311 * to support it for backwards compatibility. New methods that require context 312 * might throw when called on the null-context NfcAdapter. 313 */ 314 static NfcAdapter sNullContextNfcAdapter; // protected by NfcAdapter.class 315 316 final NfcActivityManager mNfcActivityManager; 317 final Context mContext; 318 final HashMap<NfcUnlockHandler, INfcUnlockHandler> mNfcUnlockHandlers; 319 final Object mLock; 320 321 ITagRemovedCallback mTagRemovedListener; // protected by mLock 322 323 /** 324 * A callback to be invoked when the system finds a tag while the foreground activity is 325 * operating in reader mode. 326 * <p>Register your {@code ReaderCallback} implementation with {@link 327 * NfcAdapter#enableReaderMode} and disable it with {@link 328 * NfcAdapter#disableReaderMode}. 329 * @see NfcAdapter#enableReaderMode 330 */ 331 public interface ReaderCallback { onTagDiscovered(Tag tag)332 public void onTagDiscovered(Tag tag); 333 } 334 335 /** 336 * A callback to be invoked when the system successfully delivers your {@link NdefMessage} 337 * to another device. 338 * @see #setOnNdefPushCompleteCallback 339 */ 340 public interface OnNdefPushCompleteCallback { 341 /** 342 * Called on successful NDEF push. 343 * 344 * <p>This callback is usually made on a binder thread (not the UI thread). 345 * 346 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set 347 * @see #setNdefPushMessageCallback 348 */ onNdefPushComplete(NfcEvent event)349 public void onNdefPushComplete(NfcEvent event); 350 } 351 352 /** 353 * A callback to be invoked when another NFC device capable of NDEF push (Android Beam) 354 * is within range. 355 * <p>Implement this interface and pass it to {@link 356 * NfcAdapter#setNdefPushMessageCallback setNdefPushMessageCallback()} in order to create an 357 * {@link NdefMessage} at the moment that another device is within range for NFC. Using this 358 * callback allows you to create a message with data that might vary based on the 359 * content currently visible to the user. Alternatively, you can call {@link 360 * #setNdefPushMessage setNdefPushMessage()} if the {@link NdefMessage} always contains the 361 * same data. 362 */ 363 public interface CreateNdefMessageCallback { 364 /** 365 * Called to provide a {@link NdefMessage} to push. 366 * 367 * <p>This callback is usually made on a binder thread (not the UI thread). 368 * 369 * <p>Called when this device is in range of another device 370 * that might support NDEF push. It allows the application to 371 * create the NDEF message only when it is required. 372 * 373 * <p>NDEF push cannot occur until this method returns, so do not 374 * block for too long. 375 * 376 * <p>The Android operating system will usually show a system UI 377 * on top of your activity during this time, so do not try to request 378 * input from the user to complete the callback, or provide custom NDEF 379 * push UI. The user probably will not see it. 380 * 381 * @param event {@link NfcEvent} with the {@link NfcEvent#nfcAdapter} field set 382 * @return NDEF message to push, or null to not provide a message 383 */ createNdefMessage(NfcEvent event)384 public NdefMessage createNdefMessage(NfcEvent event); 385 } 386 387 388 // TODO javadoc 389 public interface CreateBeamUrisCallback { createBeamUris(NfcEvent event)390 public Uri[] createBeamUris(NfcEvent event); 391 } 392 393 /** 394 * A callback that is invoked when a tag is removed from the field. 395 * @see NfcAdapter#ignore 396 */ 397 public interface OnTagRemovedListener { onTagRemoved()398 void onTagRemoved(); 399 } 400 401 /** 402 * A callback to be invoked when an application has registered as a 403 * handler to unlock the device given an NFC tag at the lockscreen. 404 * @hide 405 */ 406 @SystemApi 407 public interface NfcUnlockHandler { 408 /** 409 * Called at the lock screen to attempt to unlock the device with the given tag. 410 * @param tag the detected tag, to be used to unlock the device 411 * @return true if the device was successfully unlocked 412 */ onUnlockAttempted(Tag tag)413 public boolean onUnlockAttempted(Tag tag); 414 } 415 416 417 /** 418 * Helper to check if this device has FEATURE_NFC, but without using 419 * a context. 420 * Equivalent to 421 * context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC) 422 */ hasNfcFeature()423 private static boolean hasNfcFeature() { 424 IPackageManager pm = ActivityThread.getPackageManager(); 425 if (pm == null) { 426 Log.e(TAG, "Cannot get package manager, assuming no NFC feature"); 427 return false; 428 } 429 try { 430 return pm.hasSystemFeature(PackageManager.FEATURE_NFC, 0); 431 } catch (RemoteException e) { 432 Log.e(TAG, "Package manager query failed, assuming no NFC feature", e); 433 return false; 434 } 435 } 436 437 /** 438 * Returns the NfcAdapter for application context, 439 * or throws if NFC is not available. 440 * @hide 441 */ getNfcAdapter(Context context)442 public static synchronized NfcAdapter getNfcAdapter(Context context) { 443 if (!sIsInitialized) { 444 /* is this device meant to have NFC */ 445 if (!hasNfcFeature()) { 446 Log.v(TAG, "this device does not have NFC support"); 447 throw new UnsupportedOperationException(); 448 } 449 450 sService = getServiceInterface(); 451 if (sService == null) { 452 Log.e(TAG, "could not retrieve NFC service"); 453 throw new UnsupportedOperationException(); 454 } 455 try { 456 sTagService = sService.getNfcTagInterface(); 457 } catch (RemoteException e) { 458 Log.e(TAG, "could not retrieve NFC Tag service"); 459 throw new UnsupportedOperationException(); 460 } 461 462 try { 463 sCardEmulationService = sService.getNfcCardEmulationInterface(); 464 } catch (RemoteException e) { 465 Log.e(TAG, "could not retrieve card emulation service"); 466 throw new UnsupportedOperationException(); 467 } 468 469 try { 470 sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface(); 471 } catch (RemoteException e) { 472 Log.e(TAG, "could not retrieve NFC-F card emulation service"); 473 throw new UnsupportedOperationException(); 474 } 475 476 sIsInitialized = true; 477 } 478 if (context == null) { 479 if (sNullContextNfcAdapter == null) { 480 sNullContextNfcAdapter = new NfcAdapter(null); 481 } 482 return sNullContextNfcAdapter; 483 } 484 NfcAdapter adapter = sNfcAdapters.get(context); 485 if (adapter == null) { 486 adapter = new NfcAdapter(context); 487 sNfcAdapters.put(context, adapter); 488 } 489 return adapter; 490 } 491 492 /** get handle to NFC service interface */ getServiceInterface()493 private static INfcAdapter getServiceInterface() { 494 /* get a handle to NFC service */ 495 IBinder b = ServiceManager.getService("nfc"); 496 if (b == null) { 497 return null; 498 } 499 return INfcAdapter.Stub.asInterface(b); 500 } 501 502 /** 503 * Helper to get the default NFC Adapter. 504 * <p> 505 * Most Android devices will only have one NFC Adapter (NFC Controller). 506 * <p> 507 * This helper is the equivalent of: 508 * <pre> 509 * NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE); 510 * NfcAdapter adapter = manager.getDefaultAdapter();</pre> 511 * @param context the calling application's context 512 * 513 * @return the default NFC adapter, or null if no NFC adapter exists 514 */ getDefaultAdapter(Context context)515 public static NfcAdapter getDefaultAdapter(Context context) { 516 if (context == null) { 517 throw new IllegalArgumentException("context cannot be null"); 518 } 519 context = context.getApplicationContext(); 520 if (context == null) { 521 throw new IllegalArgumentException( 522 "context not associated with any application (using a mock context?)"); 523 } 524 /* use getSystemService() for consistency */ 525 NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE); 526 if (manager == null) { 527 // NFC not available 528 return null; 529 } 530 return manager.getDefaultAdapter(); 531 } 532 533 /** 534 * Legacy NfcAdapter getter, always use {@link #getDefaultAdapter(Context)} instead.<p> 535 * This method was deprecated at API level 10 (Gingerbread MR1) because a context is required 536 * for many NFC API methods. Those methods will fail when called on an NfcAdapter 537 * object created from this method.<p> 538 * @deprecated use {@link #getDefaultAdapter(Context)} 539 * @hide 540 */ 541 @Deprecated getDefaultAdapter()542 public static NfcAdapter getDefaultAdapter() { 543 // introduced in API version 9 (GB 2.3) 544 // deprecated in API version 10 (GB 2.3.3) 545 // removed from public API in version 16 (ICS MR2) 546 // should maintain as a hidden API for binary compatibility for a little longer 547 Log.w(TAG, "WARNING: NfcAdapter.getDefaultAdapter() is deprecated, use " + 548 "NfcAdapter.getDefaultAdapter(Context) instead", new Exception()); 549 550 return NfcAdapter.getNfcAdapter(null); 551 } 552 NfcAdapter(Context context)553 NfcAdapter(Context context) { 554 mContext = context; 555 mNfcActivityManager = new NfcActivityManager(this); 556 mNfcUnlockHandlers = new HashMap<NfcUnlockHandler, INfcUnlockHandler>(); 557 mTagRemovedListener = null; 558 mLock = new Object(); 559 } 560 561 /** 562 * @hide 563 */ getContext()564 public Context getContext() { 565 return mContext; 566 } 567 568 /** 569 * Returns the binder interface to the service. 570 * @hide 571 */ getService()572 public INfcAdapter getService() { 573 isEnabled(); // NOP call to recover sService if it is stale 574 return sService; 575 } 576 577 /** 578 * Returns the binder interface to the tag service. 579 * @hide 580 */ getTagService()581 public INfcTag getTagService() { 582 isEnabled(); // NOP call to recover sTagService if it is stale 583 return sTagService; 584 } 585 586 /** 587 * Returns the binder interface to the card emulation service. 588 * @hide 589 */ getCardEmulationService()590 public INfcCardEmulation getCardEmulationService() { 591 isEnabled(); 592 return sCardEmulationService; 593 } 594 595 /** 596 * Returns the binder interface to the NFC-F card emulation service. 597 * @hide 598 */ getNfcFCardEmulationService()599 public INfcFCardEmulation getNfcFCardEmulationService() { 600 isEnabled(); 601 return sNfcFCardEmulationService; 602 } 603 604 /** 605 * NFC service dead - attempt best effort recovery 606 * @hide 607 */ attemptDeadServiceRecovery(Exception e)608 public void attemptDeadServiceRecovery(Exception e) { 609 Log.e(TAG, "NFC service dead - attempting to recover", e); 610 INfcAdapter service = getServiceInterface(); 611 if (service == null) { 612 Log.e(TAG, "could not retrieve NFC service during service recovery"); 613 // nothing more can be done now, sService is still stale, we'll hit 614 // this recovery path again later 615 return; 616 } 617 // assigning to sService is not thread-safe, but this is best-effort code 618 // and on a well-behaved system should never happen 619 sService = service; 620 try { 621 sTagService = service.getNfcTagInterface(); 622 } catch (RemoteException ee) { 623 Log.e(TAG, "could not retrieve NFC tag service during service recovery"); 624 // nothing more can be done now, sService is still stale, we'll hit 625 // this recovery path again later 626 return; 627 } 628 629 try { 630 sCardEmulationService = service.getNfcCardEmulationInterface(); 631 } catch (RemoteException ee) { 632 Log.e(TAG, "could not retrieve NFC card emulation service during service recovery"); 633 } 634 635 try { 636 sNfcFCardEmulationService = service.getNfcFCardEmulationInterface(); 637 } catch (RemoteException ee) { 638 Log.e(TAG, "could not retrieve NFC-F card emulation service during service recovery"); 639 } 640 641 return; 642 } 643 644 /** 645 * Return true if this NFC Adapter has any features enabled. 646 * 647 * <p>If this method returns false, the NFC hardware is guaranteed not to 648 * generate or respond to any NFC communication over its NFC radio. 649 * <p>Applications can use this to check if NFC is enabled. Applications 650 * can request Settings UI allowing the user to toggle NFC using: 651 * <p><pre>startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))</pre> 652 * 653 * @see android.provider.Settings#ACTION_NFC_SETTINGS 654 * @return true if this NFC Adapter has any features enabled 655 */ isEnabled()656 public boolean isEnabled() { 657 try { 658 return sService.getState() == STATE_ON; 659 } catch (RemoteException e) { 660 attemptDeadServiceRecovery(e); 661 return false; 662 } 663 } 664 665 /** 666 * Return the state of this NFC Adapter. 667 * 668 * <p>Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON}, 669 * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}. 670 * 671 * <p>{@link #isEnabled()} is equivalent to 672 * <code>{@link #getAdapterState()} == {@link #STATE_ON}</code> 673 * 674 * @return the current state of this NFC adapter 675 * 676 * @hide 677 */ getAdapterState()678 public int getAdapterState() { 679 try { 680 return sService.getState(); 681 } catch (RemoteException e) { 682 attemptDeadServiceRecovery(e); 683 return NfcAdapter.STATE_OFF; 684 } 685 } 686 687 /** 688 * Enable NFC hardware. 689 * 690 * <p>This call is asynchronous. Listen for 691 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the 692 * operation is complete. 693 * 694 * <p>If this returns true, then either NFC is already on, or 695 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent 696 * to indicate a state transition. If this returns false, then 697 * there is some problem that prevents an attempt to turn 698 * NFC on (for example we are in airplane mode and NFC is not 699 * toggleable in airplane mode on this platform). 700 * 701 * @hide 702 */ 703 @SystemApi enable()704 public boolean enable() { 705 try { 706 return sService.enable(); 707 } catch (RemoteException e) { 708 attemptDeadServiceRecovery(e); 709 return false; 710 } 711 } 712 713 /** 714 * Disable NFC hardware. 715 * 716 * <p>No NFC features will work after this call, and the hardware 717 * will not perform or respond to any NFC communication. 718 * 719 * <p>This call is asynchronous. Listen for 720 * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the 721 * operation is complete. 722 * 723 * <p>If this returns true, then either NFC is already off, or 724 * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent 725 * to indicate a state transition. If this returns false, then 726 * there is some problem that prevents an attempt to turn 727 * NFC off. 728 * 729 * @hide 730 */ 731 @SystemApi disable()732 public boolean disable() { 733 try { 734 return sService.disable(true); 735 } catch (RemoteException e) { 736 attemptDeadServiceRecovery(e); 737 return false; 738 } 739 } 740 741 /** 742 * Disable NFC hardware. 743 * @hide 744 */ 745 @SystemApi disable(boolean persist)746 public boolean disable(boolean persist) { 747 try { 748 return sService.disable(persist); 749 } catch (RemoteException e) { 750 attemptDeadServiceRecovery(e); 751 return false; 752 } 753 } 754 755 /** 756 * Pauses polling for a {@code timeoutInMs} millis. If polling must be resumed before timeout, 757 * use {@link #resumePolling()}. 758 * @hide 759 */ pausePolling(int timeoutInMs)760 public void pausePolling(int timeoutInMs) { 761 try { 762 sService.pausePolling(timeoutInMs); 763 } catch (RemoteException e) { 764 attemptDeadServiceRecovery(e); 765 } 766 } 767 768 /** 769 * Resumes default polling for the current device state if polling is paused. Calling 770 * this while polling is not paused is a no-op. 771 * 772 * @hide 773 */ resumePolling()774 public void resumePolling() { 775 try { 776 sService.resumePolling(); 777 } catch (RemoteException e) { 778 attemptDeadServiceRecovery(e); 779 } 780 } 781 782 /** 783 * Set one or more {@link Uri}s to send using Android Beam (TM). Every 784 * Uri you provide must have either scheme 'file' or scheme 'content'. 785 * 786 * <p>For the data provided through this method, Android Beam tries to 787 * switch to alternate transports such as Bluetooth to achieve a fast 788 * transfer speed. Hence this method is very suitable 789 * for transferring large files such as pictures or songs. 790 * 791 * <p>The receiving side will store the content of each Uri in 792 * a file and present a notification to the user to open the file 793 * with a {@link android.content.Intent} with action 794 * {@link android.content.Intent#ACTION_VIEW}. 795 * If multiple URIs are sent, the {@link android.content.Intent} will refer 796 * to the first of the stored files. 797 * 798 * <p>This method may be called at any time before {@link Activity#onDestroy}, 799 * but the URI(s) are only made available for Android Beam when the 800 * specified activity(s) are in resumed (foreground) state. The recommended 801 * approach is to call this method during your Activity's 802 * {@link Activity#onCreate} - see sample 803 * code below. This method does not immediately perform any I/O or blocking work, 804 * so is safe to call on your main thread. 805 * 806 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback} 807 * have priority over both {@link #setNdefPushMessage} and 808 * {@link #setNdefPushMessageCallback}. 809 * 810 * <p>If {@link #setBeamPushUris} is called with a null Uri array, 811 * and/or {@link #setBeamPushUrisCallback} is called with a null callback, 812 * then the Uri push will be completely disabled for the specified activity(s). 813 * 814 * <p>Code example: 815 * <pre> 816 * protected void onCreate(Bundle savedInstanceState) { 817 * super.onCreate(savedInstanceState); 818 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 819 * if (nfcAdapter == null) return; // NFC not available on this device 820 * nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this); 821 * }</pre> 822 * And that is it. Only one call per activity is necessary. The Android 823 * OS will automatically release its references to the Uri(s) and the 824 * Activity object when it is destroyed if you follow this pattern. 825 * 826 * <p>If your Activity wants to dynamically supply Uri(s), 827 * then set a callback using {@link #setBeamPushUrisCallback} instead 828 * of using this method. 829 * 830 * <p class="note">Do not pass in an Activity that has already been through 831 * {@link Activity#onDestroy}. This is guaranteed if you call this API 832 * during {@link Activity#onCreate}. 833 * 834 * <p class="note">If this device does not support alternate transports 835 * such as Bluetooth or WiFI, calling this method does nothing. 836 * 837 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 838 * 839 * @param uris an array of Uri(s) to push over Android Beam 840 * @param activity activity for which the Uri(s) will be pushed 841 */ setBeamPushUris(Uri[] uris, Activity activity)842 public void setBeamPushUris(Uri[] uris, Activity activity) { 843 if (activity == null) { 844 throw new NullPointerException("activity cannot be null"); 845 } 846 if (uris != null) { 847 for (Uri uri : uris) { 848 if (uri == null) throw new NullPointerException("Uri not " + 849 "allowed to be null"); 850 String scheme = uri.getScheme(); 851 if (scheme == null || (!scheme.equalsIgnoreCase("file") && 852 !scheme.equalsIgnoreCase("content"))) { 853 throw new IllegalArgumentException("URI needs to have " + 854 "either scheme file or scheme content"); 855 } 856 } 857 } 858 mNfcActivityManager.setNdefPushContentUri(activity, uris); 859 } 860 861 /** 862 * Set a callback that will dynamically generate one or more {@link Uri}s 863 * to send using Android Beam (TM). Every Uri the callback provides 864 * must have either scheme 'file' or scheme 'content'. 865 * 866 * <p>For the data provided through this callback, Android Beam tries to 867 * switch to alternate transports such as Bluetooth to achieve a fast 868 * transfer speed. Hence this method is very suitable 869 * for transferring large files such as pictures or songs. 870 * 871 * <p>The receiving side will store the content of each Uri in 872 * a file and present a notification to the user to open the file 873 * with a {@link android.content.Intent} with action 874 * {@link android.content.Intent#ACTION_VIEW}. 875 * If multiple URIs are sent, the {@link android.content.Intent} will refer 876 * to the first of the stored files. 877 * 878 * <p>This method may be called at any time before {@link Activity#onDestroy}, 879 * but the URI(s) are only made available for Android Beam when the 880 * specified activity(s) are in resumed (foreground) state. The recommended 881 * approach is to call this method during your Activity's 882 * {@link Activity#onCreate} - see sample 883 * code below. This method does not immediately perform any I/O or blocking work, 884 * so is safe to call on your main thread. 885 * 886 * <p>{@link #setBeamPushUris} and {@link #setBeamPushUrisCallback} 887 * have priority over both {@link #setNdefPushMessage} and 888 * {@link #setNdefPushMessageCallback}. 889 * 890 * <p>If {@link #setBeamPushUris} is called with a null Uri array, 891 * and/or {@link #setBeamPushUrisCallback} is called with a null callback, 892 * then the Uri push will be completely disabled for the specified activity(s). 893 * 894 * <p>Code example: 895 * <pre> 896 * protected void onCreate(Bundle savedInstanceState) { 897 * super.onCreate(savedInstanceState); 898 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 899 * if (nfcAdapter == null) return; // NFC not available on this device 900 * nfcAdapter.setBeamPushUrisCallback(callback, this); 901 * }</pre> 902 * And that is it. Only one call per activity is necessary. The Android 903 * OS will automatically release its references to the Uri(s) and the 904 * Activity object when it is destroyed if you follow this pattern. 905 * 906 * <p class="note">Do not pass in an Activity that has already been through 907 * {@link Activity#onDestroy}. This is guaranteed if you call this API 908 * during {@link Activity#onCreate}. 909 * 910 * <p class="note">If this device does not support alternate transports 911 * such as Bluetooth or WiFI, calling this method does nothing. 912 * 913 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 914 * 915 * @param callback callback, or null to disable 916 * @param activity activity for which the Uri(s) will be pushed 917 */ setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity)918 public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) { 919 if (activity == null) { 920 throw new NullPointerException("activity cannot be null"); 921 } 922 mNfcActivityManager.setNdefPushContentUriCallback(activity, callback); 923 } 924 925 /** 926 * Set a static {@link NdefMessage} to send using Android Beam (TM). 927 * 928 * <p>This method may be called at any time before {@link Activity#onDestroy}, 929 * but the NDEF message is only made available for NDEF push when the 930 * specified activity(s) are in resumed (foreground) state. The recommended 931 * approach is to call this method during your Activity's 932 * {@link Activity#onCreate} - see sample 933 * code below. This method does not immediately perform any I/O or blocking work, 934 * so is safe to call on your main thread. 935 * 936 * <p>Only one NDEF message can be pushed by the currently resumed activity. 937 * If both {@link #setNdefPushMessage} and 938 * {@link #setNdefPushMessageCallback} are set, then 939 * the callback will take priority. 940 * 941 * <p>If neither {@link #setNdefPushMessage} or 942 * {@link #setNdefPushMessageCallback} have been called for your activity, then 943 * the Android OS may choose to send a default NDEF message on your behalf, 944 * such as a URI for your application. 945 * 946 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message, 947 * and/or {@link #setNdefPushMessageCallback} is called with a null callback, 948 * then NDEF push will be completely disabled for the specified activity(s). 949 * This also disables any default NDEF message the Android OS would have 950 * otherwise sent on your behalf for those activity(s). 951 * 952 * <p>If you want to prevent the Android OS from sending default NDEF 953 * messages completely (for all activities), you can include a 954 * {@code <meta-data>} element inside the {@code <application>} 955 * element of your AndroidManifest.xml file, like this: 956 * <pre> 957 * <application ...> 958 * <meta-data android:name="android.nfc.disable_beam_default" 959 * android:value="true" /> 960 * </application></pre> 961 * 962 * <p>The API allows for multiple activities to be specified at a time, 963 * but it is strongly recommended to just register one at a time, 964 * and to do so during the activity's {@link Activity#onCreate}. For example: 965 * <pre> 966 * protected void onCreate(Bundle savedInstanceState) { 967 * super.onCreate(savedInstanceState); 968 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 969 * if (nfcAdapter == null) return; // NFC not available on this device 970 * nfcAdapter.setNdefPushMessage(ndefMessage, this); 971 * }</pre> 972 * And that is it. Only one call per activity is necessary. The Android 973 * OS will automatically release its references to the NDEF message and the 974 * Activity object when it is destroyed if you follow this pattern. 975 * 976 * <p>If your Activity wants to dynamically generate an NDEF message, 977 * then set a callback using {@link #setNdefPushMessageCallback} instead 978 * of a static message. 979 * 980 * <p class="note">Do not pass in an Activity that has already been through 981 * {@link Activity#onDestroy}. This is guaranteed if you call this API 982 * during {@link Activity#onCreate}. 983 * 984 * <p class="note">For sending large content such as pictures and songs, 985 * consider using {@link #setBeamPushUris}, which switches to alternate transports 986 * such as Bluetooth to achieve a fast transfer rate. 987 * 988 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 989 * 990 * @param message NDEF message to push over NFC, or null to disable 991 * @param activity activity for which the NDEF message will be pushed 992 * @param activities optional additional activities, however we strongly recommend 993 * to only register one at a time, and to do so in that activity's 994 * {@link Activity#onCreate} 995 */ setNdefPushMessage(NdefMessage message, Activity activity, Activity ... activities)996 public void setNdefPushMessage(NdefMessage message, Activity activity, 997 Activity ... activities) { 998 int targetSdkVersion = getSdkVersion(); 999 try { 1000 if (activity == null) { 1001 throw new NullPointerException("activity cannot be null"); 1002 } 1003 mNfcActivityManager.setNdefPushMessage(activity, message, 0); 1004 for (Activity a : activities) { 1005 if (a == null) { 1006 throw new NullPointerException("activities cannot contain null"); 1007 } 1008 mNfcActivityManager.setNdefPushMessage(a, message, 0); 1009 } 1010 } catch (IllegalStateException e) { 1011 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) { 1012 // Less strict on old applications - just log the error 1013 Log.e(TAG, "Cannot call API with Activity that has already " + 1014 "been destroyed", e); 1015 } else { 1016 // Prevent new applications from making this mistake, re-throw 1017 throw(e); 1018 } 1019 } 1020 } 1021 1022 /** 1023 * @hide 1024 */ 1025 @SystemApi setNdefPushMessage(NdefMessage message, Activity activity, int flags)1026 public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) { 1027 if (activity == null) { 1028 throw new NullPointerException("activity cannot be null"); 1029 } 1030 mNfcActivityManager.setNdefPushMessage(activity, message, flags); 1031 } 1032 1033 /** 1034 * Set a callback that dynamically generates NDEF messages to send using Android Beam (TM). 1035 * 1036 * <p>This method may be called at any time before {@link Activity#onDestroy}, 1037 * but the NDEF message callback can only occur when the 1038 * specified activity(s) are in resumed (foreground) state. The recommended 1039 * approach is to call this method during your Activity's 1040 * {@link Activity#onCreate} - see sample 1041 * code below. This method does not immediately perform any I/O or blocking work, 1042 * so is safe to call on your main thread. 1043 * 1044 * <p>Only one NDEF message can be pushed by the currently resumed activity. 1045 * If both {@link #setNdefPushMessage} and 1046 * {@link #setNdefPushMessageCallback} are set, then 1047 * the callback will take priority. 1048 * 1049 * <p>If neither {@link #setNdefPushMessage} or 1050 * {@link #setNdefPushMessageCallback} have been called for your activity, then 1051 * the Android OS may choose to send a default NDEF message on your behalf, 1052 * such as a URI for your application. 1053 * 1054 * <p>If {@link #setNdefPushMessage} is called with a null NDEF message, 1055 * and/or {@link #setNdefPushMessageCallback} is called with a null callback, 1056 * then NDEF push will be completely disabled for the specified activity(s). 1057 * This also disables any default NDEF message the Android OS would have 1058 * otherwise sent on your behalf for those activity(s). 1059 * 1060 * <p>If you want to prevent the Android OS from sending default NDEF 1061 * messages completely (for all activities), you can include a 1062 * {@code <meta-data>} element inside the {@code <application>} 1063 * element of your AndroidManifest.xml file, like this: 1064 * <pre> 1065 * <application ...> 1066 * <meta-data android:name="android.nfc.disable_beam_default" 1067 * android:value="true" /> 1068 * </application></pre> 1069 * 1070 * <p>The API allows for multiple activities to be specified at a time, 1071 * but it is strongly recommended to just register one at a time, 1072 * and to do so during the activity's {@link Activity#onCreate}. For example: 1073 * <pre> 1074 * protected void onCreate(Bundle savedInstanceState) { 1075 * super.onCreate(savedInstanceState); 1076 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 1077 * if (nfcAdapter == null) return; // NFC not available on this device 1078 * nfcAdapter.setNdefPushMessageCallback(callback, this); 1079 * }</pre> 1080 * And that is it. Only one call per activity is necessary. The Android 1081 * OS will automatically release its references to the callback and the 1082 * Activity object when it is destroyed if you follow this pattern. 1083 * 1084 * <p class="note">Do not pass in an Activity that has already been through 1085 * {@link Activity#onDestroy}. This is guaranteed if you call this API 1086 * during {@link Activity#onCreate}. 1087 * <p class="note">For sending large content such as pictures and songs, 1088 * consider using {@link #setBeamPushUris}, which switches to alternate transports 1089 * such as Bluetooth to achieve a fast transfer rate. 1090 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 1091 * 1092 * @param callback callback, or null to disable 1093 * @param activity activity for which the NDEF message will be pushed 1094 * @param activities optional additional activities, however we strongly recommend 1095 * to only register one at a time, and to do so in that activity's 1096 * {@link Activity#onCreate} 1097 */ setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, Activity ... activities)1098 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, 1099 Activity ... activities) { 1100 int targetSdkVersion = getSdkVersion(); 1101 try { 1102 if (activity == null) { 1103 throw new NullPointerException("activity cannot be null"); 1104 } 1105 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, 0); 1106 for (Activity a : activities) { 1107 if (a == null) { 1108 throw new NullPointerException("activities cannot contain null"); 1109 } 1110 mNfcActivityManager.setNdefPushMessageCallback(a, callback, 0); 1111 } 1112 } catch (IllegalStateException e) { 1113 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) { 1114 // Less strict on old applications - just log the error 1115 Log.e(TAG, "Cannot call API with Activity that has already " + 1116 "been destroyed", e); 1117 } else { 1118 // Prevent new applications from making this mistake, re-throw 1119 throw(e); 1120 } 1121 } 1122 } 1123 1124 /** 1125 * @hide 1126 */ setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, int flags)1127 public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity, 1128 int flags) { 1129 if (activity == null) { 1130 throw new NullPointerException("activity cannot be null"); 1131 } 1132 mNfcActivityManager.setNdefPushMessageCallback(activity, callback, flags); 1133 } 1134 1135 /** 1136 * Set a callback on successful Android Beam (TM). 1137 * 1138 * <p>This method may be called at any time before {@link Activity#onDestroy}, 1139 * but the callback can only occur when the 1140 * specified activity(s) are in resumed (foreground) state. The recommended 1141 * approach is to call this method during your Activity's 1142 * {@link Activity#onCreate} - see sample 1143 * code below. This method does not immediately perform any I/O or blocking work, 1144 * so is safe to call on your main thread. 1145 * 1146 * <p>The API allows for multiple activities to be specified at a time, 1147 * but it is strongly recommended to just register one at a time, 1148 * and to do so during the activity's {@link Activity#onCreate}. For example: 1149 * <pre> 1150 * protected void onCreate(Bundle savedInstanceState) { 1151 * super.onCreate(savedInstanceState); 1152 * NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 1153 * if (nfcAdapter == null) return; // NFC not available on this device 1154 * nfcAdapter.setOnNdefPushCompleteCallback(callback, this); 1155 * }</pre> 1156 * And that is it. Only one call per activity is necessary. The Android 1157 * OS will automatically release its references to the callback and the 1158 * Activity object when it is destroyed if you follow this pattern. 1159 * 1160 * <p class="note">Do not pass in an Activity that has already been through 1161 * {@link Activity#onDestroy}. This is guaranteed if you call this API 1162 * during {@link Activity#onCreate}. 1163 * 1164 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 1165 * 1166 * @param callback callback, or null to disable 1167 * @param activity activity for which the NDEF message will be pushed 1168 * @param activities optional additional activities, however we strongly recommend 1169 * to only register one at a time, and to do so in that activity's 1170 * {@link Activity#onCreate} 1171 */ setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback, Activity activity, Activity ... activities)1172 public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback, 1173 Activity activity, Activity ... activities) { 1174 int targetSdkVersion = getSdkVersion(); 1175 try { 1176 if (activity == null) { 1177 throw new NullPointerException("activity cannot be null"); 1178 } 1179 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, callback); 1180 for (Activity a : activities) { 1181 if (a == null) { 1182 throw new NullPointerException("activities cannot contain null"); 1183 } 1184 mNfcActivityManager.setOnNdefPushCompleteCallback(a, callback); 1185 } 1186 } catch (IllegalStateException e) { 1187 if (targetSdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) { 1188 // Less strict on old applications - just log the error 1189 Log.e(TAG, "Cannot call API with Activity that has already " + 1190 "been destroyed", e); 1191 } else { 1192 // Prevent new applications from making this mistake, re-throw 1193 throw(e); 1194 } 1195 } 1196 } 1197 1198 /** 1199 * Enable foreground dispatch to the given Activity. 1200 * 1201 * <p>This will give give priority to the foreground activity when 1202 * dispatching a discovered {@link Tag} to an application. 1203 * 1204 * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents 1205 * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and 1206 * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED} 1207 * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled 1208 * by passing in the tech lists separately. Each first level entry in the tech list represents 1209 * an array of technologies that must all be present to match. If any of the first level sets 1210 * match then the dispatch is routed through the given PendingIntent. In other words, the second 1211 * level is ANDed together and the first level entries are ORed together. 1212 * 1213 * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters 1214 * that acts a wild card and will cause the foreground activity to receive all tags via the 1215 * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent. 1216 * 1217 * <p>This method must be called from the main thread, and only when the activity is in the 1218 * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before 1219 * the completion of their {@link Activity#onPause} callback to disable foreground dispatch 1220 * after it has been enabled. 1221 * 1222 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 1223 * 1224 * @param activity the Activity to dispatch to 1225 * @param intent the PendingIntent to start for the dispatch 1226 * @param filters the IntentFilters to override dispatching for, or null to always dispatch 1227 * @param techLists the tech lists used to perform matching for dispatching of the 1228 * {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent 1229 * @throws IllegalStateException if the Activity is not currently in the foreground 1230 */ enableForegroundDispatch(Activity activity, PendingIntent intent, IntentFilter[] filters, String[][] techLists)1231 public void enableForegroundDispatch(Activity activity, PendingIntent intent, 1232 IntentFilter[] filters, String[][] techLists) { 1233 if (activity == null || intent == null) { 1234 throw new NullPointerException(); 1235 } 1236 if (!activity.isResumed()) { 1237 throw new IllegalStateException("Foreground dispatch can only be enabled " + 1238 "when your activity is resumed"); 1239 } 1240 try { 1241 TechListParcel parcel = null; 1242 if (techLists != null && techLists.length > 0) { 1243 parcel = new TechListParcel(techLists); 1244 } 1245 ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity, 1246 mForegroundDispatchListener); 1247 sService.setForegroundDispatch(intent, filters, parcel); 1248 } catch (RemoteException e) { 1249 attemptDeadServiceRecovery(e); 1250 } 1251 } 1252 1253 /** 1254 * Disable foreground dispatch to the given activity. 1255 * 1256 * <p>After calling {@link #enableForegroundDispatch}, an activity 1257 * must call this method before its {@link Activity#onPause} callback 1258 * completes. 1259 * 1260 * <p>This method must be called from the main thread. 1261 * 1262 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 1263 * 1264 * @param activity the Activity to disable dispatch to 1265 * @throws IllegalStateException if the Activity has already been paused 1266 */ disableForegroundDispatch(Activity activity)1267 public void disableForegroundDispatch(Activity activity) { 1268 ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity, 1269 mForegroundDispatchListener); 1270 disableForegroundDispatchInternal(activity, false); 1271 } 1272 1273 OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() { 1274 @Override 1275 public void onPaused(Activity activity) { 1276 disableForegroundDispatchInternal(activity, true); 1277 } 1278 }; 1279 disableForegroundDispatchInternal(Activity activity, boolean force)1280 void disableForegroundDispatchInternal(Activity activity, boolean force) { 1281 try { 1282 sService.setForegroundDispatch(null, null, null); 1283 if (!force && !activity.isResumed()) { 1284 throw new IllegalStateException("You must disable foreground dispatching " + 1285 "while your activity is still resumed"); 1286 } 1287 } catch (RemoteException e) { 1288 attemptDeadServiceRecovery(e); 1289 } 1290 } 1291 1292 /** 1293 * Limit the NFC controller to reader mode while this Activity is in the foreground. 1294 * 1295 * <p>In this mode the NFC controller will only act as an NFC tag reader/writer, 1296 * thus disabling any peer-to-peer (Android Beam) and card-emulation modes of 1297 * the NFC adapter on this device. 1298 * 1299 * <p>Use {@link #FLAG_READER_SKIP_NDEF_CHECK} to prevent the platform from 1300 * performing any NDEF checks in reader mode. Note that this will prevent the 1301 * {@link Ndef} tag technology from being enumerated on the tag, and that 1302 * NDEF-based tag dispatch will not be functional. 1303 * 1304 * <p>For interacting with tags that are emulated on another Android device 1305 * using Android's host-based card-emulation, the recommended flags are 1306 * {@link #FLAG_READER_NFC_A} and {@link #FLAG_READER_SKIP_NDEF_CHECK}. 1307 * 1308 * @param activity the Activity that requests the adapter to be in reader mode 1309 * @param callback the callback to be called when a tag is discovered 1310 * @param flags Flags indicating poll technologies and other optional parameters 1311 * @param extras Additional extras for configuring reader mode. 1312 */ enableReaderMode(Activity activity, ReaderCallback callback, int flags, Bundle extras)1313 public void enableReaderMode(Activity activity, ReaderCallback callback, int flags, 1314 Bundle extras) { 1315 mNfcActivityManager.enableReaderMode(activity, callback, flags, extras); 1316 } 1317 1318 /** 1319 * Restore the NFC adapter to normal mode of operation: supporting 1320 * peer-to-peer (Android Beam), card emulation, and polling for 1321 * all supported tag technologies. 1322 * 1323 * @param activity the Activity that currently has reader mode enabled 1324 */ disableReaderMode(Activity activity)1325 public void disableReaderMode(Activity activity) { 1326 mNfcActivityManager.disableReaderMode(activity); 1327 } 1328 1329 /** 1330 * Manually invoke Android Beam to share data. 1331 * 1332 * <p>The Android Beam animation is normally only shown when two NFC-capable 1333 * devices come into range. 1334 * By calling this method, an Activity can invoke the Beam animation directly 1335 * even if no other NFC device is in range yet. The Beam animation will then 1336 * prompt the user to tap another NFC-capable device to complete the data 1337 * transfer. 1338 * 1339 * <p>The main advantage of using this method is that it avoids the need for the 1340 * user to tap the screen to complete the transfer, as this method already 1341 * establishes the direction of the transfer and the consent of the user to 1342 * share data. Callers are responsible for making sure that the user has 1343 * consented to sharing data on NFC tap. 1344 * 1345 * <p>Note that to use this method, the passed in Activity must have already 1346 * set data to share over Beam by using method calls such as 1347 * {@link #setNdefPushMessageCallback} or 1348 * {@link #setBeamPushUrisCallback}. 1349 * 1350 * @param activity the current foreground Activity that has registered data to share 1351 * @return whether the Beam animation was successfully invoked 1352 */ invokeBeam(Activity activity)1353 public boolean invokeBeam(Activity activity) { 1354 if (activity == null) { 1355 throw new NullPointerException("activity may not be null."); 1356 } 1357 enforceResumed(activity); 1358 try { 1359 sService.invokeBeam(); 1360 return true; 1361 } catch (RemoteException e) { 1362 Log.e(TAG, "invokeBeam: NFC process has died."); 1363 attemptDeadServiceRecovery(e); 1364 return false; 1365 } 1366 } 1367 1368 /** 1369 * @hide 1370 */ invokeBeam(BeamShareData shareData)1371 public boolean invokeBeam(BeamShareData shareData) { 1372 try { 1373 Log.e(TAG, "invokeBeamInternal()"); 1374 sService.invokeBeamInternal(shareData); 1375 return true; 1376 } catch (RemoteException e) { 1377 Log.e(TAG, "invokeBeam: NFC process has died."); 1378 attemptDeadServiceRecovery(e); 1379 return false; 1380 } 1381 } 1382 1383 /** 1384 * Enable NDEF message push over NFC while this Activity is in the foreground. 1385 * 1386 * <p>You must explicitly call this method every time the activity is 1387 * resumed, and you must call {@link #disableForegroundNdefPush} before 1388 * your activity completes {@link Activity#onPause}. 1389 * 1390 * <p>Strongly recommend to use the new {@link #setNdefPushMessage} 1391 * instead: it automatically hooks into your activity life-cycle, 1392 * so you do not need to call enable/disable in your onResume/onPause. 1393 * 1394 * <p>For NDEF push to function properly the other NFC device must 1395 * support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or 1396 * Android's "com.android.npp" (Ndef Push Protocol). This was optional 1397 * on Gingerbread level Android NFC devices, but SNEP is mandatory on 1398 * Ice-Cream-Sandwich and beyond. 1399 * 1400 * <p>This method must be called from the main thread. 1401 * 1402 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 1403 * 1404 * @param activity foreground activity 1405 * @param message a NDEF Message to push over NFC 1406 * @throws IllegalStateException if the activity is not currently in the foreground 1407 * @deprecated use {@link #setNdefPushMessage} instead 1408 */ 1409 @Deprecated enableForegroundNdefPush(Activity activity, NdefMessage message)1410 public void enableForegroundNdefPush(Activity activity, NdefMessage message) { 1411 if (activity == null || message == null) { 1412 throw new NullPointerException(); 1413 } 1414 enforceResumed(activity); 1415 mNfcActivityManager.setNdefPushMessage(activity, message, 0); 1416 } 1417 1418 /** 1419 * Disable NDEF message push over P2P. 1420 * 1421 * <p>After calling {@link #enableForegroundNdefPush}, an activity 1422 * must call this method before its {@link Activity#onPause} callback 1423 * completes. 1424 * 1425 * <p>Strongly recommend to use the new {@link #setNdefPushMessage} 1426 * instead: it automatically hooks into your activity life-cycle, 1427 * so you do not need to call enable/disable in your onResume/onPause. 1428 * 1429 * <p>This method must be called from the main thread. 1430 * 1431 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission. 1432 * 1433 * @param activity the Foreground activity 1434 * @throws IllegalStateException if the Activity has already been paused 1435 * @deprecated use {@link #setNdefPushMessage} instead 1436 */ 1437 @Deprecated disableForegroundNdefPush(Activity activity)1438 public void disableForegroundNdefPush(Activity activity) { 1439 if (activity == null) { 1440 throw new NullPointerException(); 1441 } 1442 enforceResumed(activity); 1443 mNfcActivityManager.setNdefPushMessage(activity, null, 0); 1444 mNfcActivityManager.setNdefPushMessageCallback(activity, null, 0); 1445 mNfcActivityManager.setOnNdefPushCompleteCallback(activity, null); 1446 } 1447 1448 /** 1449 * Enable NDEF Push feature. 1450 * <p>This API is for the Settings application. 1451 * @hide 1452 */ 1453 @SystemApi enableNdefPush()1454 public boolean enableNdefPush() { 1455 try { 1456 return sService.enableNdefPush(); 1457 } catch (RemoteException e) { 1458 attemptDeadServiceRecovery(e); 1459 return false; 1460 } 1461 } 1462 1463 /** 1464 * Disable NDEF Push feature. 1465 * <p>This API is for the Settings application. 1466 * @hide 1467 */ 1468 @SystemApi disableNdefPush()1469 public boolean disableNdefPush() { 1470 try { 1471 return sService.disableNdefPush(); 1472 } catch (RemoteException e) { 1473 attemptDeadServiceRecovery(e); 1474 return false; 1475 } 1476 } 1477 1478 /** 1479 * Return true if the NDEF Push (Android Beam) feature is enabled. 1480 * <p>This function will return true only if both NFC is enabled, and the 1481 * NDEF Push feature is enabled. 1482 * <p>Note that if NFC is enabled but NDEF Push is disabled then this 1483 * device can still <i>receive</i> NDEF messages, it just cannot send them. 1484 * <p>Applications cannot directly toggle the NDEF Push feature, but they 1485 * can request Settings UI allowing the user to toggle NDEF Push using 1486 * <code>startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))</code> 1487 * <p>Example usage in an Activity that requires NDEF Push: 1488 * <p><pre> 1489 * protected void onResume() { 1490 * super.onResume(); 1491 * if (!nfcAdapter.isEnabled()) { 1492 * startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)); 1493 * } else if (!nfcAdapter.isNdefPushEnabled()) { 1494 * startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS)); 1495 * } 1496 * }</pre> 1497 * 1498 * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS 1499 * @return true if NDEF Push feature is enabled 1500 */ isNdefPushEnabled()1501 public boolean isNdefPushEnabled() { 1502 try { 1503 return sService.isNdefPushEnabled(); 1504 } catch (RemoteException e) { 1505 attemptDeadServiceRecovery(e); 1506 return false; 1507 } 1508 } 1509 1510 /** 1511 * Signals that you are no longer interested in communicating with an NFC tag 1512 * for as long as it remains in range. 1513 * 1514 * All future attempted communication to this tag will fail with {@link IOException}. 1515 * The NFC controller will be put in a low-power polling mode, allowing the device 1516 * to save power in cases where it's "attached" to a tag all the time (e.g. a tag in 1517 * car dock). 1518 * 1519 * Additionally the debounceMs parameter allows you to specify for how long the tag needs 1520 * to have gone out of range, before it will be dispatched again. 1521 * 1522 * Note: the NFC controller typically polls at a pretty slow interval (100 - 500 ms). 1523 * This means that if the tag repeatedly goes in and out of range (for example, in 1524 * case of a flaky connection), and the controller happens to poll every time the 1525 * tag is out of range, it *will* re-dispatch the tag after debounceMs, despite the tag 1526 * having been "in range" during the interval. 1527 * 1528 * Note 2: if a tag with another UID is detected after this API is called, its effect 1529 * will be cancelled; if this tag shows up before the amount of time specified in 1530 * debounceMs, it will be dispatched again. 1531 * 1532 * Note 3: some tags have a random UID, in which case this API won't work reliably. 1533 * 1534 * @param tag the {@link android.nfc.Tag Tag} to ignore. 1535 * @param debounceMs minimum amount of time the tag needs to be out of range before being 1536 * dispatched again. 1537 * @param tagRemovedListener listener to be called when the tag is removed from the field. 1538 * Note that this will only be called if the tag has been out of range 1539 * for at least debounceMs, or if another tag came into range before 1540 * debounceMs. May be null in case you don't want a callback. 1541 * @param handler the {@link android.os.Handler Handler} that will be used for delivering 1542 * the callback. if the handler is null, then the thread used for delivering 1543 * the callback is unspecified. 1544 * @return false if the tag couldn't be found (or has already gone out of range), true otherwise 1545 */ ignore(final Tag tag, int debounceMs, final OnTagRemovedListener tagRemovedListener, final Handler handler)1546 public boolean ignore(final Tag tag, int debounceMs, 1547 final OnTagRemovedListener tagRemovedListener, final Handler handler) { 1548 ITagRemovedCallback.Stub iListener = null; 1549 if (tagRemovedListener != null) { 1550 iListener = new ITagRemovedCallback.Stub() { 1551 @Override 1552 public void onTagRemoved() throws RemoteException { 1553 if (handler != null) { 1554 handler.post(new Runnable() { 1555 @Override 1556 public void run() { 1557 tagRemovedListener.onTagRemoved(); 1558 } 1559 }); 1560 } else { 1561 tagRemovedListener.onTagRemoved(); 1562 } 1563 synchronized (mLock) { 1564 mTagRemovedListener = null; 1565 } 1566 } 1567 }; 1568 } 1569 synchronized (mLock) { 1570 mTagRemovedListener = iListener; 1571 } 1572 try { 1573 return sService.ignore(tag.getServiceHandle(), debounceMs, iListener); 1574 } catch (RemoteException e) { 1575 return false; 1576 } 1577 } 1578 1579 /** 1580 * Inject a mock NFC tag.<p> 1581 * Used for testing purposes. 1582 * <p class="note">Requires the 1583 * {@link android.Manifest.permission#WRITE_SECURE_SETTINGS} permission. 1584 * @hide 1585 */ dispatch(Tag tag)1586 public void dispatch(Tag tag) { 1587 if (tag == null) { 1588 throw new NullPointerException("tag cannot be null"); 1589 } 1590 try { 1591 sService.dispatch(tag); 1592 } catch (RemoteException e) { 1593 attemptDeadServiceRecovery(e); 1594 } 1595 } 1596 1597 /** 1598 * @hide 1599 */ setP2pModes(int initiatorModes, int targetModes)1600 public void setP2pModes(int initiatorModes, int targetModes) { 1601 try { 1602 sService.setP2pModes(initiatorModes, targetModes); 1603 } catch (RemoteException e) { 1604 attemptDeadServiceRecovery(e); 1605 } 1606 } 1607 1608 /** 1609 * Registers a new NFC unlock handler with the NFC service. 1610 * 1611 * <p />NFC unlock handlers are intended to unlock the keyguard in the presence of a trusted 1612 * NFC device. The handler should return true if it successfully authenticates the user and 1613 * unlocks the keyguard. 1614 * 1615 * <p /> The parameter {@code tagTechnologies} determines which Tag technologies will be polled for 1616 * at the lockscreen. Polling for less tag technologies reduces latency, and so it is 1617 * strongly recommended to only provide the Tag technologies that the handler is expected to 1618 * receive. There must be at least one tag technology provided, otherwise the unlock handler 1619 * is ignored. 1620 * 1621 * @hide 1622 */ 1623 @SystemApi addNfcUnlockHandler(final NfcUnlockHandler unlockHandler, String[] tagTechnologies)1624 public boolean addNfcUnlockHandler(final NfcUnlockHandler unlockHandler, 1625 String[] tagTechnologies) { 1626 // If there are no tag technologies, don't bother adding unlock handler 1627 if (tagTechnologies.length == 0) { 1628 return false; 1629 } 1630 1631 try { 1632 synchronized (mLock) { 1633 if (mNfcUnlockHandlers.containsKey(unlockHandler)) { 1634 // update the tag technologies 1635 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.get(unlockHandler)); 1636 mNfcUnlockHandlers.remove(unlockHandler); 1637 } 1638 1639 INfcUnlockHandler.Stub iHandler = new INfcUnlockHandler.Stub() { 1640 @Override 1641 public boolean onUnlockAttempted(Tag tag) throws RemoteException { 1642 return unlockHandler.onUnlockAttempted(tag); 1643 } 1644 }; 1645 1646 sService.addNfcUnlockHandler(iHandler, 1647 Tag.getTechCodesFromStrings(tagTechnologies)); 1648 mNfcUnlockHandlers.put(unlockHandler, iHandler); 1649 } 1650 } catch (RemoteException e) { 1651 attemptDeadServiceRecovery(e); 1652 return false; 1653 } catch (IllegalArgumentException e) { 1654 Log.e(TAG, "Unable to register LockscreenDispatch", e); 1655 return false; 1656 } 1657 1658 return true; 1659 } 1660 1661 /** 1662 * Removes a previously registered unlock handler. Also removes the tag technologies 1663 * associated with the removed unlock handler. 1664 * 1665 * @hide 1666 */ 1667 @SystemApi removeNfcUnlockHandler(NfcUnlockHandler unlockHandler)1668 public boolean removeNfcUnlockHandler(NfcUnlockHandler unlockHandler) { 1669 try { 1670 synchronized (mLock) { 1671 if (mNfcUnlockHandlers.containsKey(unlockHandler)) { 1672 sService.removeNfcUnlockHandler(mNfcUnlockHandlers.remove(unlockHandler)); 1673 } 1674 1675 return true; 1676 } 1677 } catch (RemoteException e) { 1678 attemptDeadServiceRecovery(e); 1679 return false; 1680 } 1681 } 1682 1683 /** 1684 * @hide 1685 */ getNfcAdapterExtrasInterface()1686 public INfcAdapterExtras getNfcAdapterExtrasInterface() { 1687 if (mContext == null) { 1688 throw new UnsupportedOperationException("You need a context on NfcAdapter to use the " 1689 + " NFC extras APIs"); 1690 } 1691 try { 1692 return sService.getNfcAdapterExtrasInterface(mContext.getPackageName()); 1693 } catch (RemoteException e) { 1694 attemptDeadServiceRecovery(e); 1695 return null; 1696 } 1697 } 1698 enforceResumed(Activity activity)1699 void enforceResumed(Activity activity) { 1700 if (!activity.isResumed()) { 1701 throw new IllegalStateException("API cannot be called while activity is paused"); 1702 } 1703 } 1704 getSdkVersion()1705 int getSdkVersion() { 1706 if (mContext == null) { 1707 return android.os.Build.VERSION_CODES.GINGERBREAD; // best guess 1708 } else { 1709 return mContext.getApplicationInfo().targetSdkVersion; 1710 } 1711 } 1712 } 1713