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