{"id":5409,"date":"2023-05-18T12:05:31","date_gmt":"2023-05-18T04:05:31","guid":{"rendered":"https:\/\/app.applebyme.cn\/cloud\/?p=5409"},"modified":"2023-05-18T12:05:31","modified_gmt":"2023-05-18T04:05:31","slug":"android-nfc%e5%bc%80%e5%8f%91%e9%9c%80%e8%a6%81%e6%b3%a8%e6%84%8f%e5%93%aa%e4%ba%9b%e4%ba%8b%e9%a1%b9%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/app.applebyme.cn\/cloud\/appshangjia\/5409.html","title":{"rendered":"android nfc\u5f00\u53d1\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f"},"content":{"rendered":"<p>Android NFC \u5f00\u53d1\u662f\u6307\u5229\u7528 Android \u5e73\u53f0\u63d0\u4f9b\u7684 NFC \u6280\u672f\uff0c\u5b9e\u73b0\u5728 Android \u8bbe\u5907\u4e0a\u8fdb\u884c NFC \u4ea4\u4e92\u7684\u529f\u80fd\u5f00\u53d1\u3002NFC\uff08Near Field Communication\uff09\u662f\u4e00\u79cd\u77ed\u8ddd\u79bb\u65e0\u7ebf\u901a\u4fe1\u6280\u672f\uff0c\u901a\u5e38\u7528\u4e8e\u79fb\u52a8\u8bbe\u5907\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u8f93\u548c\u8eab\u4efd\u9a8c\u8bc1\u3002\u5728 Android \u5e73\u53f0\u4e0a\uff0cNFC \u6280\u672f\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u591a\u79cd\u529f\u80fd\uff0c\u5982\u652f\u4ed8\u3001\u95e8\u7981\u5361\u3001\u516c\u4ea4\u5361\u7b49\u3002<\/p>\n<p>NFC \u6280\u672f\u7684\u539f\u7406\u662f\u5229\u7528\u9ad8\u9891\u7535\u78c1\u6ce2\u8fdb\u884c\u77ed\u8ddd\u79bb\u901a\u4fe1\u3002\u5728 Android \u8bbe\u5907\u4e0a\uff0cNFC \u82af\u7247\u53ef\u4ee5\u901a\u8fc7\u8fd1\u573a\u611f\u5e94\u8fdb\u884c\u901a\u4fe1\u3002NFC \u82af\u7247\u5305\u542b\u4e00\u4e2a\u5929\u7ebf\uff0c\u53ef\u4ee5\u901a\u8fc7\u611f\u5e94\u6765\u63a5\u6536\u548c\u53d1\u9001\u4fe1\u606f\u3002\u5f53\u4e24\u4e2a NFC \u8bbe\u5907\u9760\u8fd1\u65f6\uff0c\u5b83\u4eec\u7684 NFC \u82af\u7247\u4f1a\u4e92\u76f8\u611f\u5e94\u5e76\u5efa\u7acb\u8fde\u63a5\uff0c\u7136\u540e\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u3002<\/p>\n<p>\u5728 Android \u5e73\u53f0\u4e0a\uff0cNFC \u6280\u672f\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u6765\u4f7f\u7528\uff1a<\/p>\n<p>1. Android Beam<\/p>\n<p>Android Beam \u662f Android \u5e73\u53f0\u4e0a\u7684\u4e00\u79cd NFC \u6280\u672f\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u4e24\u4e2a Android \u8bbe\u5907\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u3002\u5f53\u4e24\u4e2a\u8bbe\u5907\u9760\u8fd1\u65f6\uff0c\u5b83\u4eec\u7684 NFC \u82af\u7247\u4f1a\u5efa\u7acb\u8fde\u63a5\u5e76\u81ea\u52a8\u542f\u52a8 Android Beam\u3002\u968f\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u5c06\u6587\u4ef6\u3001\u56fe\u7247\u3001\u97f3\u4e50\u7b49\u6570\u636e\u901a\u8fc7 NFC \u4f20\u8f93\u5230\u53e6\u4e00\u4e2a\u8bbe\u5907\u3002<\/p>\n<p>2. Host Card Emulation<\/p>\n<p>Host Card Emulation\uff08HCE\uff09\u662f\u4e00\u79cd\u6a21\u62df NFC \u5361\u7247\u7684\u6280\u672f\uff0c\u53ef\u4ee5\u8ba9 Android \u8bbe\u5907\u6a21\u62df\u6210\u4e00\u5f20 NFC \u5361\u7247\uff0c\u7528\u4e8e\u652f\u4ed8\u3001\u95e8\u7981\u5361\u3001\u516c\u4ea4\u5361\u7b49\u573a\u666f\u3002\u5f53\u7528\u6237\u5c06\u8bbe\u5907\u9760\u8fd1 NFC \u8bfb\u5361\u5668\u65f6\uff0cAndroid \u8bbe\u5907\u4f1a\u6a21\u62df\u6210\u4e00\u5f20 NFC \u5361\u7247\uff0c\u5e76\u5c06\u76f8\u5173\u4fe1\u606f\u4f20\u9012\u7ed9\u8bfb\u5361\u5668\u3002<\/p>\n<p>\u5728 Android \u5e73\u53f0\u4e0a\u8fdb\u884c NFC \u5f00\u53d1\uff0c\u9700\u8981\u4f7f\u7528 Android SDK \u4e2d\u63d0\u4f9b\u7684 NFC API\u3002\u4ee5\u4e0b\u662f NFC \u5f00\u53d1\u7684\u4e3b\u8981\u6b65\u9aa4\uff1a<\/p>\n<p>1. \u83b7\u53d6 NFC \u786c\u4ef6\u652f\u6301<\/p>\n<p>\u5728 AndroidManifest.xml \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u6743\u9650\uff1a<\/p>\n<p>&#8220;`<\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>\u7136\u540e\u5728 Activity \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u6765\u68c0\u67e5\u8bbe\u5907\u662f\u5426\u652f\u6301 NFC\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>PackageManager pm = getPackageManager();<\/p>\n<p>if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {<\/p>\n<p>    \/\/ \u8bbe\u5907\u4e0d\u652f\u6301 NFC<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>2. \u6ce8\u518c NFC \u76d1\u542c\u5668<\/p>\n<p>\u5728 AndroidManifest.xml \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u6765\u6ce8\u518c NFC \u76d1\u542c\u5668\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>\n<p>\n<\/p>\n<p>        android:resource=&#8221;@xml\/nfc_tech_filter&#8221; \/&gt;<\/p>\n<\/p>\n<p>&#8220;`<\/p>\n<p>\u7136\u540e\u5728 Activity \u4e2d\u5b9e\u73b0 NFC \u76d1\u542c\u5668\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>public class MainActivity extends Activity implements NfcAdapter.ReaderCallback {<\/p>\n<p>    private NfcAdapter nfcAdapter;<\/p>\n<p>    private PendingIntent pendingIntent;<\/p>\n<p>    private IntentFilter[] intentFilters;<\/p>\n<p>    private String[][] techLists;<\/p>\n<p>    @Override<\/p>\n<p>    protected void onCreate(Bundle savedInstanceState) {<\/p>\n<p>        super.onCreate(savedInstanceState);<\/p>\n<p>        setContentView(R.layout.activity_main);<\/p>\n<p>        nfcAdapter = NfcAdapter.getDefaultAdapter(this);<\/p>\n<p>        pendingIntent = PendingIntent.getActivity(this, 0,<\/p>\n<p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-03\/31\/15\/22910916989.jpg\" \/><\/figure>\n<\/p>\n<p>     new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);<\/p>\n<p>        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);<\/p>\n<p>        try {<\/p>\n<p>            ndef.addDataType(&#8220;*\/*&#8221;);<\/p>\n<p>        } catch (IntentFilter.MalformedMimeTypeException e) {<\/p>\n<p>            throw new RuntimeException(&#8220;fail&#8221;, e);<\/p>\n<p>        }<\/p>\n<p>        intentFilters = new IntentFilter[] { ndef };<\/p>\n<p>        techL<a href=\"https:\/\/www.yimenapp.net\/dabao-3276.html\">android vue\u6df7\u5408\u5f00\u53d1<\/a>ists = new String[][] { new String[] { NfcA.class.getName() } };<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    protected void onResume() {<\/p>\n<p>        super.onResume();<\/p>\n<p>        nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, techLists);<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    protected void onPause() {<\/p>\n<p>        super.onPause();<\/p>\n<p>        nfcAdapter.disableForegroundDispatch(this);<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    public void onTagDiscovered(Tag tag) {<\/p>\n<p>        \/\/ \u5904\u7406 NFC \u6807\u7b7e\u6570\u636e<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>3. \u5904\u7406 NFC \u6807\u7b7e\u6570\u636e<\/p>\n<p>\u5f53\u7528\u6237\u5c06 NFC \u6807\u7b7e\u9760\u8fd1\u8bbe\u5907\u65f6\uff0cAndroid \u7cfb\u7edf\u4f1a\u81ea\u52a8\u8c03\u7528 Activity \u7684 onNewIntent() \u65b9\u6cd5\u3002\u5728\u8be5\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u83b7\u53d6\u5230 NFC \u6807\u7b7e\u7684\u4fe1\u606f\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>@Override<\/p>\n<p>protected void onNewIntent(Intent intent) {<\/p>\n<p>    super.onNewIntent(intent);<\/p>\n<p>    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);<\/p>\n<p>    \/\/ \u5904\u7406 NFC \u6807\u7b7e\u6570\u636e<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>4. \u53d1\u9001 NFC \u6570\u636e<\/p>\n<p>\u5728 Android Beam \u6216 HCE \u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u6765\u53d1\u9001 NFC \u6570\u636e\uff1a<\/p>\n<p>&#8220;`<\/p>\n<p>NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);<\/p>\n<p>nfcAdapter.setNdefPushMessage(ndefMessage, this);<\/p>\n<p>&#8220;`<\/p>\n<p>\u5176\u4e2d ndefMessage \u662f\u4e00\u4e2a NdefMessage \u5bf9\u8c61\uff0c\u5305\u542b\u8981\u53d1\u9001\u7684\u6570\u636e\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u5728 Android \u5e73\u53f0\u4e0a\u8fdb\u884c NFC \u5f00\u53d1\u9700\u8981\u638c\u63e1 NFC \u6280\u672f\u7684\u539f\u7406\u548c Android SDK \u4e2d\u63d0\u4f9b\u7684 NFC API\uff0c\u901a\u8fc7\u6ce8\u518c NFC \u76d1\u542c\u5668<a href=\"https:\/\/www.yimenapp.net\/dabao-1376.html\">H5\u6253\u5305\u6210APP<\/a>\u3001\u5904\u7406 NFC \u6807\u7b7e\u6570\u636e\u548c\u53d1\u9001 NFC \u6570\u636e\u7b49\u6b65\u9aa4\u6765\u5b9e\u73b0 NFC \u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android NFC \u5f00\u53d1\u662f\u6307\u5229\u7528 Android \u5e73\u53f0\u63d0\u4f9b\u7684 NFC \u6280\u672f\uff0c\u5b9e\u73b0\u5728 Android \u8bbe\u5907\u4e0a\u8fdb\u884c NFC \u4ea4\u4e92\u7684\u529f\u80fd\u5f00\u53d1\u3002NFC\uff08Near Field Communication\uff09<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[7699,7698,926,7700,882],"class_list":["post-5409","post","type-post","status-publish","format-standard","hentry","category-appshangjia","tag-webexe","tag-apk","tag-926","tag-7700","tag-882"],"_links":{"self":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/5409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/comments?post=5409"}],"version-history":[{"count":0,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/posts\/5409\/revisions"}],"wp:attachment":[{"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/media?parent=5409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/categories?post=5409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/app.applebyme.cn\/cloud\/wp-json\/wp\/v2\/tags?post=5409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}