{"id":4486,"date":"2023-09-07T16:32:12","date_gmt":"2023-09-07T08:32:12","guid":{"rendered":"https:\/\/www.yimenyun.cn\/emen\/?p=4486"},"modified":"2023-09-07T16:32:12","modified_gmt":"2023-09-07T08:32:12","slug":"app%e5%b0%81%e8%a3%85%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95nfc%e5%b7%a5%e5%85%b7%e7%b1%bb%e6%93%8d%e4%bd%9c%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.yimenyun.cn\/emen\/4486\/","title":{"rendered":"app\u5c01\u88c5\u4e00\u4e2a\u7b80\u5355nfc\u5de5\u5177\u7c7b\u64cd\u4f5c\u4ecb\u7ecd"},"content":{"rendered":"

NFC\uff08Near Field Communication\uff09\u662f\u4e00\u79cd\u65e0\u7ebf\u901a\u4fe1\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u8bbe\u5907\u4e4b\u95f4\u8fd1\u8ddd\u79bb\u4ea4\u6362\u6570\u636e\u3002\u5728\u79fb\u52a8\u8bbe\u5907\u9886\u57df\uff0cNFC\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u652f\u4ed8\u3001\u8eab\u4efd\u9a8c\u8bc1\u3001\u95e8\u7981\u63a7\u5236\u7b49\u573a\u666f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c01\u88c5\u4e00\u4e2a\u7b80\u5355\u7684NFC\u5de5\u5177\u7c7b\uff0c\u7528\u4e8e\u5b9e\u73b0NFC\u529f\u80fd\u7684\u5feb\u901f\u5f00\u53d1\u3002<\/p>\n

\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3NFC\u7684\u5de5\u4f5c\u539f\u7406\u3002NFC\u4f7f\u7528\u8fd1\u573a\u611f\u5e94\u5668\uff08Near Field Induction \u5b89\u5353\u5c01\u88c5\u6253\u5305\u5de5\u5177\u5728\u54ea<\/a>Sensor\uff09\u6765\u6536\u53d1\u5c04\u9891\u4fe1\u53f7\uff0c\u5b83\u652f\u6301\u4e09\u79cd\u5de5\u4f5c\u6a21\u5f0f\uff1a\u5361\u6a21\u62df\u5668\uff08Card Emulation\uff09\u3001\u70b9\u5bf9\u70b9\uff08Peer-to-Peer\uff09\u548c\u8bfb\u5199\u5668\uff08Reader\/Writer\uff09\u3002\u5728Android\u5e73\u53f0\u4e0a\uff0c\u6211\u4eec\u4e00\u822c\u4f7f\u7528\u8bfb\u5199\u5668\u6a21\u5f0f\u8fdb\u884cNFC\u901a\u4fe1\u3002<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u6765\u521b\u5efa\u4e00\u4e2aNFC\u5de5\u5177\u7c7b\u3002<\/p>\n

“`java<\/p>\n

public class NfcUtils {<\/p>\n

private NfcAdapter mAdapter;<\/p>\n

private PendingIntent mPendingIntent;<\/p>\n

private IntentFilter[] mIntentFilters;<\/p>\n

private String[][] mTechLists;<\/p>\n

private Context mContext;<\/p>\n

public NfcUtils(Context context) {<\/p>\n

mContext = context;<\/p>\n

mAdapter = NfcAdapter.getDefaultAdapter(mContext);<\/p>\n

mPendingIntent = PendingIntent.getActivity(mContext, 0,<\/p>\n

new Intent(mContext, mContext.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);<\/p>\n

IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);<\/p>\n

mIntentFilters = new IntentFilter[]{ndef};<\/p>\n

mTechLists = new String[][]{<\/p>\n

new String[]{Ndef.class.getName()},<\/p>\n

new String[]{NdefFormatable.class.getName()}<\/p>\n

};<\/p>\n

}<\/p>\n

public void enableForegroundDispatch(Activity activity) {<\/p>\n

mAdapter.enableForegroundDispatch(activity, mPendingIntent, mIntentFilters, mTechLists);<\/p>\n

}<\/p>\n

public void disableForegroundDispatch(Activity activity) {<\/p>\n

mAdapter.disableForegroundDispatch(activity);<\/p>\n

}<\/p>\n

public void processIntent(Intent intent) {<\/p>\n

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {<\/p>\n

Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);<\/p>\n

if (rawMsgs != null) {<\/p>\n

NdefMessage[] msgs = new NdefMessage[rawMsgs.length];<\/p>\n

for (int i = 0; i <\/p>\n

msgs[i] = (NdefMessage) rawMsgs[i];<\/p>\n

}<\/p>\n

\/\/ \u5904\u7406NDEF\u6d88\u606f<\/p>\n

handleNdefMessages(msgs);<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

private void handleNdefMessages(NdefMessage[] msgs) {<\/p>\n

for (NdefMessage msg : msgs) {<\/p>\n

NdefRecord[] records = msg.getRecords();<\/p>\n

for (NdefRecord record : records) {<\/p>\n

\/\/ \u5904\u7406NDEF\u8bb0\u5f55<\/p>\n

handleNdefRecord(record);<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

private void handleNdefRecord(NdefRecord record) {<\/p>\n

byte[] payload = record.getPayload();<\/p>\n

String textEncoding = ((payload[0] & 0x80) == 0) ? “UTF-8” : “UTF-16”;<\/p>\n

int langCodeLen = payload[0] & 0x3F;<\/p>\n

try {<\/p>\n

String text = new String(payload, langCodeLen + 1, payload.length – langCodeLen – 1, textEncoding);<\/p>\n

\/\/ \u5904\u7406NDEF\u8bb0\u5f55\u5185\u5bb9<\/p>\n

handleText(text);<\/p>\n

} catch (UnsupportedEncodingException e) {<\/p>\n

e.printStackTrace();<\/p>\n

}<\/p>\n

}<\/p>\n

private void handleText(String text) {<\/p>\n

\/\/ \u5904\u7406\u6587\u672c\u5185\u5bb9<\/p>\n

Log.i(“NfcUtils”, “Text: ” + text);<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u4e0a\u8ff0NfcUtils\u7c7b\u5c01\u88c5\u4e86NFC\u76f8\u5173\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5305\u62ec\u542f\u7528\u524d\u53f0\u8c03\u5ea6\uff08enableForegroundDispatch\uff09\u3001\u7981\u7528\u524d\u53f0\u8c03\u5ea6\uff08disableForegroundDispatch\uff09\u3001\u5904\u7406NFC\u610f\u56fe\uff08processIntent\uff09\u7b49\u3002<\/p>\n

\u5728\u4f7f\u7528NfcUtils\u65f6\uff0c\u9700\u8981\u5728Activity\u7684\u751f\u547d\u5468\u671f\u65b9\u6cd5\u4e2d\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n

“`java<\/p>\n

public class MainActivity extends AppCompatActivity {<\/p>\n

private NfcUtils mNfcUtils;<\/p>\n

@Override<\/p>\n

protected void onCreate(Bundle savedInstanceState) {<\/p>\n

super.onCreate(savedInstanceState);<\/p>\n

setContentView(R.layout.activity_main);<\/p>\n

mNfcUtils = new NfcUtils(this);<\/p>\n

\/\/ \u5176\u4ed6\u521d\u59cb\u5316\u64cd\u4f5c…<\/p>\n

handleNfcIntent(getIntent());<\/p>\n

}<\/p>\n

@Override<\/p>\n

protected void onResume() {<\/p>\n

super.onResume();<\/p>\n

\n

<\/figure>\n<\/p>\n

mNfcUtils.enableForegroundDispatch(this);<\/p>\n

}<\/p>\n

@Override<\/p>\n

protected void onPause() {<\/p>\n

super.onPause();<\/p>\n

mNfcUtils.disableForegroundDispatch(this);<\/p>\n

}<\/p>\n

@Override<\/p>\n

protected void onNewIntent(Intent intent) {<\/p>\n

super.onNewIntent(intent);<\/p>\n

handleNfcIntent(intent);<\/p>\n

}<\/p>\n

private void handleNfcIntent(Intent intent) {<\/p>\n

mNfcUtils.processIntent(intent);<\/p>\n

}<\/p>\n

}<\/p>\n

“`<\/p>\n

\u5728MainActivity\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u521d\u59cb\u5316NfcUtils\uff0c\u5e76\u7f51\u5740\u4e00\u952e\u6253\u5305app\u5c01\u88c5\u7f51\u7ad9\u5de5\u5177<\/a>\u5728\u5408\u9002\u7684\u751f\u547d\u5468\u671f\u65b9\u6cd5\u4e2d\u542f\u7528\u524d\u53f0\u8c03\u5ea6\u3002\u540c\u65f6\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u91cd\u5199onNewIntent\u65b9\u6cd5\uff0c\u4ee5\u5904\u7406\u65b0\u7684NFC\u610f\u56fe\u3002<\/p>\n

\u4ee5\u4e0a\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684NFC\u5de5\u5177\u7c7b\u7684\u5c01\u88c5\u8fc7\u7a0b\u3002\u901a\u8fc7\u8be5\u5de5\u5177\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5f00\u53d1NFC\u76f8\u5173\u7684\u529f\u80fd\uff0c\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n

\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8eNFC\u529f\u80fd\u7684\u7279\u6b8a\u6027\uff0c\u4f7f\u7528NFC\u65f6\u8fd8\u9700\u8981\u76f8\u5173\u6743\u9650\u914d\u7f6e\u548c\u8bbe\u5907\u7684\u652f\u6301\uff0c\u8bf7\u53c2\u8003Android\u5b98\u65b9\u6587\u6863\u8fdb\u884c\u914d\u7f6e\u548c\u6d4b\u8bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

NFC\uff08Near Field Communication\uff09\u662f\u4e00\u79cd\u65e0\u7ebf\u901a\u4fe1\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u8bbe\u5907\u4e4b\u95f4\u8fd1\u8ddd\u79bb\u4ea4\u6362\u6570\u636e\u3002\u5728\u79fb\u52a8\u8bbe\u5907\u9886\u57df\uff0cNFC\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u652f\u4ed8\u3001\u8eab\u4efd\u9a8c\u8bc1\u3001\u95e8\u7981\u63a7\u5236\u7b49\u573a\u666f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5c01\u88c5\u4e00\u4e2a\u7b80<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[5298,8675,8674,52,1604],"topic":[],"class_list":["post-4486","post","type-post","status-publish","format-standard","hentry","category-appkf","tag-app-","tag-8675","tag-8674","tag-52","tag-1604"],"_links":{"self":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/4486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/comments?post=4486"}],"version-history":[{"count":0,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/posts\/4486\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/media?parent=4486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/categories?post=4486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/tags?post=4486"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.yimenyun.cn\/emen\/wp-json\/wp\/v2\/topic?post=4486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}