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